-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine_test.go
313 lines (249 loc) · 6.63 KB
/
combine_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2023-2024 Oliver Eikemeier. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package async_test
import (
"context"
"testing"
"fillmore-labs.com/exp/async"
"fillmore-labs.com/exp/async/result"
"github.com/stretchr/testify/assert"
)
const iterations = 3
func makePromisesAndFutures[R any]() ([]async.Promise[R], []async.Future[R]) {
var promises [iterations]async.Promise[R]
var futures [iterations]async.Future[R]
for i := 0; i < iterations; i++ {
promises[i], futures[i] = async.New[R]()
}
return promises[:], futures[:]
}
func TestWaitAll(t *testing.T) {
t.Parallel()
// given
promises, futures := makePromisesAndFutures[int]()
promises[0].Resolve(1)
promises[1].Reject(errTest)
promises[2].Resolve(2)
// when
ctx := context.Background()
results := async.AwaitAllResults(ctx, futures...)
// then
assert.Len(t, results, len(futures))
v0, err0 := results[0].V()
err1 := results[1].Err()
v2, err2 := results[2].V()
if assert.NoError(t, err0) {
assert.Equal(t, 1, v0)
}
assert.ErrorIs(t, err1, errTest)
if assert.NoError(t, err2) {
assert.Equal(t, 2, v2)
}
}
func TestAllValues(t *testing.T) {
t.Parallel()
// given
promises, futures := makePromisesAndFutures[int]()
for i := 0; i < iterations; i++ {
promises[i].Resolve(i + 1)
}
// when
ctx := context.Background()
results, err := async.AwaitAllValues(ctx, futures...)
// then
if assert.NoError(t, err) {
assert.Len(t, results, iterations)
for i := 0; i < iterations; i++ {
assert.Equal(t, i+1, results[i])
}
}
}
func TestAllValuesError(t *testing.T) {
t.Parallel()
// given
promises, futures := makePromisesAndFutures[int]()
promises[1].Reject(errTest)
// when
ctx := context.Background()
_, err := async.AwaitAllValues(ctx, futures...)
// then
assert.ErrorIs(t, err, errTest)
}
func TestFirst(t *testing.T) {
t.Parallel()
// given
promises, futures := makePromisesAndFutures[int]()
promises[1].Resolve(2)
// when
ctx := context.Background()
v, err := async.AwaitFirst(ctx, futures...)
// then
if assert.NoError(t, err) {
assert.Equal(t, 2, v)
}
}
func TestCombineCancellation(t *testing.T) {
t.Parallel()
subTests := []struct {
name string
combine func([]async.Future[int], context.Context) error
}{
{name: "First", combine: func(futures []async.Future[int], ctx context.Context) error {
_, err := async.AwaitFirst(ctx, futures...)
return err
}},
{name: "All", combine: func(futures []async.Future[int], ctx context.Context) error {
r := async.AwaitAllResults(ctx, futures...)
return r[0].Err()
}},
{name: "AllValues", combine: func(futures []async.Future[int], ctx context.Context) error {
_, err := async.AwaitAllValues(ctx, futures...)
return err
}},
}
for _, tc := range subTests {
combine := tc.combine
test := func(t *testing.T) {
t.Helper()
t.Parallel()
// given
_, futures := makePromisesAndFutures[int]()
// when
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := combine(futures, ctx)
// then
assert.ErrorIs(t, err, context.Canceled)
}
_ = t.Run(tc.name, test)
}
}
func TestCombineMemoized(t *testing.T) {
t.Parallel()
subTests := []struct {
name string
combine func(context.Context, []async.Future[int]) (any, error)
expect func(t *testing.T, actual any)
}{
{name: "First", combine: func(ctx context.Context, futures []async.Future[int]) (any, error) {
return async.AwaitFirst(ctx, futures...)
}, expect: func(t *testing.T, actual any) { t.Helper(); assert.Equal(t, 3, actual) }},
{name: "All", combine: func(ctx context.Context, futures []async.Future[int]) (any, error) {
return async.AwaitAllResults(ctx, futures...), nil
}, expect: func(t *testing.T, actual any) {
t.Helper()
vv, ok := actual.([]result.Result[int])
if !ok {
assert.Fail(t, "Unexpected result type")
return
}
for _, v := range vv {
value, err := v.V()
if assert.NoError(t, err) {
assert.Equal(t, 3, value)
}
}
}},
{name: "AllValues", combine: func(ctx context.Context, futures []async.Future[int]) (any, error) {
return async.AwaitAllValues(ctx, futures...)
}, expect: func(t *testing.T, actual any) { t.Helper(); assert.Equal(t, []int{3, 3, 3}, actual) }},
}
for _, tc := range subTests {
combine := tc.combine
expect := tc.expect
_ = t.Run(tc.name, func(t *testing.T) {
t.Helper()
t.Parallel()
// given
promises, futures := makePromisesAndFutures[int]()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, p := range promises {
p.Resolve(3)
}
// when
v, err := combine(ctx, futures)
// then
if assert.NoError(t, err) {
expect(t, v)
}
})
}
}
func TestAwaitAllEmpty(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
results := async.AwaitAllResultsAny(ctx)
// then
assert.Empty(t, results)
}
func TestAwaitAllValuesEmpty(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
results, err := async.AwaitAllValuesAny(ctx)
// then
if assert.NoError(t, err) {
assert.Empty(t, results)
}
}
func TestAwaitFirstEmpty(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
_, err := async.AwaitFirstAny(ctx)
// then
assert.ErrorIs(t, err, async.ErrNoResult)
}
func TestAllAny(t *testing.T) {
// given
t.Parallel()
ctx := context.Background()
p1, f1 := async.New[int]()
p2, f2 := async.New[string]()
p3, f3 := async.New[struct{}]()
p1.Resolve(1)
p2.Resolve("test")
p3.Resolve(struct{}{})
// when
results := make([]result.Result[any], 3)
async.AwaitAllAny(ctx, f1, f2, f3)(func(i int, r result.Result[any]) bool {
results[i] = r
return true
})
// then
for i, r := range results {
if assert.NoError(t, r.Err()) {
switch i {
case 0:
assert.Equal(t, 1, r.Value())
case 1:
assert.Equal(t, "test", r.Value())
case 2:
assert.Equal(t, struct{}{}, r.Value())
default:
assert.Fail(t, "unexpected index")
}
}
}
}