forked from riverqueue/river
-
Notifications
You must be signed in to change notification settings - Fork 0
/
periodic_job_test.go
71 lines (53 loc) · 1.62 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
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) {
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)
})
}
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
}