-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduer_test.go
184 lines (162 loc) · 3.38 KB
/
scheduer_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package planner_test
import (
"context"
"strings"
"testing"
"time"
"github.com/gianarb/planner"
)
const Seq = "seq"
type FakePlan struct {
P []planner.Procedure
Counter int
}
func (p *FakePlan) Create(ctx context.Context) ([]planner.Procedure, error) {
if p.Counter > 0 {
return nil, nil
}
p.Counter++
return p.P, nil
}
func (p *FakePlan) Name() string {
return "fake"
}
type Two struct {
Seq chan string
Counter int
}
func (o *Two) Name() string {
return "two"
}
func (o *Two) Do(ctx context.Context) ([]planner.Procedure, error) {
if o.Counter > 0 {
return nil, nil
}
o.Seq <- "two"
o.Counter++
return []planner.Procedure{
&One{
String: "from-two",
Seq: o.Seq,
},
}, nil
}
type One struct {
Seq chan string
String string
}
func (o *One) Name() string {
return "one"
}
func (o *One) Do(ctx context.Context) ([]planner.Procedure, error) {
o.Seq <- "one"
return nil, nil
}
type FakeStep struct {
do func(ctx context.Context) ([]planner.Procedure, error)
name string
}
func (o *FakeStep) Name() string {
if o.name == "" {
return "fake"
}
return o.name
}
func (o *FakeStep) Do(ctx context.Context) ([]planner.Procedure, error) {
return o.do(ctx)
}
func TestTriggerSchedulerTimeout(t *testing.T) {
ctx := context.Background()
ctx, cF := context.WithTimeout(ctx, 200*time.Millisecond)
defer cF()
p := &FakePlan{
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &FakeStep{
name: "sleep",
do: func(ctx context.Context) ([]planner.Procedure, error) {
time.Sleep(210 * time.Millisecond)
return nil, nil
},
})
p.P = append(p.P, &FakeStep{
name: "sleep",
do: func(ctx context.Context) ([]planner.Procedure, error) {
time.Sleep(210 * time.Millisecond)
return nil, nil
},
})
s := planner.NewScheduler()
err := s.Execute(ctx, p)
if err != context.DeadlineExceeded {
t.Fatalf("expected to get an deadline exceeded error. we got %s", err)
}
}
func TestExecutionSingleStep(t *testing.T) {
order := []string{}
seq := make(chan string, 1)
ctx := context.Background()
p := &FakePlan{
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &One{
Seq: seq,
})
s := planner.NewScheduler()
s.Execute(ctx, p)
close(seq)
for ss := range seq {
order = append(order, ss)
}
if strings.Join(order, " ") != "one" {
t.Errorf("expected \"one\". Got \"%s\".", strings.Join(order, " "))
}
}
func TestExecutionStepTwoThatReturnStepOne(t *testing.T) {
order := []string{}
seq := make(chan string, 2)
ctx := context.Background()
p := &FakePlan{
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &Two{
Seq: seq,
})
s := planner.NewScheduler()
s.Execute(ctx, p)
close(seq)
for ss := range seq {
order = append(order, ss)
}
if strings.Join(order, " ") != "two one" {
t.Errorf("expected \"two one\". Got \"%s\".", strings.Join(order, " "))
}
}
func TestExecutionStepTwoAndStapOne(t *testing.T) {
order := []string{}
seq := make(chan string, 3)
ctx := context.Background()
p := &FakePlan{
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &Two{
Seq: seq,
})
p.P = append(p.P, &One{
Seq: seq,
String: "plan",
})
s := planner.NewScheduler()
s.Execute(ctx, p)
close(seq)
for ss := range seq {
order = append(order, ss)
}
if strings.Join(order, " ") != "two one one" {
t.Errorf("expected \"two one one\". Got \"%s\".", strings.Join(order, " "))
}
}