| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "sync" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/jackc/pgx/v5/pgxpool" |
| 11 | ) |
| 12 | |
| 13 | func TestBackoff(t * |
| 14 | maxRand := func(n |
| 15 | |
| 16 | tests := []struct |
| 17 | attempt int |
| 18 | want time. |
| 19 | }{ |
| 20 | {0, 10 * time |
| 21 | {1, 20 * time |
| 22 | {2, 40 * time |
| 23 | {3, 80 * time |
| 24 | {8, 2560 * time |
| 25 | {9, time.Hour |
| 26 | {30, time.Hour |
| 27 | {100, time.Hour |
| 28 | } |
| 29 | for _, tt |
| 30 | got := backoff( |
| 31 | if got != tt |
| 32 | t.Errorf("backoff( |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Tests only touch rows whose idempotency_key starts with this prefix, |
| 38 | // so they are safe to run against a shared dev database. |
| 39 | const testKeyPrefix = "test: |
| 40 | |
| 41 | // testDB connects to TEST_DATABASE_URL, or skips the test when it is unset. |
| 42 | // It refuses to run if the queue holds real pending jobs, because claim |
| 43 | // would lease them. Cleanup deletes only the rows the test inserted. |
| 44 | func testDB(t * |
| 45 | t.Helper() |
| 46 | url := os. |
| 47 | if url == "" |
| 48 | t.Skip("TEST_DATABASE_URL is not set |
| 49 | } |
| 50 | db, err := |
| 51 | if err != nil |
| 52 | t.Fatal(err |
| 53 | } |
| 54 | |
| 55 | var real int |
| 56 | err = db. |
| 57 | `SELECT count(*) FROM delivery_job WHERE status = 'pending' AND idempotency_key NOT LIKE $1 || '%'`, |
| 58 | testKeyPrefix).Scan(& |
| 59 | if err != nil |
| 60 | t.Fatal(err |
| 61 | } |
| 62 | if real > 0 |
| 63 | t.Fatalf("%d |
| 64 | } |
| 65 | |
| 66 | t.Cleanup(func() |
| 67 | // t.Context() is already canceled when cleanup runs. |
| 68 | _, err := |
| 69 | `DELETE FROM delivery_job WHERE idempotency_key LIKE $1 || '%'`, testKeyPrefix) |
| 70 | if err != nil |
| 71 | t.Errorf("cleanup: |
| 72 | } |
| 73 | db.Close() |
| 74 | }) |
| 75 | return db |
| 76 | } |
| 77 | |
| 78 | // insertJobs adds n pending email jobs and returns their ids. |
| 79 | func insertJobs(t * |
| 80 | t.Helper() |
| 81 | ids := make( |
| 82 | for range n { |
| 83 | var id string |
| 84 | err := db. |
| 85 | `INSERT INTO delivery_job (kind, idempotency_key, payload) |
| 86 | VALUES ('email', $1 || gen_random_uuid()::text, '{}') |
| 87 | RETURNING id`, testKeyPrefix).Scan |
| 88 | if err != nil |
| 89 | t.Fatal(err |
| 90 | } |
| 91 | ids[id] = |
| 92 | } |
| 93 | return ids |
| 94 | } |
| 95 | |
| 96 | func TestClaimNeverHandsOutAJobTwice(t * |
| 97 | db := testDB( |
| 98 | inserted := insertJobs( |
| 99 | |
| 100 | var ( |
| 101 | mu sync. |
| 102 | claimed = map[string |
| 103 | wg sync. |
| 104 | ) |
| 105 | for range 4 { |
| 106 | wg.Go(func() |
| 107 | for { |
| 108 | jobs, err := |
| 109 | if err != nil |
| 110 | t.Error(err |
| 111 | return |
| 112 | } |
| 113 | if len(jobs) |
| 114 | return |
| 115 | } |
| 116 | mu.Lock() |
| 117 | for _, j |
| 118 | claimed[j.ID |
| 119 | } |
| 120 | mu.Unlock() |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | wg.Wait() |
| 125 | |
| 126 | if len(claimed) |
| 127 | t.Errorf("claimed |
| 128 | } |
| 129 | for id, n |
| 130 | if n > 1 |
| 131 | t.Errorf("job |
| 132 | } |
| 133 | if !inserted[id |
| 134 | t.Errorf("claimed job |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestClaimReclaimsAfterLeaseExpires(t * |
| 140 | db := testDB( |
| 141 | insertJobs(t, db |
| 142 | |
| 143 | first, err := |
| 144 | if err != nil |
| 145 | t.Fatal(err |
| 146 | } |
| 147 | if len(first) |
| 148 | t.Fatalf("first claim got |
| 149 | } |
| 150 | |
| 151 | // Lease is held: nobody else may take it. |
| 152 | again, err := |
| 153 | if err != nil |
| 154 | t.Fatal(err |
| 155 | } |
| 156 | if len(again) |
| 157 | t.Fatalf("claimed |
| 158 | } |
| 159 | |
| 160 | // Simulate the worker dying: expire the lease instead of sleeping 60s. |
| 161 | _, err = |
| 162 | `UPDATE delivery_job SET locked_until = now() - interval '1 second' WHERE id = $1`, first[ |
| 163 | if err != nil |
| 164 | t.Fatal(err |
| 165 | } |
| 166 | |
| 167 | second, err := |
| 168 | if err != nil |
| 169 | t.Fatal(err |
| 170 | } |
| 171 | if len(second) |
| 172 | t.Fatalf("reclaim got |
| 173 | } |
| 174 | if second[0]. |
| 175 | t.Error("reclaim kept the old claim_token; a stale worker could overwrite the result |
| 176 | } |
| 177 | if second[0]. |
| 178 | t.Errorf("attempts = |
| 179 | } |
| 180 | } |