forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 4
/
watchdog_test.go
99 lines (85 loc) · 1.82 KB
/
watchdog_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
package work
import (
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCheckTimesHeap(t *testing.T) {
t1, _ := time.Parse(time.RFC3339, "2019-01-01T00:00:00Z")
t2, _ := time.Parse(time.RFC3339, "2020-01-01T00:00:00Z")
t3, _ := time.Parse(time.RFC3339, "2021-01-01T00:00:00Z")
tests := []struct {
data []time.Time
expected []time.Time
}{
{
[]time.Time{},
[]time.Time{},
},
{
[]time.Time{t1},
[]time.Time{t1},
},
{
[]time.Time{t1, t1},
[]time.Time{t1},
},
{
[]time.Time{t1, t2, t3},
[]time.Time{t1, t2, t3},
},
{
[]time.Time{t3, t3, t3},
[]time.Time{t3},
},
{
[]time.Time{t2, t1},
[]time.Time{t1, t2},
},
{
[]time.Time{t3, t2, t1},
[]time.Time{t1, t2, t3},
},
{
[]time.Time{t3, t3, t3, t2, t2, t1},
[]time.Time{t1, t2, t3},
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
h := newCheckTimesHeap()
for _, v := range tt.data {
h.Push(v)
}
require.Equal(t, len(tt.expected), h.Len())
for i := 0; h.Len() > 0; i++ {
tm := h.Pop()
require.Equal(t, tt.expected[i], tm)
}
})
}
}
func TestWatchdog(t *testing.T) {
t.Parallel()
require := require.New(t)
const jobName = "test"
j, err := newPeriodicJob("* * * * * *", jobName)
require.NoError(err)
w := newWatchdog(
watchdogWithFailCheckingTimeout(time.Millisecond * 2000),
)
defer w.stop()
w.addPeriodicJobs(j)
w.start()
w.planning(time.Now())
time.Sleep(time.Millisecond * 2000)
w.processedJobs <- &Job{
Name: jobName,
EnqueuedAt: time.Now().Unix(),
}
time.Sleep(time.Millisecond * 500)
require.Equal(WatchdogStat{Name: "test", Processed: 1, Skipped: 0}, w.stats()[0])
time.Sleep(time.Millisecond * 1600)
require.Equal(WatchdogStat{Name: "test", Processed: 1, Skipped: 1}, w.stats()[0])
}