MCPcopy Index your code
hub / github.com/riverqueue/river / TestWorker_Work

Function TestWorker_Work

rivertest/worker_test.go:73–481  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

71}
72
73func TestWorker_Work(t *testing.T) {
74 t.Parallel()
75
76 ctx := context.Background()
77
78 type testBundle struct {
79 config *river.Config
80 driver *riverpgxv5.Driver
81 tx pgx.Tx
82 }
83
84 setup := func(t *testing.T) *testBundle {
85 t.Helper()
86
87 var (
88 config = &river.Config{ID: "rivertest-worker"}
89 driver = riverpgxv5.New(nil)
90 tx = riverdbtest.TestTxPgx(ctx, t)
91 )
92
93 return &testBundle{
94 config: config,
95 driver: driver,
96 tx: tx,
97 }
98 }
99
100 t.Run("WorkASimpleJob", func(t *testing.T) {
101 t.Parallel()
102
103 bundle := setup(t)
104
105 worker := river.WorkFunc(func(ctx context.Context, job *river.Job[testArgs]) error {
106 require.Equal(t, testArgs{Value: "test"}, job.Args)
107 require.Equal(t, 1, job.Attempt)
108 require.NotNil(t, job.AttemptedAt)
109 require.WithinDuration(t, time.Now(), *job.AttemptedAt, 5*time.Second)
110 require.Equal(t, []string{"rivertest-worker"}, job.AttemptedBy)
111 require.WithinDuration(t, time.Now(), job.CreatedAt, 5*time.Second)
112 require.JSONEq(t, `{"value": "test"}`, string(job.EncodedArgs))
113 require.Empty(t, job.Errors)
114 require.Nil(t, job.FinalizedAt)
115 require.Positive(t, job.ID)
116 require.Equal(t, "rivertest_work_test", job.Kind)
117 require.Equal(t, river.MaxAttemptsDefault, job.MaxAttempts)
118 require.Equal(t, []byte(`{}`), job.Metadata)
119 require.Equal(t, river.PriorityDefault, job.Priority)
120 require.Equal(t, river.QueueDefault, job.Queue)
121 require.WithinDuration(t, time.Now(), job.ScheduledAt, 2*time.Second)
122 require.Equal(t, rivertype.JobStateRunning, job.State)
123 require.Equal(t, []string{}, job.Tags)
124 require.Nil(t, job.UniqueKey)
125
126 _, hasContextKeyInsideTestWorker := ctx.Value(execution.ContextKeyInsideTestWorker{}).(bool)
127 require.True(t, hasContextKeyInsideTestWorker)
128
129 return nil
130 })

Callers

nothing calls this directly

Calls 15

WorkMethod · 0.95
StubNowMethod · 0.95
NewFunction · 0.92
TestTxPgxFunction · 0.92
WorkFuncFunction · 0.92
JobSnoozeFunction · 0.92
JobCancelFunction · 0.92
HookWorkBeginFuncFuncType · 0.92
WorkerMiddlewareFuncFuncType · 0.92
DBPoolFunction · 0.92
TestSchemaFunction · 0.92
NewWorkerFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…