-
Notifications
You must be signed in to change notification settings - Fork 92
/
periodic_job_test.go
91 lines (68 loc) · 2.14 KB
/
periodic_job_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
package river
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/riverqueue/river/internal/maintenance"
"github.com/riverqueue/river/rivershared/riversharedtest"
)
func TestPeriodicJobBundle(t *testing.T) {
t.Parallel()
type testBundle struct{}
setup := func(t *testing.T) (*PeriodicJobBundle, *testBundle) { //nolint:unparam
t.Helper()
periodicJobEnqueuer := maintenance.NewPeriodicJobEnqueuer(
riversharedtest.BaseServiceArchetype(t),
&maintenance.PeriodicJobEnqueuerConfig{},
nil,
)
return newPeriodicJobBundle(newTestConfig(t, nil), periodicJobEnqueuer), &testBundle{}
}
t.Run("ConstructorFuncGeneratesNewArgsOnEachCall", func(t *testing.T) {
t.Parallel()
periodicJobBundle, _ := setup(t)
type TestJobArgs struct {
JobArgsReflectKind[TestJobArgs]
JobNum int `json:"job_num"`
}
var jobNum int
periodicJob := NewPeriodicJob(
PeriodicInterval(15*time.Minute),
func() (JobArgs, *InsertOpts) {
jobNum++
return TestJobArgs{JobNum: jobNum}, nil
},
nil,
)
internalPeriodicJob := periodicJobBundle.toInternal(periodicJob)
insertParams1, err := internalPeriodicJob.ConstructorFunc()
require.NoError(t, err)
require.Equal(t, 1, mustUnmarshalJSON[TestJobArgs](t, insertParams1.EncodedArgs).JobNum)
insertParams2, err := internalPeriodicJob.ConstructorFunc()
require.NoError(t, err)
require.Equal(t, 2, mustUnmarshalJSON[TestJobArgs](t, insertParams2.EncodedArgs).JobNum)
})
t.Run("ReturningNilDoesntInsertNewJob", func(t *testing.T) {
t.Parallel()
periodicJobBundle, _ := setup(t)
periodicJob := NewPeriodicJob(
PeriodicInterval(15*time.Minute),
func() (JobArgs, *InsertOpts) {
// Returning nil from the constructor function should not insert a new job.
return nil, nil
},
nil,
)
internalPeriodicJob := periodicJobBundle.toInternal(periodicJob)
_, err := internalPeriodicJob.ConstructorFunc()
require.ErrorIs(t, err, maintenance.ErrNoJobToInsert)
})
}
func mustUnmarshalJSON[T any](t *testing.T, data []byte) *T {
t.Helper()
var val T
err := json.Unmarshal(data, &val)
require.NoError(t, err)
return &val
}