-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow_sevices_test.go
149 lines (119 loc) · 2.95 KB
/
flow_sevices_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
package workflow
import (
"fmt"
"testing"
"time"
)
func TestFlow_AndCall(t *testing.T) {
counter := 2
flow := NewFlow(func() {
counter = counter - 1
}).AndCall(func() {
time.Sleep(50 * time.Millisecond)
counter = counter - 1
})
flow.Execute()
flow.Get(0)
if counter != 0 {
t.Errorf("expected counter=0 but counter=%d", counter)
}
}
func TestFlow_ThenApply(t *testing.T) {
counter := 1
addFlow := NewFlow(func() int {
return 2
}).ThenApply(func(cnt int) {
counter = cnt
})
addFlow.Execute()
if get, err := addFlow.Get(0); err != nil {
t.Errorf("did not expect an error (%s)", err.Error())
} else if len(get) == 0 && counter == 2 {
} else {
t.Errorf("expected a return value of 2 but got %d", counter)
}
}
func TestFlow_ThenCombine(t *testing.T) {
addFlow := NewFlow(func() int {
return 1
}).AndCall(func() int {
return 1
}).ThenCombine(func(op1, op2 int) int {
return op1 + op2
})
addFlow.Execute()
if get, err := addFlow.Get(0); err != nil {
t.Errorf("did not expect an error (%s)", err.Error())
} else if len(get) == 1 && get[0] == 2 {
} else {
t.Errorf("expected a return value of 2 but got %d", get[0])
}
}
func TestFlow_Cancel(t *testing.T) {
flow := NewFlow(func() bool {
time.Sleep(100 * time.Millisecond)
return true
})
flow.Execute()
time.AfterFunc(50*time.Millisecond, func() {
fmt.Println("Triggered user abort")
flow.Cancel()
})
if _, err := flow.Get(0); err == nil {
t.Errorf("expected an error!!!")
}
}
func TestFlow_Get_TimeOut(t *testing.T) {
flow := NewFlow(func() bool {
time.Sleep(100 * time.Millisecond)
return true
})
flow.Execute()
if _, err := flow.Get(5 * time.Millisecond); err == nil {
t.Errorf("expected an error!!!")
}
}
func TestFlow_Execute_RunOnce(t *testing.T) {
counter := 1
flow := NewFlow(func() {
counter = counter - 1
})
go func() {
flow.Execute()
}()
go func() {
flow.Execute()
}()
flow.Get(0)
if counter < 0 {
t.Errorf("target func ran twice!!, expected counter=0 but it is set to %d", counter)
}
}
func ExampleNewFlow() {
getBillAmount := func(timeDelay time.Duration) int {
time.Sleep(timeDelay * time.Millisecond) //simulate blocking call
return 100
}
getDollarValue := func(timeDelay time.Duration) int {
time.Sleep(timeDelay * time.Millisecond) //simulate blocking call
return 60
}
billInDollars := func(billAmount, conversionRate int) int {
return billAmount * conversionRate
}
billFlow := NewFlow(getBillAmount, time.Duration(30)).
AndCall(getDollarValue, time.Duration(30)).
ThenCombine(billInDollars).
ThenApply(func(finalAmount int) string {
return fmt.Sprintf("Bill Amount = %d$", finalAmount)
})
newExecutorService := NewExecutorService(10, 5)
billFlow.SetExecutor(newExecutorService).Execute()
if finalAmount, e := billFlow.Get(0); e != nil {
fmt.Errorf("error (%s) while waiting to get the final bill amount", e.Error())
} else {
fmt.Println(finalAmount[0])
}
//// Output:
//// Bill Amount = 6000$
}