-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_test.go
189 lines (155 loc) · 3.48 KB
/
workflow_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
185
186
187
188
189
package workflow
import (
"testing"
"time"
)
func double(data interface{}) (interface{}, error) {
d := data.(int)
return d * 2, nil
}
func sq(data interface{}) (interface{}, error) {
d := data.(int)
return d * d, nil
}
func up(data interface{}) (interface{}, error) {
d := data.(int)
return d + 1, nil
}
func buildExpectation(n int) []int {
expected := []int{}
for i := 0; i < n; i++ {
expected = append(expected, 4*i*i+1)
}
return expected
}
func TestWorkflow(t *testing.T) {
workflow := NewWorkflow(&Config{}, double, sq, up)
if l := len(workflow.steps); l != 3 {
t.Errorf("Workflow Len was incorrect, got: %d, want: %d.", l, 3)
}
}
func TestWorkflowSerial(t *testing.T) {
workflow := NewWorkflow(&Config{}, double, sq, up)
expected := 17
if got := workflow.Exec(2).(int); got != expected {
t.Errorf("Workflow Serial was incorrect, got: %d, want: %d.", got, expected)
}
}
func TestWorkflowParallel(t *testing.T) {
nb := 25
workflow := NewWorkflow(&Config{MaxRetries: 0, Concurrency: 2}, double, sq, up)
returnChan := workflow.HelperAddChanResult(nb)
workflow.Start()
defer workflow.Close()
expected := buildExpectation(nb)
for i := 0; i < nb; i++ {
workflow.AddJob(i)
}
var results []int
for i := 0; i < nb; i++ {
l := <-returnChan
results = append(results, l.(int))
}
if !sameSlice(results, expected) {
t.Errorf("Workflow Parallel was incorrect, got: %v, want: %v.", results, expected)
}
}
func TestWait(t *testing.T) {
nb := 20
workflow := NewWorkflow(&Config{MaxRetries: 0, Concurrency: 2}, double, sq, up)
returnChan := workflow.HelperAddChanResult(nb)
workflow.Start()
waitCh := make(chan struct{})
defer workflow.Close()
expected := []int{}
for i := 0; i < nb; i++ {
workflow.AddJob(i)
expected = append(expected, 4*i*i+1)
}
go func() {
for {
<-returnChan
}
}()
go func() {
workflow.Wait()
close(waitCh)
}()
select {
case <-waitCh:
case <-time.After(100 * time.Millisecond):
t.Error("Workflow Parallel didnt terminated by WaitGroup but with Timeout.")
}
}
func step1(w *Workflow) Processor {
return func(data interface{}) (interface{}, error) {
v := data.(int)
if v < 5 {
w.AddJob(v + 1)
}
return v + 1, nil
}
}
func step2(data interface{}) (interface{}, error) {
d := data.(int)
return d * d, nil
}
func TestSpawning(t *testing.T) {
workflow := NewWorkflow(&Config{MaxRetries: 0, Concurrency: 2})
workflow.AddProcessors(step1(workflow), step2)
returnChan := workflow.HelperAddChanResult(5)
workflow.Start()
waitCh := make(chan struct{})
defer func() {
workflow.Close()
close(returnChan)
}()
expected := []int{4, 9, 16, 25, 36}
got := []int{}
workflow.AddJob(1)
go func() {
// for mdg := range returnChan
for {
msg, open := <-returnChan
if !open {
break
}
got = append(got, msg.(int))
}
}()
go func() {
workflow.Wait()
close(waitCh)
}()
select {
case <-waitCh:
case <-time.After(100 * time.Millisecond):
t.Error("Workflow Parallel didnt terminated by WaitGroup but with Timeout.")
}
<-time.After(100 * time.Millisecond)
if !sameSlice(got, expected) {
t.Errorf("TestSpawning was incorrect, got: %v, want: %v.", got, expected)
}
}
func sameSlice(x, y []int) bool {
if len(x) != len(y) {
return false
}
diff := make(map[int]int, len(x))
for _, _x := range x {
diff[_x]++
}
for _, _y := range y {
if _, ok := diff[_y]; !ok {
return false
}
diff[_y]--
if diff[_y] == 0 {
delete(diff, _y)
}
}
if len(diff) == 0 {
return true
}
return false
}