-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduler_test.go
138 lines (115 loc) · 3.5 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
package htask
import (
"context"
"fmt"
"sync"
"testing"
"time"
)
type mockTask struct {
ctx context.Context
i int
chResult chan int
}
func (m mockTask) Task(ts time.Time) {
select {
case <-m.ctx.Done():
case m.chResult <- m.i:
}
}
func TestScheduler(t *testing.T) {
var wg sync.WaitGroup
scheduler := NewScheduler(&wg, 1)
defer func() {
scheduler.Close()
wg.Wait()
}()
chResult := make(chan int)
ctx := context.Background()
ctxCancel, cancelTask := context.WithCancel(context.Background())
cancelTask()
times := make([]time.Time, 10)
times[0] = time.Now().Add(time.Millisecond * 100)
for i := 1; i < 10; i++ {
times[i] = times[i-1].Add(1)
}
scheduler.Set(ctx.Done(), times[0], mockTask{ctx: ctx, i: 0, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[3], mockTask{ctx: ctx, i: 1, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[2], mockTask{ctx: ctx, i: 2, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[5], mockTask{ctx: ctx, i: 3, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[1], mockTask{ctx: ctx, i: 4, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[1], mockTask{ctx: ctx, i: 4, chResult: chResult}.Task)
scheduler.Set(ctxCancel.Done(), times[4], mockTask{ctx: ctx, i: 5, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[2], func(ts time.Time) {
scheduler.Set(ctx.Done(), times[7], mockTask{ctx: ctx, i: 6, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[6], mockTask{ctx: ctx, i: 7, chResult: chResult}.Task)
})
select {
case i := <-chResult:
t.Fatal("unexpected received result befor timer expired : ", i)
case <-time.After(time.Millisecond * 10):
}
time.Sleep(time.Millisecond * 100)
result := []int{0, 4, 4, 2, 1, 3, 7, 6}
for _, i := range result {
select {
case r := <-chResult:
if r != i {
t.Errorf("result received but not euqal %v != %v", r, i)
}
}
}
}
func TestScheduler_Set(t *testing.T) {
var wg sync.WaitGroup
scheduler := NewScheduler(&wg, 1)
defer func() {
scheduler.Close()
wg.Wait()
}()
task := func(_ time.Time) { fmt.Println("invalid time") }
if err := scheduler.Set(nil, time.Time{}, task); err != ErrInvalidTime {
t.Errorf("zero time error : %v expected %v", err, ErrInvalidTime)
}
if err := scheduler.Set(nil, time.Now(), nil); err != ErrInvalidTask {
t.Errorf("nil task error : %v expected %v", err, ErrInvalidTask)
}
}
func TestScheduler_ChangeWorkers(t *testing.T) {
var wg sync.WaitGroup
scheduler := NewScheduler(&wg, 1)
defer func() {
scheduler.Close()
wg.Wait()
}()
chResult := make(chan int)
ctx := context.Background()
testSchedule := func(workers int) {
times := make([]time.Time, 10)
times[0] = time.Now().Add(time.Millisecond * 50)
for i := 1; i < 10; i++ {
times[i] = times[i-1].Add(1)
}
scheduler.Set(ctx.Done(), times[0], mockTask{ctx: ctx, i: 0, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[1], mockTask{ctx: ctx, i: 0, chResult: chResult}.Task)
scheduler.Set(ctx.Done(), times[2], mockTask{ctx: ctx, i: 0, chResult: chResult}.Task)
time.Sleep(time.Millisecond * 50)
for _, i := range []int{0, 0, 0} {
select {
case r := <-chResult:
if r != i {
t.Errorf("workers(%v) result received but not euqal %v != %v", workers, r, i)
}
}
}
}
testSchedule(1)
if err := scheduler.ChangeWorkers(3); err != nil {
t.Errorf("ChangeWorkers 3 err = %v", err)
}
testSchedule(3)
if err := scheduler.ChangeWorkers(0); err != nil {
t.Errorf("ChangeWorkers 0 err = %v", err)
}
testSchedule(0)
}