forked from riverqueue/river
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
152 lines (106 loc) · 3.48 KB
/
worker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package river
import (
"context"
"testing"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
"github.com/riverqueue/river/internal/riverinternaltest"
"github.com/riverqueue/river/rivershared/riversharedtest"
)
func TestWork(t *testing.T) {
t.Parallel()
workers := NewWorkers()
AddWorker(workers, &noOpWorker{})
require.Contains(t, workers.workersMap, (noOpArgs{}).Kind())
require.PanicsWithError(t, `worker for kind "noOp" is already registered`, func() {
AddWorker(workers, &noOpWorker{})
})
fn := func(ctx context.Context, job *Job[callbackArgs]) error { return nil }
ch := callbackWorker{fn: fn}
// function worker
AddWorker(workers, &ch)
require.Contains(t, workers.workersMap, (callbackArgs{}).Kind())
}
type configurableArgs struct {
uniqueOpts UniqueOpts
}
func (a configurableArgs) Kind() string { return "configurable" }
func (a configurableArgs) InsertOpts() InsertOpts {
return InsertOpts{UniqueOpts: a.uniqueOpts}
}
type configurableWorker struct {
WorkerDefaults[configurableArgs]
}
func (w *configurableWorker) Work(ctx context.Context, job *Job[configurableArgs]) error {
return nil
}
func TestWorkers_add(t *testing.T) {
t.Parallel()
workers := NewWorkers()
err := workers.add(noOpArgs{}, &workUnitFactoryWrapper[noOpArgs]{worker: &noOpWorker{}})
require.NoError(t, err)
// Different worker kind.
err = workers.add(configurableArgs{}, &workUnitFactoryWrapper[configurableArgs]{worker: &configurableWorker{}})
require.NoError(t, err)
err = workers.add(noOpArgs{}, &workUnitFactoryWrapper[noOpArgs]{worker: &noOpWorker{}})
require.EqualError(t, err, `worker for kind "noOp" is already registered`)
}
type WorkFuncArgs struct{}
func (WorkFuncArgs) Kind() string { return "work_func" }
type StructWithFunc struct {
WorkChan chan struct{}
}
func (s *StructWithFunc) Work(ctx context.Context, job *Job[WorkFuncArgs]) error {
s.WorkChan <- struct{}{}
return nil
}
func TestWorkFunc(t *testing.T) {
t.Parallel()
ctx := context.Background()
type testBundle struct{}
setup := func(t *testing.T) (*Client[pgx.Tx], *testBundle) {
t.Helper()
dbPool := riverinternaltest.TestDB(ctx, t)
client := newTestClient(t, dbPool, newTestConfig(t, nil))
startClient(ctx, t, client)
return client, &testBundle{}
}
t.Run("RoundTrip", func(t *testing.T) {
t.Parallel()
client, _ := setup(t)
workChan := make(chan struct{})
AddWorker(client.config.Workers, WorkFunc(func(ctx context.Context, job *Job[WorkFuncArgs]) error {
workChan <- struct{}{}
return nil
}))
_, err := client.Insert(ctx, &WorkFuncArgs{}, nil)
require.NoError(t, err)
riversharedtest.WaitOrTimeout(t, workChan)
})
t.Run("StructFunction", func(t *testing.T) {
t.Parallel()
client, _ := setup(t)
structWithFunc := &StructWithFunc{
WorkChan: make(chan struct{}),
}
AddWorker(client.config.Workers, WorkFunc(structWithFunc.Work))
_, err := client.Insert(ctx, &WorkFuncArgs{}, nil)
require.NoError(t, err)
riversharedtest.WaitOrTimeout(t, structWithFunc.WorkChan)
})
t.Run("JobArgsReflectKind", func(t *testing.T) {
t.Parallel()
client, _ := setup(t)
type InFuncWorkFuncArgs struct {
JobArgsReflectKind[InFuncWorkFuncArgs]
}
workChan := make(chan struct{})
AddWorker(client.config.Workers, WorkFunc(func(ctx context.Context, job *Job[InFuncWorkFuncArgs]) error {
workChan <- struct{}{}
return nil
}))
_, err := client.Insert(ctx, &InFuncWorkFuncArgs{}, nil)
require.NoError(t, err)
riversharedtest.WaitOrTimeout(t, workChan)
})
}