idempotency_test.go96 lines, 3.2 KB
| 1 | package idempotency_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/fiston-user/payments/internal/platform/idempotency" |
| 11 | "github.com/fiston-user/payments/internal/platform/postgres" |
| 12 | "github.com/google/uuid" |
| 13 | "github.com/jackc/pgx/v5" |
| 14 | ) |
| 15 | |
| 16 | // newStore uses the wallet's database: every service's database has the |
| 17 | // idempotency_keys table. |
| 18 | func newStore(t *testing.T) *idempotency.Store { |
| 19 | t.Helper() |
| 20 | if testing.Short() { |
| 21 | t.Skip("integration test") |
| 22 | } |
| 23 | pool, err := postgres.NewPool(t.Context(), postgresURL, 4) |
| 24 | if err != nil { |
| 25 | t.Fatalf("connect: %v", err) |
| 26 | } |
| 27 | t.Cleanup(pool.Close) |
| 28 | return idempotency.NewStore(pool, nil) |
| 29 | } |
| 30 | |
| 31 | // newKey returns the key value, sent by caller with a request to move amount. |
| 32 | func newKey(caller, value string, amount int) idempotency.Key { |
| 33 | // A map of ints always encodes, so there's no error to check. |
| 34 | fp, _ := idempotency.Fingerprint("POST", "/v1/transfers", map[string]int{"amount": amount}) |
| 35 | return idempotency.Key{Scope: caller, Value: value, Fingerprint: fp} |
| 36 | } |
| 37 | |
| 38 | func TestRun(t *testing.T) { |
| 39 | t.Parallel() |
| 40 | s := newStore(t) |
| 41 | alice, bob := uuid.NewString(), uuid.NewString() // two callers, new every run |
| 42 | |
| 43 | // Fake work: runs counts how often work really ran, and each response |
| 44 | // says which run produced it. |
| 45 | runs := 0 |
| 46 | created := func(pgx.Tx) (idempotency.Response, error) { |
| 47 | runs++ |
| 48 | return idempotency.JSON(http.StatusCreated, map[string]int{"run": runs}) |
| 49 | } |
| 50 | errInvalid := errors.New("invalid input") |
| 51 | invalid := func(pgx.Tx) (idempotency.Response, error) { |
| 52 | runs++ |
| 53 | return idempotency.Response{}, errInvalid |
| 54 | } |
| 55 | // duplicate sends the same request again while its own work is still |
| 56 | // running. The duplicate must be refused, and never run. |
| 57 | duplicate := func(tx pgx.Tx) (idempotency.Response, error) { |
| 58 | _, err := s.Run(t.Context(), newKey(alice, "c", 500), created) |
| 59 | if !errors.Is(err, idempotency.ErrInProgress) { |
| 60 | return idempotency.Response{}, fmt.Errorf("duplicate: got %v, want ErrInProgress", err) |
| 61 | } |
| 62 | return created(tx) |
| 63 | } |
| 64 | |
| 65 | // The rows run in order, and share keys. |
| 66 | tests := []struct { |
| 67 | name string |
| 68 | caller string |
| 69 | key string |
| 70 | amount int |
| 71 | work func(pgx.Tx) (idempotency.Response, error) |
| 72 | err error |
| 73 | body string |
| 74 | replayed bool |
| 75 | }{ |
| 76 | {"runs the first time", alice, "a", 500, created, nil, `{"run":1}`, false}, |
| 77 | {"replays a retry", alice, "a", 500, created, nil, `{"run":1}`, true}, |
| 78 | {"refuses a different request", alice, "a", 900, created, idempotency.ErrKeyReused, "", false}, |
| 79 | {"keys are per caller", bob, "a", 500, created, nil, `{"run":2}`, false}, |
| 80 | {"a failure", alice, "b", 500, invalid, errInvalid, "", false}, |
| 81 | {"doesn't burn the key", alice, "b", 500, created, nil, `{"run":4}`, false}, |
| 82 | {"refuses a duplicate in progress", alice, "c", 500, duplicate, nil, `{"run":5}`, false}, |
| 83 | } |
| 84 | for _, tt := range tests { |
| 85 | t.Run(tt.name, func(t *testing.T) { |
| 86 | resp, err := s.Run(t.Context(), newKey(tt.caller, tt.key, tt.amount), tt.work) |
| 87 | if !errors.Is(err, tt.err) { |
| 88 | t.Fatalf("err = %v, want %v", err, tt.err) |
| 89 | } |
| 90 | body := strings.TrimSpace(string(resp.Body)) |
| 91 | if body != tt.body || resp.Replayed != tt.replayed { |
| 92 | t.Errorf("got %s replayed=%t, want %s replayed=%t", body, resp.Replayed, tt.body, tt.replayed) |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | } |