This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler_test.go
152 lines (119 loc) · 2.94 KB
/
scheduler_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
package scheduler
import (
"strconv"
"sync/atomic"
"testing"
"time"
)
func TestJobs(t *testing.T) {
var err error
s := NewScheduler()
for i := 0; i < 20; i++ {
err = s.Make(strconv.FormatInt(int64(i), 10), "1 0 0 1 1 * 2099", testFunction, nil)
if err != nil {
t.Fatal("Make error:", err)
}
}
names := s.Jobs()
if len(names) != 20 {
t.Fatalf("names - expected: %v - received: %v", 20, len(names))
}
for i := 0; i < 20; i++ {
err = s.Delete(strconv.FormatInt(int64(i), 10))
if err != nil {
t.Fatal("Delete error:", err)
}
}
}
func TestStopAllWait(t *testing.T) {
var err error
var name string
s := NewScheduler()
for i := 0; i < 30; i++ {
name = strconv.FormatInt(int64(i), 10)
err = s.Make(name, "* * * * * * *", testFunction, nil)
if err != nil {
t.Fatal("Make error:", err)
}
err = s.UpdateNextRun(name, time.Now())
if err != nil {
t.Fatal("UpdateNextRun error:", err)
}
err = s.Start(name)
if err != nil {
t.Fatal("Start error:", err)
}
}
s.StopAllWait(5 * time.Second)
jobsNotStopped := atomic.LoadInt64(&s.jobsNotStopped)
if jobsNotStopped != 0 {
t.Fatalf("jobsNotStopped - expected: %v - received: %v", 0, jobsNotStopped)
}
for i := 0; i < 30; i++ {
name = strconv.FormatInt(int64(i), 10)
err = s.UpdateFunction(name, testFunction, uint(2))
if err != nil {
t.Fatal("UpdateFunction error:", err)
}
err = s.UpdateNextRun(name, time.Now())
if err != nil {
t.Fatal("UpdateNextRun error:", err)
}
err = s.Start(name)
if err != nil {
t.Fatal("Start error:", err)
}
}
s.StopAllWait(5 * time.Second)
jobsNotStopped = atomic.LoadInt64(&s.jobsNotStopped)
if jobsNotStopped != 0 {
t.Fatalf("jobsNotStopped - expected: %v - received: %v", 0, jobsNotStopped)
}
for i := 0; i < 30; i++ {
name = strconv.FormatInt(int64(i), 10)
err = s.UpdateNextRun(name, time.Now().Add(time.Millisecond))
if err != nil {
t.Fatal("UpdateNextRun error:", err)
}
err = s.Start(name)
if err != nil {
t.Fatal("Start error:", err)
}
time.Sleep(time.Millisecond)
}
s.StopAllWait(5 * time.Second)
jobsNotStopped = atomic.LoadInt64(&s.jobsNotStopped)
if jobsNotStopped != 0 {
t.Fatalf("jobsNotStopped - expected: %v - received: %v", 0, jobsNotStopped)
}
for i := 0; i < 30; i++ {
name = strconv.FormatInt(int64(i), 10)
err = s.Delete(name)
if err != nil {
t.Fatal("Delete error:", err)
}
}
err = s.Make("a", "* * * * * * *", testFunction, 100)
if err != nil {
t.Fatal("Make error:", err)
}
err = s.UpdateNextRun("a", time.Now())
if err != nil {
t.Fatal("UpdateNextRun error:", err)
}
err = s.Start("a")
if err != nil {
t.Fatal("Start error:", err)
}
<-chanStart
s.StopAllWait(time.Nanosecond)
jobsNotStopped = atomic.LoadInt64(&s.jobsNotStopped)
if jobsNotStopped != 1 {
t.Fatalf("jobsNotStopped - expected: %v - received: %v", 1, jobsNotStopped)
}
err = s.Delete("a")
if err != nil {
t.Fatal("Delete error:", err)
}
<-chanDone
}