| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "sync" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgxpool" |
| 12 | ) |
| 13 | |
| 14 | func TestBackoff(t * |
| 15 | maxRand := func(n |
| 16 | |
| 17 | tests := []struct |
| 18 | attempt int |
| 19 | want time. |
| 20 | }{ |
| 21 | {0, 10 * time |
| 22 | {1, 20 * time |
| 23 | {2, 40 * time |
| 24 | {3, 80 * time |
| 25 | {8, 2560 * time |
| 26 | {9, time.Hour |
| 27 | {30, time.Hour |
| 28 | {100, time.Hour |
| 29 | } |
| 30 | for _, tt |
| 31 | got := backoff( |
| 32 | if got != tt |
| 33 | t.Errorf("backoff( |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Tests only touch rows whose idempotency_key starts with this prefix, |
| 39 | // so they are safe to run against a shared dev database. |
| 40 | const testKeyPrefix = "test: |
| 41 | |
| 42 | // testDB connects to TEST_DATABASE_URL, or skips the test when it is unset. |
| 43 | // It refuses to run if the queue holds real pending jobs, because claim |
| 44 | // would lease them. Cleanup deletes only the rows the test inserted. |
| 45 | func testDB(t * |
| 46 | t.Helper() |
| 47 | url := os. |
| 48 | if url == "" |
| 49 | t.Skip("TEST_DATABASE_URL is not set |
| 50 | } |
| 51 | db, err := |
| 52 | if err != nil |
| 53 | t.Fatal(err |
| 54 | } |
| 55 | |
| 56 | var real int |
| 57 | err = db. |
| 58 | `SELECT count(*) FROM delivery_job WHERE status = 'pending' AND idempotency_key NOT LIKE $1 || '%'`, |
| 59 | testKeyPrefix).Scan(& |
| 60 | if err != nil |
| 61 | t.Fatal(err |
| 62 | } |
| 63 | if real > 0 |
| 64 | t.Fatalf("%d |
| 65 | } |
| 66 | |
| 67 | t.Cleanup(func() |
| 68 | // t.Context() is already canceled when cleanup runs. |
| 69 | _, err := |
| 70 | `DELETE FROM delivery_job WHERE idempotency_key LIKE $1 || '%'`, testKeyPrefix) |
| 71 | if err != nil |
| 72 | t.Errorf("cleanup: |
| 73 | } |
| 74 | db.Close() |
| 75 | }) |
| 76 | return db |
| 77 | } |
| 78 | |
| 79 | // insertJobs adds n pending email jobs and returns their ids. |
| 80 | func insertJobs(t * |
| 81 | t.Helper() |
| 82 | ids := make( |
| 83 | for range n { |
| 84 | var id string |
| 85 | err := db. |
| 86 | `INSERT INTO delivery_job (kind, idempotency_key, payload) |
| 87 | VALUES ('email', $1 || gen_random_uuid()::text, '{}') |
| 88 | RETURNING id`, testKeyPrefix).Scan |
| 89 | if err != nil |
| 90 | t.Fatal(err |
| 91 | } |
| 92 | ids[id] = |
| 93 | } |
| 94 | return ids |
| 95 | } |
| 96 | |
| 97 | func TestClaimNeverHandsOutAJobTwice(t * |
| 98 | db := testDB( |
| 99 | inserted := insertJobs( |
| 100 | |
| 101 | var ( |
| 102 | mu sync. |
| 103 | claimed = map[string |
| 104 | wg sync. |
| 105 | ) |
| 106 | for range 4 { |
| 107 | wg.Go(func() |
| 108 | for { |
| 109 | jobs, err := |
| 110 | if err != nil |
| 111 | t.Error(err |
| 112 | return |
| 113 | } |
| 114 | if len(jobs) |
| 115 | return |
| 116 | } |
| 117 | mu.Lock() |
| 118 | for _, j |
| 119 | claimed[j.ID |
| 120 | } |
| 121 | mu.Unlock() |
| 122 | } |
| 123 | }) |
| 124 | } |
| 125 | wg.Wait() |
| 126 | |
| 127 | if len(claimed) |
| 128 | t.Errorf("claimed |
| 129 | } |
| 130 | for id, n |
| 131 | if n > 1 |
| 132 | t.Errorf("job |
| 133 | } |
| 134 | if !inserted[id |
| 135 | t.Errorf("claimed job |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestClaimReclaimsAfterLeaseExpires(t * |
| 141 | db := testDB( |
| 142 | insertJobs(t, db |
| 143 | |
| 144 | first, err := |
| 145 | if err != nil |
| 146 | t.Fatal(err |
| 147 | } |
| 148 | if len(first) |
| 149 | t.Fatalf("first claim got |
| 150 | } |
| 151 | |
| 152 | // Lease is held: nobody else may take it. |
| 153 | again, err := |
| 154 | if err != nil |
| 155 | t.Fatal(err |
| 156 | } |
| 157 | if len(again) |
| 158 | t.Fatalf("claimed |
| 159 | } |
| 160 | |
| 161 | // Simulate the worker dying: expire the lease instead of sleeping 60s. |
| 162 | _, err = |
| 163 | `UPDATE delivery_job SET locked_until = now() - interval '1 second' WHERE id = $1`, first[ |
| 164 | if err != nil |
| 165 | t.Fatal(err |
| 166 | } |
| 167 | |
| 168 | second, err := |
| 169 | if err != nil |
| 170 | t.Fatal(err |
| 171 | } |
| 172 | if len(second) |
| 173 | t.Fatalf("reclaim got |
| 174 | } |
| 175 | if second[0]. |
| 176 | t.Error("reclaim kept the old claim_token; a stale worker could overwrite the result |
| 177 | } |
| 178 | if second[0]. |
| 179 | t.Errorf("attempts = |
| 180 | } |
| 181 | |
| 182 | // The worker that lost its lease must not overwrite the new owner's result. |
| 183 | if err := complete |
| 184 | t.Errorf("complete with stale token = |
| 185 | } |
| 186 | if err := complete |
| 187 | t.Fatalf("complete with current token: |
| 188 | } |
| 189 | if got := jobStatus |
| 190 | t.Errorf("status = |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestFailSchedulesRetry(t * |
| 195 | db := testDB( |
| 196 | insertJobs(t, db |
| 197 | jobs, err := |
| 198 | if err != nil |
| 199 | t.Fatalf("claim = |
| 200 | } |
| 201 | |
| 202 | if err := fail |
| 203 | t.Fatal(err |
| 204 | } |
| 205 | |
| 206 | var ( |
| 207 | status, lastError string |
| 208 | delay time. |
| 209 | ) |
| 210 | err = db. |
| 211 | `SELECT status, last_error, next_attempt_at - now() FROM delivery_job WHERE id = $1`, |
| 212 | jobs[0].ID |
| 213 | if err != nil |
| 214 | t.Fatal(err |
| 215 | } |
| 216 | if status != " |
| 217 | t.Errorf("status = |
| 218 | } |
| 219 | if lastError != " |
| 220 | t.Errorf("last_error = |
| 221 | } |
| 222 | if delay < 29 |
| 223 | t.Errorf("next attempt in |
| 224 | } |
| 225 | |
| 226 | // Not due yet, so it must not be claimable. |
| 227 | again, err := |
| 228 | if err != nil |
| 229 | t.Fatal(err |
| 230 | } |
| 231 | if len(again) |
| 232 | t.Errorf("claimed |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestFailPastMaxAgeIsDead(t * |
| 237 | db := testDB( |
| 238 | _, err := |
| 239 | `INSERT INTO delivery_job (kind, idempotency_key, payload, created_at) |
| 240 | VALUES ('email', $1 || gen_random_uuid()::text, '{}', now() - interval '4 days')`, |
| 241 | testKeyPrefix) |
| 242 | if err != nil |
| 243 | t.Fatal(err |
| 244 | } |
| 245 | jobs, err := |
| 246 | if err != nil |
| 247 | t.Fatalf("claim = |
| 248 | } |
| 249 | |
| 250 | if err := fail |
| 251 | t.Fatal(err |
| 252 | } |
| 253 | if got := jobStatus |
| 254 | t.Errorf("status = |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | func jobStatus(t * |
| 259 | t.Helper() |
| 260 | var s string |
| 261 | if err := db |
| 262 | t.Fatal(err |
| 263 | } |
| 264 | return s |
| 265 | } |
| 266 | |
| 267 | func TestListenWakesOnNotify(t * |
| 268 | db := testDB( |
| 269 | wake := make( |
| 270 | go listen(t. |
| 271 | |
| 272 | waitWake := func(what |
| 273 | t.Helper() |
| 274 | select { |
| 275 | case <-wake: |
| 276 | case <-time.After |
| 277 | t.Fatalf("no wake-up for |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | waitWake("initial LISTEN") |
| 282 | if _, err |
| 283 | t.Fatal(err |
| 284 | } |
| 285 | waitWake("NOTIFY delivery") |
| 286 | } |