transfer_test.go127 lines, 3.2 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "math/rand/v2" |
| 6 | "sync" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/fiston-user/payments/internal/wallet" |
| 10 | "github.com/google/uuid" |
| 11 | ) |
| 12 | |
| 13 | // usd is an amount of US cents. |
| 14 | func usd(cents int64) wallet.Money { |
| 15 | return wallet.Money{Amount: cents, Currency: "USD"} |
| 16 | } |
| 17 | |
| 18 | // newWallet opens a USD wallet for a new owner, and deposits amount into it. |
| 19 | func newWallet(t *testing.T, svc *wallet.Service, amount int64) uuid.UUID { |
| 20 | t.Helper() |
| 21 | w, err := svc.CreateWallet(t.Context(), uuid.NewString(), "USD") |
| 22 | if err != nil { |
| 23 | t.Fatalf("create wallet: %v", err) |
| 24 | } |
| 25 | if amount > 0 { |
| 26 | if _, err := svc.Deposit(t.Context(), w.ID, usd(amount)); err != nil { |
| 27 | t.Fatalf("deposit: %v", err) |
| 28 | } |
| 29 | } |
| 30 | return w.ID |
| 31 | } |
| 32 | |
| 33 | // concurrently calls fn n times from n goroutines, released at the same |
| 34 | // moment, and returns each call's error. |
| 35 | func concurrently(n int, fn func() error) []error { |
| 36 | errs := make([]error, n) |
| 37 | start := make(chan struct{}) |
| 38 | var wg sync.WaitGroup |
| 39 | for i := range n { |
| 40 | wg.Go(func() { |
| 41 | <-start |
| 42 | errs[i] = fn() |
| 43 | }) |
| 44 | } |
| 45 | close(start) // the starting gun: every goroutine is waiting on it |
| 46 | wg.Wait() |
| 47 | return errs |
| 48 | } |
| 49 | |
| 50 | // tally counts the transfers that went through, and the ones refused for |
| 51 | // insufficient funds. Any other error fails the test. |
| 52 | func tally(t *testing.T, errs []error) (ok, refused int) { |
| 53 | t.Helper() |
| 54 | var failed []error |
| 55 | for _, err := range errs { |
| 56 | switch { |
| 57 | case err == nil: |
| 58 | ok++ |
| 59 | case errors.Is(err, wallet.ErrInsufficientFunds): |
| 60 | refused++ |
| 61 | default: |
| 62 | failed = append(failed, err) |
| 63 | } |
| 64 | } |
| 65 | if len(failed) > 0 { |
| 66 | t.Errorf("%d of %d transfers failed, the first with: %v", len(failed), len(errs), failed[0]) |
| 67 | } |
| 68 | return ok, refused |
| 69 | } |
| 70 | |
| 71 | func TestTransferCannotOverdraw(t *testing.T) { |
| 72 | t.Parallel() |
| 73 | s := newStore(t) |
| 74 | svc := wallet.NewService(s) |
| 75 | alice, bob := newWallet(t, svc, 1000), newWallet(t, svc, 0) |
| 76 | |
| 77 | // Ten transfers of $5 out of a $10 wallet, all at once: two can succeed. |
| 78 | errs := concurrently(10, func() error { |
| 79 | _, err := svc.Transfer(t.Context(), alice.String(), bob.String(), usd(500)) |
| 80 | return err |
| 81 | }) |
| 82 | if ok, _ := tally(t, errs); ok != 2 { |
| 83 | t.Errorf("%d transfers succeeded, want 2", ok) |
| 84 | } |
| 85 | if got := balance(t, s, alice); got != 0 { |
| 86 | t.Errorf("alice's balance = %d, want 0", got) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestConcurrentTransfersConserveMoney(t *testing.T) { |
| 91 | t.Parallel() |
| 92 | s := newStore(t) |
| 93 | svc := wallet.NewService(s) |
| 94 | |
| 95 | // Four wallets with $100 each. |
| 96 | const start = 10_000 |
| 97 | wallets := make([]uuid.UUID, 4) |
| 98 | for i := range wallets { |
| 99 | wallets[i] = newWallet(t, svc, start) |
| 100 | } |
| 101 | |
| 102 | // 200 transfers of up to $50, between two random wallets, all at once. |
| 103 | errs := concurrently(200, func() error { |
| 104 | i, j := rand.IntN(4), rand.IntN(3) |
| 105 | if j >= i { |
| 106 | j++ // any wallet but i |
| 107 | } |
| 108 | amount := usd(rand.Int64N(5000) + 1) |
| 109 | _, err := svc.Transfer(t.Context(), wallets[i].String(), wallets[j].String(), amount) |
| 110 | return err |
| 111 | }) |
| 112 | ok, refused := tally(t, errs) |
| 113 | t.Logf("%d transfers went through, %d refused for insufficient funds", ok, refused) |
| 114 | |
| 115 | // Money moved around, but none was created or lost, and no wallet went negative. |
| 116 | var total int64 |
| 117 | for _, w := range wallets { |
| 118 | b := balance(t, s, w) |
| 119 | if b < 0 { |
| 120 | t.Errorf("wallet %s has a negative balance: %d", w, b) |
| 121 | } |
| 122 | total += b |
| 123 | } |
| 124 | if total != 4*start { |
| 125 | t.Errorf("total = %d, want %d", total, 4*start) |
| 126 | } |
| 127 | } |