-
Notifications
You must be signed in to change notification settings - Fork 21
/
client_test.go
387 lines (344 loc) · 9.81 KB
/
client_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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package dalga // nolint: testpackage
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestClient performs basic functionality tests.
func TestClient(t *testing.T) {
c := make(chan string)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
buf.ReadFrom(r.Body)
defer r.Body.Close()
c <- buf.String()
_, _ = w.Write([]byte("OK"))
}))
defer srv.Close()
config := DefaultConfig
config.Endpoint.BaseURL = "http://" + srv.Listener.Addr().String() + "/"
config.MySQL.SkipLocked = false
config.Listen.Port = 34007
config.MySQL.Table = "test_client"
d, lis, cleanup := newDalga(t, config)
defer cleanup()
ctx, cancel := context.WithCancel(context.Background())
go d.Run(ctx)
defer func() {
cancel()
<-d.NotifyDone()
}()
callCtx := context.Background()
clnt := NewClient("http://" + lis.Addr())
t.Run("get nonexistent", func(t *testing.T) {
_, err := clnt.Get(callCtx, "what", "who")
if err != ErrNotExist {
t.Fatal("expected ErrNotExist")
}
})
t.Run("schedule", func(t *testing.T) {
if j, err := clnt.Schedule(callCtx, "when", "where", MustWithIntervalString("PT1M")); err != nil {
t.Fatal(err)
} else if j.Body != "where" {
t.Fatalf("unexpected body: %s", j.Body)
}
})
t.Run("get", func(t *testing.T) {
if j, err := clnt.Get(callCtx, "when", "where"); err != nil {
t.Fatal(err)
} else if j.Body != "where" {
t.Fatalf("unexpected body: %s", j.Body)
}
})
t.Run("can't disable nonexistent", func(t *testing.T) {
if _, err := clnt.Disable(callCtx, "apple", "banana"); err != ErrNotExist {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("disable", func(t *testing.T) {
if j, err := clnt.Disable(callCtx, "when", "where"); err != nil {
t.Fatal(err)
} else if j.NextRun != nil {
t.Fatalf("unexpected next_run: %v", j.NextRun)
}
})
t.Run("enable", func(t *testing.T) {
if j, err := clnt.Enable(callCtx, "when", "where"); err != nil {
t.Fatal(err)
} else if j.NextRun == nil {
t.Fatalf("unexpected next_run: %v", j.NextRun)
}
})
t.Run("cancel", func(t *testing.T) {
if err := clnt.Cancel(callCtx, "when", "where"); err != nil {
t.Fatal(err)
}
if _, err := clnt.Get(callCtx, "when", "where"); err != ErrNotExist {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("idempotent cancel", func(t *testing.T) {
if err := clnt.Cancel(callCtx, "when", "where"); err != nil {
t.Fatal(err)
}
})
}
func printJob(t *testing.T, j *Job) {
t.Helper()
var nextRun string
if j.NextRun != nil {
nextRun = *j.NextRun
}
t.Logf("Job: interval=%s, next_run=%s, next_sched=%s", j.Interval, nextRun, j.NextSched)
}
// TestEnableScheduling ensures that re-enabled jobs schedule their next run correctly.
func TestEnableScheduling(t *testing.T) {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
t.Fatal(err)
}
start := time.Date(2020, 8, 29, 15, 47, 0, 0, loc)
tests := []struct {
name string
fixed bool
retry int
start time.Time
firstRun time.Time
interval string
success bool
disableAt time.Time
enableAt time.Time
expectRunAt time.Time
notes string
}{
{
name: "brief-pause",
fixed: false,
retry: 60,
start: start,
firstRun: start.Add(time.Hour),
interval: "PT5H",
success: true,
disableAt: start.Add(time.Hour * 2),
enableAt: start.Add(time.Hour * 3),
expectRunAt: start.Add(time.Hour * 6),
notes: "should have no effect",
},
{
name: "brief-pause-fixed",
fixed: true,
retry: 60,
start: start,
firstRun: start.Add(time.Hour),
interval: "PT5H",
success: true,
disableAt: start.Add(time.Hour * 2),
enableAt: start.Add(time.Hour * 3),
expectRunAt: start.Add(time.Hour * 6),
notes: "should have no effect",
},
{
name: "brief-pause-during-retry",
fixed: false,
retry: 60,
start: start,
firstRun: start.Add(time.Hour),
interval: "PT5H",
success: false,
disableAt: start.Add(time.Hour + time.Second*30),
enableAt: start.Add(time.Hour + time.Second*45),
expectRunAt: start.Add(time.Hour + time.Minute),
notes: "should have no effect",
},
{
name: "brief-pause-during-retry-fixed",
fixed: true,
retry: 60,
start: start,
firstRun: start.Add(time.Hour),
interval: "PT5H",
success: false,
disableAt: start.Add(time.Hour + time.Second*30),
enableAt: start.Add(time.Hour + time.Second*45),
expectRunAt: start.Add(time.Hour * 6),
notes: "should cancel retries and reschedule",
},
{
name: "pass-over-schedule-point",
fixed: false,
retry: 60,
start: start,
firstRun: start.Add(time.Hour),
interval: "PT5H",
success: true,
disableAt: start.Add(time.Hour * 2),
enableAt: start.Add(time.Hour * 7),
expectRunAt: start.Add(time.Hour * 6),
notes: "should have run point in the past",
},
{
name: "pass-over-schedule-point-fixed",
fixed: true,
retry: 60,
start: start,
firstRun: start.Add(time.Hour),
interval: "PT5H",
success: true,
disableAt: start.Add(time.Hour * 2),
enableAt: start.Add(time.Hour * 7),
expectRunAt: start.Add(time.Hour * 11),
notes: "should reschedule for the next future point",
},
}
config := DefaultConfig
config.MySQL.SkipLocked = false
config.Listen.Port = 34009
config.MySQL.Table = "test_enable_scheduling"
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
c := make(chan string)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
buf.ReadFrom(r.Body)
defer r.Body.Close()
c <- buf.String()
if test.success {
_, _ = w.Write([]byte("OK"))
} else {
http.Error(w, "failed", 400)
}
}))
config.Endpoint.BaseURL = "http://" + srv.Listener.Addr().String() + "/"
config.Jobs.FixedIntervals = test.fixed
config.Jobs.RetryInterval = time.Duration(test.retry) * time.Second
config.Jobs.RetryMaxInterval = time.Duration(test.retry) * time.Second
config.Jobs.RetryMultiplier = 1
d, lis, cleanup := newDalga(t, config)
defer cleanup()
t.Log("setting clock to:", test.start.String())
clk := d.UseClock(test.start)
runCtx, cancel := context.WithCancel(context.Background())
go d.Run(runCtx)
defer func() {
cancel()
<-d.NotifyDone()
}()
ctx := context.Background()
clnt := NewClient("http://" + lis.Addr())
t.Log("scheduling test job")
j, err := clnt.Schedule(ctx, "abc", test.name,
WithFirstRun(test.firstRun),
MustWithIntervalString(test.interval),
WithLocation(loc),
)
if err != nil {
t.Fatal(err)
}
printJob(t, j)
t.Log("setting clock to:", test.firstRun.Add(time.Second))
clk.Set(test.firstRun.Add(time.Second))
select {
case body := <-c:
if body != test.name {
t.Fatalf("expected '%s' but found '%s'", test.name, body)
}
case <-time.After(time.Second * 3):
t.Fatal("never received POST for 1st job execution")
}
<-time.After(time.Millisecond * 100)
j, err = clnt.Get(ctx, j.Path, j.Body)
if err != nil {
t.Fatal(err)
}
printJob(t, j)
t.Log("setting clock to:", test.disableAt.String())
clk.Set(test.disableAt)
t.Log("disabling test job")
j, err = clnt.Disable(ctx, "abc", test.name)
if err != nil {
t.Fatal(err)
}
printJob(t, j)
if j.NextRun != nil {
t.Fatalf("unexpected next run: %s", *j.NextRun)
}
t.Log("setting clock to:", test.enableAt.String())
clk.Set(test.enableAt)
t.Log("disabling test job")
j, err = clnt.Enable(ctx, "abc", test.name)
if err != nil {
t.Fatal(err)
}
printJob(t, j)
if j.NextRun == nil {
t.Fatalf("unexpected j.NextRun: %v", j.NextRun)
}
nextRun, err := time.Parse(time.RFC3339, *j.NextRun)
if err != nil {
t.Fatal(err)
}
if nextRun.After(test.expectRunAt.Add(time.Second)) || nextRun.Before(test.expectRunAt.Add(-time.Second)) {
t.Fatalf("run at '%s' too different from expected value '%s'", nextRun.Format(time.RFC3339), test.expectRunAt.Format(time.RFC3339))
}
})
}
}
// TestDisableRunningJob ensures that if a job is disabled after
// it starts executing, the rescheduling action that occurs when
// execution finishes will not inadvertently re-enable the job.
func TestDisableRunningJob(t *testing.T) {
c1 := make(chan struct{})
c2 := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c1 <- struct{}{}
<-c2
_, _ = w.Write([]byte("OK"))
}))
config := DefaultConfig
config.MySQL.SkipLocked = false
config.Listen.Port = 34010
config.MySQL.Table = "test_disable_running_job"
config.Endpoint.BaseURL = "http://" + srv.Listener.Addr().String() + "/"
d, lis, cleanup := newDalga(t, config)
defer cleanup()
runCtx, cancel := context.WithCancel(context.Background())
go d.Run(runCtx)
defer func() {
cancel()
<-d.NotifyDone()
}()
ctx := context.Background()
clnt := NewClient("http://" + lis.Addr())
_, err := clnt.Schedule(ctx, "alpha", "beta",
WithFirstRun(time.Now().Add(time.Second)),
MustWithIntervalString("PT1H"),
)
if err != nil {
t.Fatal(err)
}
select {
case <-c1:
case <-time.After(time.Second * 3):
t.Fatal("never received POST")
}
j, err := clnt.Disable(ctx, "alpha", "beta")
if err != nil {
t.Fatal(err)
}
if j.NextRun != nil {
t.Fatalf("unexpected nextRun: %v", *j.NextRun)
}
c2 <- struct{}{}
<-time.After(time.Millisecond * 100)
j, err = clnt.Get(ctx, "alpha", "beta")
if err != nil {
t.Fatal(err)
}
if j.NextRun != nil {
t.Fatalf("unexpected nextRun: %v", *j.NextRun)
}
}