-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_test.go
88 lines (67 loc) · 1.78 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
package schedly
import (
"github.com/stretchr/testify/assert"
"sync/atomic"
"testing"
"time"
)
func TestStartStop(t *testing.T) {
sched := NewScheduler(time.Millisecond)
sched.Start()
sched.Stop()
sched.WaitUntilStopped()
}
func TestAligned(t *testing.T) {
sched := NewScheduler(time.Millisecond).SetAligned(true)
var startedAt time.Time
sched.Schedule(5*time.Millisecond, "x", func() {
startedAt = time.Now()
sched.Stop()
})
sched.Start()
sched.WaitUntilStopped()
assert.Less(t, startedAt.Sub(startedAt.Truncate(5*time.Millisecond)).Nanoseconds(), time.Millisecond.Nanoseconds())
}
func TestAddJob(t *testing.T) {
sched := NewScheduler(time.Millisecond)
tasks := []string{"x", "y"}
resultChan := make(chan string)
for tNum := 0; tNum < len(tasks); tNum++ {
tName := tasks[tNum]
sched.Schedule(time.Millisecond, tName, func() {
resultChan <- tName
})
}
sched.Start()
results := make(map[string]int)
for len(results) != len(tasks) {
select {
case r := <-resultChan:
results[r]++
}
}
sched.Stop()
for _, tName := range tasks {
if _, ok := results[tName]; !ok {
t.Fatalf("Task %s has not been launched", tName)
}
}
sched.WaitUntilStopped()
}
func TestScheduler_WaitForRunningTasks(t *testing.T) {
sched := NewScheduler(time.Millisecond)
var result int32 = 1
sched.Schedule(time.Millisecond, "x", func() {
time.Sleep(5 * time.Millisecond)
atomic.CompareAndSwapInt32(&result, 1, 2)
})
sched.Start()
time.Sleep(time.Millisecond)
sched.Stop()
waitStarted := time.Now()
sched.WaitForRunningTasks()
waited := time.Now().Sub(waitStarted).Milliseconds()
assert.Less(t, int64(4), waited, "Shoul've waited for the task to finish")
t.Logf("Waited for tasks to finish for %d ms", waited)
assert.Equal(t, int32(2), atomic.LoadInt32(&result))
}