-
Notifications
You must be signed in to change notification settings - Fork 18
/
control_impl_test.go
166 lines (136 loc) · 4.19 KB
/
control_impl_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
package floc
import (
"fmt"
"testing"
)
var templateData = []struct {
data interface{}
test func(sample interface{}) bool
}{
{nil, func(sample interface{}) bool { return sample == nil }},
{string("ABC"), func(sample interface{}) bool { s, ok := sample.(string); return ok && s == "ABC" }},
{int(123), func(sample interface{}) bool { d, ok := sample.(int); return ok && d == 123 }},
{float64(3.1415), func(sample interface{}) bool { f, ok := sample.(float64); return ok && f >= 3.1415 && f <= 3.14151 }},
}
func TestNewControl(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
ctrl := NewControl(ctx)
defer ctrl.Release()
if ctrl.IsFinished() == true {
t.Fatalf("%s expects Control to be not finished", t.Name())
}
result, _, _ := ctrl.Result()
if result.IsNone() == false {
t.Fatalf("%s expects result to be None but has %s", t.Name(), result.String())
}
}
func TestNewControl_Panic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("%s must panic when ctx is nil", t.Name())
}
}()
ctrl := NewControl(nil)
defer ctrl.Release()
}
func TestFlowControl_Release(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
ctrl := NewControl(ctx)
ctrl.Release()
if ctrl.IsFinished() == false {
result, _, _ := ctrl.Result()
t.Fatalf("%s expects Control to be finished on Release but has state %s", t.Name(), result.String())
}
result, _, _ := ctrl.Result()
if result.IsCanceled() == false {
t.Fatalf("%s expects Control to be Canceled bu has %s", t.Name(), result.String())
}
}
func TestFlowControl_Release2(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
ctrl := NewControl(ctx)
ctrl.Complete(nil)
ctrl.Release()
if ctrl.IsFinished() == false {
result, _, _ := ctrl.Result()
t.Fatalf("%s expects Control to be finished on Release but has state %s", t.Name(), result.String())
}
result, _, _ := ctrl.Result()
if result.IsCompleted() == false {
t.Fatalf("%s expects Control to be Completed but has %s", t.Name(), result.String())
}
}
func TestFlowControl_Cancel(t *testing.T) {
for i, td := range templateData {
ctx := NewContext()
ctrl := NewControl(ctx)
ctrl.Cancel(td.data)
result, data, err := ctrl.Result()
if err != nil {
t.Fatalf("%s expects error to be nil bu has %s", t.Name(), err.Error())
} else if result.IsCanceled() == false {
t.Fatalf("%s expects result to be Canceled but has %s", t.Name(), result.String())
} else if td.test(data) == false {
t.Fatalf("%s failed to test data sample %d:%v", t.Name(), i, data)
}
ctrl.Release()
ctx.Release()
}
}
func TestFlowControl_Complete(t *testing.T) {
for i, td := range templateData {
ctx := NewContext()
ctrl := NewControl(ctx)
ctrl.Complete(td.data)
result, data, err := ctrl.Result()
if err != nil {
t.Fatalf("%s expects error to be nil bu has %s", t.Name(), err.Error())
} else if result.IsCompleted() == false {
t.Fatalf("%s expects result to be Completed but has %s", t.Name(), result.String())
} else if td.test(data) == false {
t.Fatalf("%s failed to test data sample %d:%v", t.Name(), i, data)
}
ctrl.Release()
ctx.Release()
}
}
func TestFlowControl_Fail(t *testing.T) {
for i, td := range templateData {
ctx := NewContext()
ctrl := NewControl(ctx)
ctrl.Fail(td.data, fmt.Errorf("fail %d", i))
result, data, err := ctrl.Result()
if err == nil {
t.Fatalf("%s expects error to be not nil", t.Name())
} else if result.IsFailed() == false {
t.Fatalf("%s expects result to be Failed but has %s", t.Name(), result.String())
} else if td.test(data) == false {
t.Fatalf("%s failed to test data sample %d:%v", t.Name(), i, data)
}
ctrl.Release()
ctx.Release()
}
}
func TestFlowControl_IsFinished(t *testing.T) {
var finishers = []func(ctrl Control){
func(ctrl Control) { ctrl.Complete(nil) },
func(ctrl Control) { ctrl.Cancel(nil) },
func(ctrl Control) { ctrl.Fail(nil, nil) },
}
for _, finish := range finishers {
ctx := NewContext()
ctrl := NewControl(ctx)
if ctrl.IsFinished() == true {
t.Fatalf("%s expects Control to be not finished", t.Name())
}
finish(ctrl)
if ctrl.IsFinished() == false {
t.Fatalf("%s expects Control to be finished", t.Name())
}
ctrl.Release()
ctx.Release()
}
}