books_test.go88 lines, 2.6 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "math/rand/v2" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/fiston-user/payments/internal/wallet" |
| 9 | "github.com/google/uuid" |
| 10 | "github.com/jackc/pgx/v5" |
| 11 | "github.com/jackc/pgx/v5/pgxpool" |
| 12 | ) |
| 13 | |
| 14 | // problems lists what's wrong with the whole ledger, in one query, so in one |
| 15 | // snapshot: a currency whose debits don't equal its credits, a wallet below |
| 16 | // zero, or a holds account without exactly the money of the open holds. |
| 17 | const problems = ` |
| 18 | SELECT 'unbalanced: ' || currency FROM entries GROUP BY currency |
| 19 | HAVING sum(CASE direction WHEN 'debit' THEN amount ELSE -amount END) <> 0 |
| 20 | UNION ALL |
| 21 | SELECT 'overdrawn: ' || b.name FROM account_balances b |
| 22 | JOIN accounts a ON a.id = b.account_id |
| 23 | WHERE a.kind = 'wallet' AND b.balance < 0 |
| 24 | UNION ALL |
| 25 | SELECT 'holds account off: ' || b.currency FROM account_balances b |
| 26 | JOIN accounts a ON a.id = b.account_id |
| 27 | WHERE a.kind = 'system' AND a.name = 'holds' AND b.balance <> ( |
| 28 | SELECT coalesce(sum(amount), 0) FROM holds h |
| 29 | WHERE h.currency = b.currency AND h.status = 'held')` |
| 30 | |
| 31 | // checkBooks fails the test if the ledger has any problems. It runs when the |
| 32 | // test is over, and t.Context() is done, so it brings a context of its own. |
| 33 | func checkBooks(t *testing.T, pool *pgxpool.Pool) { |
| 34 | t.Helper() |
| 35 | rows, _ := pool.Query(context.Background(), problems) |
| 36 | found, err := pgx.CollectRows(rows, pgx.RowTo[string]) |
| 37 | if err != nil { |
| 38 | t.Fatalf("check the books: %v", err) |
| 39 | } |
| 40 | for _, p := range found { |
| 41 | t.Error(p) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestBooksBalanceUnderLoad(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | s := newStore(t) |
| 48 | svc := wallet.NewService(s) |
| 49 | wallets := make([]uuid.UUID, 5) |
| 50 | for i := range wallets { |
| 51 | wallets[i] = newWallet(t, svc, 10_000) |
| 52 | } |
| 53 | |
| 54 | // 300 payments at once between five wallets: half of them transfers, and |
| 55 | // half the saga's way, a hold that's then captured or released. |
| 56 | errs := concurrently(300, func() error { |
| 57 | i, j := rand.IntN(5), rand.IntN(4) |
| 58 | if j >= i { |
| 59 | j++ // any wallet but i |
| 60 | } |
| 61 | from, to, amount := wallets[i], wallets[j], usd(rand.Int64N(2000)+1) |
| 62 | if rand.IntN(2) == 0 { |
| 63 | _, err := svc.Transfer(t.Context(), from.String(), to.String(), amount) |
| 64 | return err |
| 65 | } |
| 66 | hold := uuid.New() |
| 67 | if _, err := svc.Hold(t.Context(), hold, from, amount); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | var err error |
| 71 | if rand.IntN(2) == 0 { |
| 72 | _, err = svc.Capture(t.Context(), hold, to) |
| 73 | } else { |
| 74 | _, err = svc.Release(t.Context(), hold) |
| 75 | } |
| 76 | return err |
| 77 | }) |
| 78 | ok, refused := tally(t, errs) |
| 79 | t.Logf("%d payments went through, %d refused for insufficient funds", ok, refused) |
| 80 | |
| 81 | var total int64 |
| 82 | for _, w := range wallets { |
| 83 | total += balance(t, s, w) |
| 84 | } |
| 85 | if total != 5*10_000 { |
| 86 | t.Errorf("the wallets hold %d, want %d", total, 5*10_000) |
| 87 | } |
| 88 | } |