-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_internal_test.go
More file actions
226 lines (189 loc) · 5.14 KB
/
job_internal_test.go
File metadata and controls
226 lines (189 loc) · 5.14 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package scheduler
import (
"bytes"
"context"
"fmt"
"log/slog"
"sync/atomic"
"testing"
"time"
)
// TestNewJob verifies that NewJob properly creates a Job.
func TestNewJob(t *testing.T) {
id := "test"
interval := time.Second
called := false
runFunc := func(*Job) bool {
called = true
return true
}
job := NewJob(id, interval, runFunc)
// Test the Job's ID
if job.id != id {
t.Errorf("job.id = %q; want %q", job.id, id)
}
// Test the Job's interval
if job.interval != interval {
t.Errorf("job.interval = %v; want %v", job.interval, interval)
}
// Test that the run function is set and callable
if !job.runFunc(job) {
t.Errorf("job.runFunc() returned false; want true")
}
if !called {
t.Errorf("job.runFunc() was not called")
}
// Test that the recover function is set
if job.recoverFunc == nil {
t.Errorf("job.recoverFunc is nil; want non-nil")
}
// Test that stopOnPanic is false
if job.stopOnPanic != false {
t.Errorf("job.stopOnPanic = %t; want false", job.stopOnPanic)
}
// Test that maxExecutions is set to zero (unlimited)
if job.maxExecutions != 0 {
t.Errorf("job.maxExecutions = %d; want 0", job.maxExecutions)
}
// Test that executions counter is zero
if job.executions.Load() != 0 {
t.Errorf("job.executions = %d; want 0", job.executions.Load())
}
// Test that the logger is set
if job.logger == nil {
t.Errorf("job.logger is nil; want non-nil")
}
// Test that the context and cancel function are set
if job.cancelCtx == nil {
t.Errorf("job.cancelCtx is nil; want non-nil")
}
if job.cancelFunc == nil {
t.Errorf("job.cancelFunc is nil; want non-nil")
}
}
// TestWithRecoveryHandler verifies that the custom function to handle panics
// works properly.
func TestWithRecoveryHandler(t *testing.T) {
called := false
job := NewJob("test",
time.Second,
func(*Job) bool {
panic("test recovery handler")
return true
},
WithRecoveryHandler(
func(*Job, any) {
called = true
},
),
)
shouldContinue := job.execute()
if !shouldContinue {
t.Errorf("shouldContinue is false, want true")
}
if !called {
t.Errorf("recoveryHandler was not called")
}
}
// TestWithMaxExecutions confirms that WithMaxExecutions option is observed.
func TestWithMaxExecutions(t *testing.T) {
tests := []struct {
maxExecutions uint64
wantCounts []uint64
}{
{maxExecutions: 1, wantCounts: []uint64{1, 1}},
{maxExecutions: 2, wantCounts: []uint64{1, 2, 2}},
{maxExecutions: 3, wantCounts: []uint64{1, 2, 3, 3}},
}
for _, tc := range tests {
name := fmt.Sprintf("maxExecutions=%d", tc.maxExecutions)
t.Run(name, func(t *testing.T) {
job := NewJob("test",
time.Second,
func(*Job) bool { return true },
WithMaxExecutions(tc.maxExecutions),
)
for i, expectedCount := range tc.wantCounts {
gotContinue := job.execute()
gotExecutions := job.executions.Load()
wantContinue := i < int(tc.maxExecutions)
if gotExecutions != expectedCount {
t.Errorf("Run %d: job.executions = %d, want %d", i+1, gotExecutions, expectedCount)
}
if gotContinue != wantContinue {
t.Errorf("Run %d: continue = %t, want %t", i+1, gotContinue, wantContinue)
}
}
})
}
}
func TestWithStopOnPanic(t *testing.T) {
tests := []struct {
name string
stopOnPanic bool
wantContinue bool
}{
{name: "continue", stopOnPanic: false, wantContinue: true},
{name: "stop", stopOnPanic: true, wantContinue: false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var executions atomic.Uint64
job := NewJob(tc.name,
time.Second,
func(*Job) bool {
executions.Add(1)
panic(tc.name)
return true
},
WithStopOnPanic(tc.stopOnPanic),
WithRecoveryHandler(func(*Job, any) {}),
)
gotContinue := job.execute()
if gotContinue != tc.wantContinue {
t.Errorf("continue = %t, want %t",
gotContinue, tc.wantContinue)
}
gotExecutions := executions.Load()
wantExecutions := uint64(1)
if gotExecutions != wantExecutions {
t.Errorf("executions = %d, want %d",
gotExecutions, wantExecutions)
}
})
}
}
// TestDefaultRecoveryHandler verifies that the defaultRecoveryHandler logs
// the panic as expected.
func TestDefaultRecoveryHandler(t *testing.T) {
var logBuffer bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logBuffer, nil))
ctx, cancel := context.WithCancel(context.Background())
job := &Job{
logger: logger,
cancelCtx: ctx,
cancelFunc: cancel,
}
panicValue := "TestDefaultRecoveryHandler"
defaultRecoveryHandler(job, panicValue)
logOutput := logBuffer.String()
expectedMsg := "job panicked"
if !bytes.Contains([]byte(logOutput), []byte(expectedMsg)) {
t.Errorf("logOutput = %q, want to contain msg %q",
logOutput, expectedMsg)
}
if !bytes.Contains([]byte(logOutput), []byte(panicValue)) {
t.Errorf("logOutput = %q, want to contain value %q",
logOutput, panicValue)
}
}
// TestStop verifies if Stop correctly sets the job to be stopped.
func TestJobStop(t *testing.T) {
job := NewJob("test", time.Second, func(*Job) bool { return true })
job.Stop()
got := job.IsStopped()
want := true
if got != want {
t.Errorf("IsStopped() = %t, want %t", got, want)
}
}