-
Notifications
You must be signed in to change notification settings - Fork 0
/
periodic_test.go
56 lines (51 loc) · 1.68 KB
/
periodic_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
package amboy
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWaitUntil(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
const interval = 5 * time.Second
t.Run("PastStartAt", func(t *testing.T) {
tsa := time.Now().Round(time.Second)
waitUntilInterval(ctx, time.Now().Round(time.Second).Add(-interval), interval)
delta := time.Since(tsa.Add(interval)).Abs()
assert.True(t, delta < interval, "%s", delta)
assert.True(t, tsa.Before(time.Now()))
})
t.Run("FutureStartAt", func(t *testing.T) {
tsa := time.Now().Round(time.Second)
waitUntilInterval(ctx, time.Now().Round(time.Second).Add(interval), interval)
assert.Equal(t, tsa.Add(interval), time.Now().Round(time.Second))
assert.True(t, tsa.Before(time.Now()))
})
t.Run("Cancelable", func(t *testing.T) {
ctx, cancel = context.WithCancel(ctx)
cancel()
tsa := time.Now().Round(time.Second)
waitUntilInterval(ctx, time.Now().Round(time.Second).Add(interval), interval)
assert.Equal(t, time.Now().Round(time.Second), tsa)
})
t.Run("DuplicateJobError", func(t *testing.T) {
t.Run("WithReportingDisabled", func(t *testing.T) {
err := scheduleOp(ctx, nil, func(_ context.Context, q Queue) error {
return NewDuplicateJobError("err")
}, QueueOperationConfig{})
if err != nil {
t.Fatal(err)
}
})
t.Run("WithReportingEnabled", func(t *testing.T) {
err := scheduleOp(ctx, nil, func(_ context.Context, q Queue) error {
return NewDuplicateJobError("err")
}, QueueOperationConfig{EnableDuplicateJobReporting: true})
if err == nil {
t.Error("expected error")
}
assert.True(t, IsDuplicateJobError(err))
})
})
}