run_test.go98 lines, 2.6 KB
| 1 | package jobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/fiston-user/murmur/internal/platform/testdb" |
| 9 | ) |
| 10 | |
| 11 | // A job that waited in the queue leaves its wait in the tally, once. |
| 12 | func TestWaitIsCounted(t *testing.T) { |
| 13 | w, _, clock, fails := queue(t) |
| 14 | ctx := context.Background() |
| 15 | *fails = false |
| 16 | |
| 17 | *clock = clock.Add(90 * time.Second) |
| 18 | if ok, err := w.RunOne(ctx); !ok || err != nil { |
| 19 | t.Fatalf("ran = %v, err = %v, want a job and no error", ok, err) |
| 20 | } |
| 21 | waits := w.done.take() |
| 22 | if len(waits) != 1 || waits[0].Round(time.Second) != 90*time.Second { |
| 23 | t.Fatalf("waits = %v, want one wait of 1m30s", waits) |
| 24 | } |
| 25 | if again := w.done.take(); len(again) != 0 { |
| 26 | t.Fatalf("a second take = %v, want nothing", again) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Run is cancelled while a job is in hand. It returns only when that job is finished. |
| 31 | func TestRunFinishesTheJobInHand(t *testing.T) { |
| 32 | db := testdb.New(t) |
| 33 | ctx, cancel := context.WithCancel(context.Background()) |
| 34 | defer cancel() |
| 35 | if err := Enqueue(ctx, db, "slow", 1); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | started, finish := make(chan struct{}), make(chan struct{}) |
| 39 | w := NewWorker(db) |
| 40 | w.Handle("slow", func(context.Context, []byte) error { |
| 41 | close(started) |
| 42 | <-finish |
| 43 | return nil |
| 44 | }) |
| 45 | stopped := make(chan struct{}) |
| 46 | go func() { |
| 47 | w.Run(ctx, 1, time.Millisecond, time.Minute, time.Minute) |
| 48 | close(stopped) |
| 49 | }() |
| 50 | |
| 51 | <-started |
| 52 | cancel() |
| 53 | select { |
| 54 | case <-stopped: |
| 55 | t.Fatal("Run returned with a job in hand") |
| 56 | default: |
| 57 | } |
| 58 | close(finish) |
| 59 | <-stopped |
| 60 | |
| 61 | var left int |
| 62 | err := db.QueryRow(context.Background(), `select count(*) from jobs`).Scan(&left) |
| 63 | if err != nil || left != 0 { |
| 64 | t.Fatalf("jobs left = %d, err = %v, want an empty queue", left, err) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // A job that doesn't finish within the grace period is cut off, and it stays running. |
| 69 | // To the queue, its worker has gone away, and a sweep will find the job. |
| 70 | func TestRunCutsOffAfterGrace(t *testing.T) { |
| 71 | db := testdb.New(t) |
| 72 | ctx, cancel := context.WithCancel(context.Background()) |
| 73 | defer cancel() |
| 74 | if err := Enqueue(ctx, db, "endless", 1); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | started := make(chan struct{}) |
| 78 | w := NewWorker(db) |
| 79 | w.Handle("endless", func(ctx context.Context, _ []byte) error { |
| 80 | close(started) |
| 81 | <-ctx.Done() |
| 82 | return ctx.Err() |
| 83 | }) |
| 84 | stopped := make(chan struct{}) |
| 85 | go func() { |
| 86 | w.Run(ctx, 1, time.Millisecond, time.Minute, time.Millisecond) |
| 87 | close(stopped) |
| 88 | }() |
| 89 | |
| 90 | <-started |
| 91 | cancel() |
| 92 | <-stopped |
| 93 | var status string |
| 94 | err := db.QueryRow(context.Background(), `select status from jobs`).Scan(&status) |
| 95 | if err != nil || status != "running" { |
| 96 | t.Fatalf("status = %q, err = %v, want a job left running for the sweep", status, err) |
| 97 | } |
| 98 | } |