-
Notifications
You must be signed in to change notification settings - Fork 11
/
wait_any_test.go
190 lines (156 loc) · 8.04 KB
/
wait_any_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
190
package asynctask_test
import (
"fmt"
"testing"
"time"
"github.com/Azure/go-asynctask"
"github.com/stretchr/testify/assert"
)
func TestWaitAnyNoTask(t *testing.T) {
t.Parallel()
ctx, _ := newTestContextWithTimeout(t, 2*time.Second)
err := asynctask.WaitAny(ctx, nil)
assert.NoError(t, err)
}
func TestWaitAny(t *testing.T) {
t.Parallel()
ctx, cancelTaskExecution := newTestContextWithTimeout(t, 2*time.Second)
start := time.Now()
countingTsk3 := asynctask.Start(ctx, getCountingTask(10, "countingPer2ms", 2*time.Millisecond))
result := "something"
completedTsk := asynctask.NewCompletedTask(&result)
err := asynctask.WaitAny(ctx, nil, countingTsk3, completedTsk)
elapsed := time.Since(start)
assert.NoError(t, err)
// should finish after right away
assert.True(t, elapsed < 2*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
start = time.Now()
countingTsk1 := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond))
countingTsk2 := asynctask.Start(ctx, getCountingTask(10, "countingPer20ms", 20*time.Millisecond))
countingTsk3 = asynctask.Start(ctx, getCountingTask(10, "countingPer2ms", 2*time.Millisecond))
err = asynctask.WaitAny(ctx, &asynctask.WaitAnyOptions{FailOnAnyError: true}, countingTsk1, countingTsk2, countingTsk3)
elapsed = time.Since(start)
assert.NoError(t, err)
cancelTaskExecution()
// should finish right after countingTsk3
assert.True(t, elapsed >= 20*time.Millisecond && elapsed < 200*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
// counting task do testing.Logf in another go routine
// while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343
// wait minor time for the go routine to finish.
time.Sleep(1 * time.Millisecond)
}
func TestWaitAnyContextCancel(t *testing.T) {
t.Parallel()
ctx, cancelTaskExecution := newTestContextWithTimeout(t, 2*time.Second)
start := time.Now()
countingTsk1 := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond))
countingTsk2 := asynctask.Start(ctx, getCountingTask(10, "countingPer20ms", 20*time.Millisecond))
go func() {
time.Sleep(5 * time.Millisecond)
cancelTaskExecution()
}()
err := asynctask.WaitAny(ctx, nil, countingTsk1, countingTsk2)
elapsed := time.Since(start)
assert.Error(t, err)
assert.Equal(t, "WaitAny context canceled", err.Error(), "expecting context canceled error")
// should finish right after countingTsk3
assert.True(t, elapsed >= 5*time.Millisecond && elapsed < 200*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
// counting task do testing.Logf in another go routine
// while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343
// wait minor time for the go routine to finish.
time.Sleep(1 * time.Millisecond)
}
func TestWaitAnyErrorCase(t *testing.T) {
t.Parallel()
ctx, cancelTaskExecution := newTestContextWithTimeout(t, 3*time.Second)
start := time.Now()
errorTsk := asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond))
result := "something"
completedTsk := asynctask.NewCompletedTask(&result)
err := asynctask.WaitAny(ctx, nil, errorTsk, completedTsk)
assert.NoError(t, err)
elapsed := time.Since(start)
// should finish after right away
assert.True(t, elapsed < 20*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
completedTskState := completedTsk.State()
assert.Equal(t, asynctask.StateCompleted, completedTskState, "completed task should finished")
start = time.Now()
countingTsk := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond))
errorTsk = asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond))
panicTsk := asynctask.Start(ctx, getPanicTask(20*time.Millisecond))
err = asynctask.WaitAny(ctx, nil, countingTsk, errorTsk, panicTsk)
// there is a succeed task
assert.NoError(t, err)
elapsed = time.Since(start)
countingTskState := countingTsk.State()
panicTskState := panicTsk.State()
errTskState := errorTsk.State()
cancelTaskExecution() // all assertion variable captured, cancel counting task
// should only finish after longest task.
assert.True(t, elapsed >= 40*10*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
assert.Equal(t, asynctask.StateCompleted, countingTskState, "countingTask should NOT finished")
assert.Equal(t, asynctask.StateFailed, errTskState, "error task should failed")
assert.Equal(t, asynctask.StateFailed, panicTskState, "panic task should Not failed")
// counting task do testing.Logf in another go routine
// while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343
// wait minor time for the go routine to finish.
time.Sleep(1 * time.Millisecond)
}
func TestWaitAnyAllFailCase(t *testing.T) {
t.Parallel()
ctx, cancelTaskExecution := newTestContextWithTimeout(t, 3*time.Second)
start := time.Now()
errorTsk := asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond))
panicTsk := asynctask.Start(ctx, getPanicTask(20*time.Millisecond))
err := asynctask.WaitAny(ctx, nil, errorTsk, panicTsk)
assert.Error(t, err)
panicTskState := panicTsk.State()
errTskState := errorTsk.State()
elapsed := time.Since(start)
cancelTaskExecution() // all assertion variable captured, cancel counting task
assert.Equal(t, "expected error", err.Error(), "expecting first error")
// should finsh after both error
assert.True(t, elapsed >= 20*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
assert.Equal(t, asynctask.StateFailed, errTskState, "error task should failed")
assert.Equal(t, asynctask.StateFailed, panicTskState, "panic task should Not failed")
// counting task do testing.Logf in another go routine
// while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343
// wait minor time for the go routine to finish.
time.Sleep(1 * time.Millisecond)
}
func TestWaitAnyErrorWithFailOnAnyErrorCase(t *testing.T) {
t.Parallel()
ctx, cancelTaskExecution := newTestContextWithTimeout(t, 3*time.Second)
start := time.Now()
errorTsk := asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond))
result := "something"
completedTsk := asynctask.NewCompletedTask(&result)
err := asynctask.WaitAny(ctx, &asynctask.WaitAnyOptions{FailOnAnyError: true}, errorTsk, completedTsk)
assert.NoError(t, err)
elapsed := time.Since(start)
// should finish after right away
assert.True(t, elapsed < 20*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
start = time.Now()
countingTsk := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond))
errorTsk = asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond))
panicTsk := asynctask.Start(ctx, getPanicTask(20*time.Millisecond))
err = asynctask.WaitAny(ctx, &asynctask.WaitAnyOptions{FailOnAnyError: true}, countingTsk, errorTsk, panicTsk)
assert.Error(t, err)
completedTskState := completedTsk.State()
assert.Equal(t, asynctask.StateCompleted, completedTskState, "completed task should finished")
countingTskState := countingTsk.State()
panicTskState := panicTsk.State()
errTskState := errorTsk.State()
elapsed = time.Since(start)
cancelTaskExecution() // all assertion variable captured, cancel counting task
assert.Equal(t, "expected error", err.Error(), "expecting first error")
// should finsh after first error
assert.True(t, elapsed >= 10*time.Millisecond && elapsed < 20*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed))
assert.Equal(t, asynctask.StateRunning, countingTskState, "countingTask should NOT finished")
assert.Equal(t, asynctask.StateFailed, errTskState, "error task should failed")
assert.Equal(t, asynctask.StateRunning, panicTskState, "panic task should Not failed")
// counting task do testing.Logf in another go routine
// while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343
// wait minor time for the go routine to finish.
time.Sleep(1 * time.Millisecond)
}