package tests import ( "fmt" "github.com/google/uuid" "net/http" "sync" "testing" ) func TestIdempotencyContract(t *testing.T) { api := newAPI(t) alice := api.createUser(t, "alice") bob := api.createUser(t, "bob") eve := api.createUser(t, "eve") chat := api.createChat(t, alice, alice, bob) other := api.createChat(t, alice, alice) path := "/api/v1/chats/" + chat + "/messages" for _, key := range []string{"", "bad key", "кириллица", string(make([]byte, 129))} { // NUL cannot be sent by net/http: use an overlength ASCII key instead. if len(key) == 129 { key = fmt.Sprintf("%0129d", 1) } assertError(t, api.requestKey(t, "POST", path, alice, `{"text":"hello"}`, key), 400, "invalid_request") } key := uuid.NewString() first := api.requestKey(t, "POST", path, alice, `{"text":"hello"}`, key) var want map[string]any decodeOK(t, first, 201, &want) var got map[string]any decodeOK(t, api.requestKey(t, "POST", path, alice, `{ "text" : "hello" }`, key), 201, &got) if jsonString(t, want) != jsonString(t, got) { t.Fatal("replay changed message") } assertError(t, api.requestKey(t, "POST", path, alice, `{"text":"changed"}`, key), 409, "conflict") assertError(t, api.requestKey(t, "POST", path, eve, `{"text":"hello"}`, key), 404, "not_found") for _, tc := range []struct{ user, path string }{{bob, path}, {alice, "/api/v1/chats/" + other + "/messages"}} { var msg map[string]any decodeOK(t, api.requestKey(t, "POST", tc.path, tc.user, `{"text":"hello"}`, key), 201, &msg) if msg["id"] == want["id"] { t.Fatal("scope not isolated") } } resp := api.request(t, http.MethodDelete, "/api/v1/auth/sessions/current", alice, "") if resp.status != 204 { t.Fatal(resp.status) } assertError(t, api.requestKey(t, "POST", path, alice, `{"text":"hello"}`, key), 401, "unauthorized") } func TestConcurrentIdempotencyAcrossReplicas(t *testing.T) { api := newAPI(t) user := api.createUser(t, "alice") chat := api.createChat(t, user, user) path := "/api/v1/chats/" + chat + "/messages" key := uuid.NewString() const count = 32 type result struct { response apiResponse err error } responses := make(chan result, count) start := make(chan struct{}) var wg sync.WaitGroup for range count { wg.Go(func() { <-start response, err := api.send("POST", path, user, `{"text":"one logical message"}`, key) responses <- result{response: response, err: err} }) } close(start) wg.Wait() close(responses) instances := map[string]bool{} var id string for result := range responses { if result.err != nil { t.Fatal(result.err) } resp := result.response var msg struct { ID string `json:"id"` } decodeOK(t, resp, 201, &msg) if id != "" && id != msg.ID { t.Fatal("duplicate side effect") } id = msg.ID instances[resp.instanceID] = true } if len(instances) < 2 { t.Fatalf("need at least 2 real API replicas, observed %d", len(instances)) } var page struct { Items []struct { ID string `json:"id"` } `json:"items"` } decodeOK(t, api.request(t, "GET", path, user, ""), 200, &page) if len(page.Items) != 1 || page.Items[0].ID != id { t.Fatalf("history contains %d messages", len(page.Items)) } t.Logf("%d requests across %d replicas created exactly one message", count, len(instances)) }