//go:build race package studenttests_test import ( "bytes" "encoding/json" "io" "net/http" "net/url" "sync" "testing" "github.com/google/uuid" ) const ( concurrentRequests = 20 concurrentRounds = 5 ) type keyedResult struct { status int instance string body []byte err error } func postWithKey(actor testUser, chatID uuid.UUID, key, text string) keyedResult { body, err := json.Marshal(map[string]string{"text": text}) if err != nil { return keyedResult{err: err} } req, err := http.NewRequest( http.MethodPost, *baseURL+"/api/v1/chats/"+chatID.String()+"/messages", bytes.NewReader(body), ) if err != nil { return keyedResult{err: err} } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+actor.Token) req.Header.Set("Idempotency-Key", key) resp, err := httpClient.Do(req) if err != nil { return keyedResult{err: err} } defer resp.Body.Close() data, err := io.ReadAll(resp.Body) return keyedResult{ status: resp.StatusCode, instance: resp.Header.Get("X-Instance-Id"), body: data, err: err, } } func TestConcurrentIdempotencyAcrossReplicas(t *testing.T) { alice := createUser(t) bob := createUser(t) chat := createChat(t, alice, alice.ID, bob.ID) instances := map[string]struct{}{} for round := 0; round < concurrentRounds; round++ { key := uuid.NewString() text := "concurrent " + key results := make([]keyedResult, concurrentRequests) start := make(chan struct{}) var wg sync.WaitGroup for i := range results { wg.Add(1) go func(i int) { defer wg.Done() <-start results[i] = postWithKey(alice, chat.ID, key, text) }(i) } close(start) wg.Wait() createdIDs := map[string]struct{}{} succeeded := 0 for i, r := range results { if r.err != nil { t.Fatalf("round %d, request %d: %v", round, i, r.err) } if r.instance != "" { instances[r.instance] = struct{}{} } switch r.status { case http.StatusOK, http.StatusCreated: succeeded++ var m struct { ID string `json:"id"` } if err := json.Unmarshal(r.body, &m); err != nil || m.ID == "" { t.Fatalf("round %d: bad success body %q: %v", round, r.body, err) } createdIDs[m.ID] = struct{}{} case http.StatusConflict: default: t.Errorf("round %d, request %d: unexpected status %d, body: %s", round, i, r.status, r.body) } } if succeeded == 0 { t.Fatalf("round %d: no request succeeded", round) } if len(createdIDs) != 1 { t.Fatalf("round %d: responses returned %d different message ids, want 1: %v", round, len(createdIDs), createdIDs) } if got := countMessagesWithText(t, bob, chat.ID, text); got != 1 { t.Fatalf("round %d: %d messages with text %q in history, want exactly 1", round, got, text) } } if len(instances) < 2 { t.Fatalf("requests were served by %d replica(s) %v, need at least 2", len(instances), instances) } t.Logf("served by replicas: %v", instances) } func countMessagesWithText(t *testing.T, actor testUser, chatID uuid.UUID, text string) int { t.Helper() count := 0 path := "/api/v1/chats/" + chatID.String() + "/messages?limit=100" for page := 0; page < 100; page++ { resp := doRequest(t, http.MethodGet, path, actor.Token, nil) requireStatus(t, resp, http.StatusOK) var body struct { Items []struct { Text string `json:"text"` } `json:"items"` NextCursor *string `json:"next_cursor"` } mustDecode(t, resp, &body) for _, m := range body.Items { if m.Text == text { count++ } } if body.NextCursor == nil { return count } path = "/api/v1/chats/" + chatID.String() + "/messages?limit=100&cursor=" + url.QueryEscape(*body.NextCursor) } t.Fatalf("history pagination exceeded safety bound") return 0 }