Сделать отправку сообщения идемпотентной по Idempotency-Key с общей таблицей ключей и их чисткой
This commit is contained in:
@@ -18,6 +18,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/pagination"
|
||||
@@ -238,11 +240,16 @@ func (ts *tester) chat(token, title string, members ...string) chat.ChatResponse
|
||||
|
||||
func (ts *tester) message(token, chatID, text string) chat.MessageResponse {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", token, map[string]string{"text": text}, nil)
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", token, map[string]string{"text": text}, freshKey())
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[chat.MessageResponse](ts.t, res)
|
||||
}
|
||||
|
||||
// freshKey is the header of a first attempt: a key never used before.
|
||||
func freshKey() map[string]string {
|
||||
return map[string]string{"Idempotency-Key": uuid.NewString()}
|
||||
}
|
||||
|
||||
func (ts *tester) chats(token, query string) pagination.PageResponse[chat.ChatResponse] {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodGet, "/api/v1/chats"+query, token, nil, nil)
|
||||
@@ -418,7 +425,7 @@ func TestMessagesEndToEnd(t *testing.T) {
|
||||
t.Fatalf("page2 = %+v", page2)
|
||||
}
|
||||
|
||||
post := func(actor string, body any) response { return ts.do(http.MethodPost, path, actor, body, nil) }
|
||||
post := func(actor string, body any) response { return ts.do(http.MethodPost, path, actor, body, freshKey()) }
|
||||
ts.expect(post(alice.token, map[string]string{"text": ""}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": " "}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4001)}), http.StatusBadRequest, "invalid_request")
|
||||
@@ -480,7 +487,7 @@ func TestConcurrentSendsProduceExactlyUniqueMessages(t *testing.T) {
|
||||
for n, c := range clientsList {
|
||||
wg.Go(func() {
|
||||
for i := 0; i < perClient; i++ {
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", c.token, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, nil)
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", c.token, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, freshKey())
|
||||
if res.status != http.StatusCreated {
|
||||
t.Errorf("client %d: status %d body %s", n, res.status, res.raw)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user