retry_test.go112 lines, 3.4 KB
| 1 | package jobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/fiston-user/murmur/internal/platform/testdb" |
| 10 | "github.com/jackc/pgx/v5/pgxpool" |
| 11 | ) |
| 12 | |
| 13 | // queue is a worker on an empty test database, one job that fails for as long as |
| 14 | // *fails is true, and a clock that stands still until the test moves it. |
| 15 | func queue(t *testing.T) (w *Worker, db *pgxpool.Pool, clock *time.Time, fails *bool) { |
| 16 | db = testdb.New(t) |
| 17 | if err := Enqueue(context.Background(), db, "flaky", 1); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | clock, fails = new(time.Now()), new(true) |
| 21 | w = NewWorker(db) |
| 22 | w.now = func() time.Time { return *clock } |
| 23 | w.Handle("flaky", func(context.Context, []byte) error { |
| 24 | if *fails { |
| 25 | return errors.New("not this time") |
| 26 | } |
| 27 | return nil |
| 28 | }) |
| 29 | return w, db, clock, fails |
| 30 | } |
| 31 | |
| 32 | // A job that fails is tried again later, and not before. |
| 33 | func TestFailedJobWaits(t *testing.T) { |
| 34 | w, _, clock, fails := queue(t) |
| 35 | ctx := context.Background() |
| 36 | |
| 37 | if ok, err := w.RunOne(ctx); !ok || err == nil { |
| 38 | t.Fatalf("first try: ran = %v, err = %v, want a job and an error", ok, err) |
| 39 | } |
| 40 | *fails = false |
| 41 | if ok, _ := w.RunOne(ctx); ok { |
| 42 | t.Fatal("the job was tried again at once, want a wait") |
| 43 | } |
| 44 | *clock = clock.Add(baseWait) |
| 45 | if ok, err := w.RunOne(ctx); !ok || err != nil { |
| 46 | t.Fatalf("after the wait: ran = %v, err = %v, want a job and no error", ok, err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // A job that fails every time is dead after the last attempt, with its error. |
| 51 | func TestDeadAfterLastAttempt(t *testing.T) { |
| 52 | w, db, clock, _ := queue(t) |
| 53 | ctx := context.Background() |
| 54 | |
| 55 | for range MaxAttempts { |
| 56 | if ok, err := w.RunOne(ctx); !ok || err == nil { |
| 57 | t.Fatalf("ran = %v, err = %v, want a job and an error", ok, err) |
| 58 | } |
| 59 | *clock = clock.Add(maxWait) |
| 60 | } |
| 61 | if ok, _ := w.RunOne(ctx); ok { |
| 62 | t.Fatal("a dead job was tried again") |
| 63 | } |
| 64 | var status, lastError string |
| 65 | err := db.QueryRow(ctx, `select status, last_error from jobs`).Scan(&status, &lastError) |
| 66 | if err != nil || status != "dead" || lastError != "not this time" { |
| 67 | t.Fatalf("status = %q, last error = %q, err = %v, want a dead job", status, lastError, err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // A worker that dies with a job in its hands leaves it running. The sweep finds it. |
| 72 | func TestStuckJobIsSwept(t *testing.T) { |
| 73 | w, _, clock, fails := queue(t) |
| 74 | ctx := context.Background() |
| 75 | *fails = false |
| 76 | |
| 77 | // The worker claims the job, and that's the last we hear of it. |
| 78 | if _, err := w.claim(ctx); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | if n, err := w.Sweep(ctx, time.Minute); n != 0 || err != nil { |
| 82 | t.Fatalf("too early: swept = %d, err = %v, want 0", n, err) |
| 83 | } |
| 84 | *clock = clock.Add(2 * time.Minute) |
| 85 | if n, err := w.Sweep(ctx, time.Minute); n != 1 || err != nil { |
| 86 | t.Fatalf("after the timeout: swept = %d, err = %v, want 1", n, err) |
| 87 | } |
| 88 | if ok, err := w.RunOne(ctx); !ok || err != nil { |
| 89 | t.Fatalf("after the sweep: ran = %v, err = %v, want a job and no error", ok, err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // A job that takes its worker down every time is dead after the last attempt, like any other. |
| 94 | func TestSweptJobDies(t *testing.T) { |
| 95 | w, db, clock, _ := queue(t) |
| 96 | ctx := context.Background() |
| 97 | |
| 98 | for range MaxAttempts { |
| 99 | if _, err := w.claim(ctx); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | *clock = clock.Add(2 * time.Minute) |
| 103 | if n, err := w.Sweep(ctx, time.Minute); n != 1 || err != nil { |
| 104 | t.Fatalf("swept = %d, err = %v, want 1", n, err) |
| 105 | } |
| 106 | } |
| 107 | var status string |
| 108 | err := db.QueryRow(ctx, `select status from jobs`).Scan(&status) |
| 109 | if err != nil || status != "dead" { |
| 110 | t.Fatalf("status = %q, err = %v, want a dead job", status, err) |
| 111 | } |
| 112 | } |