From 7f94356d6485c63583fe6d4013262737030c2517 Mon Sep 17 00:00:00 2001 From: Brandur Date: Tue, 14 Nov 2023 18:35:35 -0800 Subject: [PATCH] Add missing integration-level job snooze test I noticed while trying to refactor something the other day that I had a problem around snoozing, but only the example snooze test was failing. Looking into it further, it turned out that it was because the snooze example was the only snooze test we had. Example tests aren't great because they do obnoxious things like swallow all output (making it harder to debug), but they also don't support some feature like `-count`, making it harder to debug intermittent problems. Here, add another client test for snooze. We use the new client test line that makes use of `JobArgsReflectKind` and `WorkFunc`, keeping the implementation clean and all local to the specific test case that needs it. And since snooze and cancel are quite similar, I added a test case for cancel as well. Cancel did have another test elsewhere, but I figure I might as well add one in this new line too to line up with the snooze test case. It should be a pretty fast test, so doesn't hurt to have a duplicate. --- client_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/client_test.go b/client_test.go index 7f6a4f23..10d0e6e5 100644 --- a/client_test.go +++ b/client_test.go @@ -168,7 +168,9 @@ func Test_Client(t *testing.T) { ctx := context.Background() - type testBundle struct{} + type testBundle struct { + subscribeChan <-chan *Event + } setup := func(t *testing.T) (*Client[pgx.Tx], *testBundle) { t.Helper() @@ -176,7 +178,16 @@ func Test_Client(t *testing.T) { config := newTestConfig(t, nil) client := newTestClient(ctx, t, config) - return client, &testBundle{} + subscribeChan, _ := client.Subscribe( + EventKindJobCancelled, + EventKindJobCompleted, + EventKindJobFailed, + EventKindJobSnoozed, + ) + + return client, &testBundle{ + subscribeChan: subscribeChan, + } } t.Run("StartInsertAndWork", func(t *testing.T) { @@ -203,6 +214,54 @@ func Test_Client(t *testing.T) { rivertest.WaitOrTimeout(t, workedChan) }) + t.Run("JobCancel", func(t *testing.T) { + t.Parallel() + + client, bundle := setup(t) + + type JobArgs struct { + JobArgsReflectKind[JobArgs] + } + + AddWorker(client.config.Workers, WorkFunc(func(ctx context.Context, job *Job[JobArgs]) error { + return JobCancel(fmt.Errorf("a persisted internal error")) + })) + + startClient(ctx, t, client) + + _, err := client.Insert(ctx, &JobArgs{}, nil) + require.NoError(t, err) + + event := rivertest.WaitOrTimeout(t, bundle.subscribeChan) + require.Equal(t, EventKindJobCancelled, event.Kind) + require.Equal(t, JobStateCancelled, event.Job.State) + require.WithinDuration(t, time.Now(), *event.Job.FinalizedAt, 2*time.Second) + }) + + t.Run("JobSnooze", func(t *testing.T) { + t.Parallel() + + client, bundle := setup(t) + + type JobArgs struct { + JobArgsReflectKind[JobArgs] + } + + AddWorker(client.config.Workers, WorkFunc(func(ctx context.Context, job *Job[JobArgs]) error { + return JobSnooze(15 * time.Minute) + })) + + startClient(ctx, t, client) + + _, err := client.Insert(ctx, &JobArgs{}, nil) + require.NoError(t, err) + + event := rivertest.WaitOrTimeout(t, bundle.subscribeChan) + require.Equal(t, EventKindJobSnoozed, event.Kind) + require.Equal(t, JobStateScheduled, event.Job.State) + require.WithinDuration(t, time.Now().Add(15*time.Minute), event.Job.ScheduledAt, 2*time.Second) + }) + t.Run("AlternateSchema", func(t *testing.T) { t.Parallel()