-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathperiodic_job_test.go
More file actions
153 lines (114 loc) · 3.89 KB
/
periodic_job_test.go
File metadata and controls
153 lines (114 loc) · 3.89 KB
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
153
package river
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/riverqueue/river/internal/maintenance"
"github.com/riverqueue/river/rivershared/riversharedtest"
"github.com/riverqueue/river/rivershared/util/testutil"
)
func TestNeverSchedule(t *testing.T) {
t.Parallel()
t.Run("NextReturnsMaximumTime", func(t *testing.T) {
t.Parallel()
schedule := NeverSchedule()
now := time.Now()
next := schedule.Next(now)
require.Equal(t, time.Unix(1<<63-62135596801, 999999999), next)
require.False(t, next.Before(now))
// use an arbitrary duration to check that
// the next schedule is far in the future
require.Greater(t, next.Year()-now.Year(), 1000)
})
}
func TestPeriodicJobBundle(t *testing.T) {
t.Parallel()
type testBundle struct{}
setup := func(t *testing.T) (*PeriodicJobBundle, *testBundle) { //nolint:unparam
t.Helper()
periodicJobEnqueuer, err := maintenance.NewPeriodicJobEnqueuer(
riversharedtest.BaseServiceArchetype(t),
&maintenance.PeriodicJobEnqueuerConfig{},
nil,
)
require.NoError(t, err)
return newPeriodicJobBundle(newTestConfig(t, ""), periodicJobEnqueuer), &testBundle{}
}
t.Run("ConstructorFuncGeneratesNewArgsOnEachCall", func(t *testing.T) {
t.Parallel()
periodicJobBundle, _ := setup(t)
type TestJobArgs struct {
testutil.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.mapper.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.mapper.toInternal(periodicJob)
_, err := internalPeriodicJob.ConstructorFunc()
require.ErrorIs(t, err, maintenance.ErrNoJobToInsert)
})
t.Run("AddError", func(t *testing.T) {
t.Parallel()
periodicJobBundle, _ := setup(t)
periodicJob := NewPeriodicJob(
PeriodicInterval(15*time.Minute),
func() (JobArgs, *InsertOpts) { return nil, nil },
&PeriodicJobOpts{ID: "periodic_job_id"},
)
periodicJobBundle.Add(periodicJob)
require.PanicsWithError(t, "periodic job with ID already registered: periodic_job_id", func() {
periodicJobBundle.Add(periodicJob)
})
_, err := periodicJobBundle.AddSafely(periodicJob)
require.EqualError(t, err, "periodic job with ID already registered: periodic_job_id")
})
t.Run("AddManyError", func(t *testing.T) {
t.Parallel()
periodicJobBundle, _ := setup(t)
periodicJob := NewPeriodicJob(
PeriodicInterval(15*time.Minute),
func() (JobArgs, *InsertOpts) { return nil, nil },
&PeriodicJobOpts{ID: "periodic_job_id"},
)
periodicJobBundle.Add(periodicJob)
require.PanicsWithError(t, "periodic job with ID already registered: periodic_job_id", func() {
periodicJobBundle.AddMany([]*PeriodicJob{periodicJob})
})
_, err := periodicJobBundle.AddManySafely([]*PeriodicJob{periodicJob})
require.EqualError(t, err, "periodic job with ID already registered: periodic_job_id")
})
}
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
}