-
Notifications
You must be signed in to change notification settings - Fork 1
/
combine_all_test.go
122 lines (102 loc) · 2.66 KB
/
combine_all_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
// 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
//go:build goexperiment.rangefunc
package async_test
import (
"context"
"testing"
"fillmore-labs.com/exp/async"
"fillmore-labs.com/exp/async/result"
"github.com/stretchr/testify/assert"
)
func TestAll(t *testing.T) {
t.Parallel()
// given
promises, futures := makePromisesAndFutures[int]()
values := []struct {
value int
err error
}{
{1, nil},
{0, errTest},
{2, nil},
}
for i, v := range values {
promises[i].Do(func() (int, error) { return v.value, v.err })
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
results := make([]result.Result[int], len(futures))
for i, r := range async.AwaitAll(ctx, futures...) { //nolint:typecheck
results[i] = r
}
// then
if assert.NoError(t, results[0].Err()) {
assert.Equal(t, 1, results[0].Value())
}
if assert.ErrorIs(t, results[1].Err(), errTest) {
_ = results[1].Value() // Should not panic
}
if assert.NoError(t, results[2].Err()) {
assert.Equal(t, 2, results[2].Value())
}
}
func TestAllEmpty(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
allFutures := async.AwaitAllResults[int](ctx)
// then
assert.Zero(t, len(allFutures))
for _, v := range allFutures { //nolint:typecheck
t.Errorf("Invalid value %v", v)
}
}
func TestAnyAll(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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)
for i, r := range async.AwaitAllAny(ctx, f1, f2, f3) { //nolint:typecheck
results[i] = r
}
// 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")
}
}
}
}