-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmulti_tasks_aggration_test.go
175 lines (159 loc) · 4.63 KB
/
multi_tasks_aggration_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
package concurrency
import (
"context"
"fmt"
"testing"
"time"
)
var task1 Task = func(ctx context.Context) TaskResult {
time.Sleep(time.Second * 2)
return TaskResult{
"Task1", nil,
}
}
var task2 Task = func(ctx context.Context) TaskResult {
time.Sleep(time.Second * 1)
return TaskResult{
"Task2", nil,
}
}
var task3 Task = func(ctx context.Context) TaskResult {
time.Sleep(time.Second * 3)
return TaskResult{
"Task3", nil,
}
}
// Tests for AnyOneReturn
func TestHappyCaseOfAnyReturn(t *testing.T) {
startTime := time.Now()
retStub := AsyncExecutorForFirstReturn(context.TODO(), 3500, task1, task2, task3)
if time.Since(startTime).Milliseconds() > 2 {
t.Error("It is not asynchronous call.")
}
fmt.Println("nonblocking")
if retStub.GetResultWhenAnyTaskReturns().Err != nil {
t.Error("Unexpected error occurred.")
}
str, _ := retStub.GetResultWhenAnyTaskReturns().Result.(string)
if str != "Task2" {
t.Error("unexpected return value.")
return
}
if time.Since(startTime).Milliseconds() > 1050 {
t.Error("Time spent is unexpected.")
return
}
fmt.Println(retStub.GetResultWhenAnyTaskReturns().Result.(string),
retStub.GetResultWhenAnyTaskReturns().Err)
}
func TestTimeoutCaseOfAnyReturn(t *testing.T) {
startTime := time.Now()
retStub := AsyncExecutorForFirstReturn(context.TODO(), 100, task1, task2, task3)
if time.Since(startTime).Milliseconds() > 2 {
t.Error("It is not asynchronous call.")
}
fmt.Println("nonblocking")
err := retStub.GetResultWhenAnyTaskReturns().Err
if err == nil || err != ErrTimeout {
t.Error("Timeout error is expected.")
return
}
if time.Since(startTime).Milliseconds() > 110 {
t.Error("Time spent is unexpected.")
return
}
}
func TestCancelCaseOfAnyReturn(t *testing.T) {
startTime := time.Now()
ctx, cacelFn := context.WithCancel(context.Background())
retStub := AsyncExecutorForFirstReturn(ctx, 100, task1, task2, task3)
if time.Since(startTime).Milliseconds() > 2 {
t.Error("It is not asynchronous call.")
}
fmt.Println("nonblocking")
cacelFn()
err := retStub.GetResultWhenAnyTaskReturns().Err
if err == nil || err != ErrCancelled {
t.Error("Cancelled error is expected.")
return
}
if time.Since(startTime).Milliseconds() > 3 {
t.Error("Time spent is unexpected.")
return
}
fmt.Println(retStub.GetResultWhenAnyTaskReturns())
}
// Tests for AllReturn
func TestHappyCaseOfAllReturn(t *testing.T) {
startTime := time.Now()
retStub := AsyncExecutorForAllReturn(context.TODO(), 3500, task1, task2, task3)
if time.Since(startTime).Milliseconds() > 2 {
t.Error("It is not asynchronous call.")
}
fmt.Println("nonblocking")
if retStub.GetResultsWhenAllTasksReturn().Err != nil {
t.Error("Unexpected error occurred.")
return
}
taskRet := *retStub.GetResultsWhenAllTasksReturn().Results
if taskRet[0] == nil || taskRet[1] == nil || taskRet[2] == nil {
t.Error("Unexpected results.")
return
}
rets := *retStub.GetResultsWhenAllTasksReturn().Results
fmt.Println(rets[0], rets[1], rets[2])
if time.Since(startTime).Milliseconds() > 3050 {
t.Error("Time spent is unexpected.")
return
}
}
func TestTimeoutCaseOfAllReturn(t *testing.T) {
startTime := time.Now()
retStub := AsyncExecutorForAllReturn(context.TODO(), 2100, task1, task2, task3)
if time.Since(startTime).Milliseconds() > 2 {
t.Error("It is not asynchronous call.")
}
fmt.Println("nonblocking")
rets := *retStub.GetResultsWhenAllTasksReturn().Results
if rets[0] == nil || rets[1] == nil || rets[2] != nil {
t.Error("unexpected results", rets[0], rets[1], rets[2])
return
}
fmt.Println(rets[0], rets[1], rets[2])
err := retStub.GetResultsWhenAllTasksReturn().Err
fmt.Println(err)
if err == nil || err != ErrTimeout {
t.Error("Timeout error is expected.")
return
}
if time.Since(startTime).Milliseconds() > 2200 {
t.Error("Time spent is unexpected.")
return
}
}
func TestCancelCaseOfAllReturn(t *testing.T) {
startTime := time.Now()
ctx, cacelFn := context.WithCancel(context.Background())
retStub := AsyncExecutorForAllReturn(ctx, 100, task1, task2, task3)
if time.Since(startTime).Milliseconds() > 2 {
t.Error("It is not asynchronous call.")
}
fmt.Println("nonblocking")
cacelFn()
err := retStub.GetResultsWhenAllTasksReturn().Err
if err == nil || err != ErrCancelled {
t.Error("Cancelled error is expected.")
return
}
if time.Since(startTime).Milliseconds() > 3 {
t.Error("Time spent is unexpected.")
return
}
fmt.Println(retStub.GetResultsWhenAllTasksReturn())
rets := *retStub.GetResultsWhenAllTasksReturn().Results
if rets[0] != nil || rets[1] != nil || rets[2] != nil {
t.Error("unexpected results", rets[0], rets[1], rets[2])
return
}
fmt.Println(rets[0], rets[1], rets[2])
}