-
Notifications
You must be signed in to change notification settings - Fork 0
/
future_test.go
257 lines (206 loc) · 4.82 KB
/
future_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
// 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"
"errors"
"sync"
"testing"
"time"
"fillmore-labs.com/async"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)
var errTest = errors.New("test error")
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestAsyncValue(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
f := async.NewAsync(func() (int, error) { return 1, nil })
val, err := f.Await(ctx)
// then
if assert.NoError(t, err) {
assert.Equal(t, 1, val)
}
}
func TestAsyncError(t *testing.T) {
t.Parallel()
// given
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// when
f := async.NewAsync(func() (int, error) { return 0, errTest })
_, err := f.Await(ctx)
// then
assert.ErrorIs(t, err, errTest)
}
func TestCancellation(t *testing.T) {
t.Parallel()
// given
run := make(chan struct{})
f := async.NewAsync(func() (int, error) {
<-run
return 1, nil
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
// when
_, err := f.Await(ctx)
close(run)
// then
assert.ErrorIs(t, err, context.Canceled)
}
func TestMultiple(t *testing.T) {
t.Parallel()
// given
const iterations = 1_000
const concurrency = 4
// when
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
for i := 0; i < iterations; i++ {
p := async.Promise[int]{}
f := p.Future()
var values [concurrency]int
var errs [concurrency]error
var wg sync.WaitGroup
wg.Add(concurrency)
for c := 0; c < concurrency; c++ {
go func(i int) {
defer wg.Done()
values[i], errs[i] = f.Await(ctx)
}(c)
}
p.Resolve(i)
wg.Wait()
// then
for c := 0; c < concurrency; c++ {
if assert.NoError(t, errs[c]) {
assert.Equal(t, i, values[c])
}
}
}
}
func TestTry(t *testing.T) {
t.Parallel()
// given
p := async.Promise[int]{}
f := p.Future()
// when
_, err1 := f.Try()
_ = f.Done()
_, err2 := f.Try()
p.Resolve(1)
value3, err3 := f.Try()
value4, err4 := f.Try()
// then
assert.ErrorIs(t, err1, async.ErrNotReady)
assert.ErrorIs(t, err2, async.ErrNotReady)
if assert.NoError(t, err3) {
assert.Equal(t, 1, value3)
}
if assert.NoError(t, err4) {
assert.Equal(t, 1, value4)
}
}
func TestNil(t *testing.T) {
t.Parallel()
// given
var p *async.Promise[int]
f := p.Future()
// when
p.Resolve(1)
p.Reject(errTest)
p.Do(func() (int, error) { panic("should not be called") })
_, err := f.Try()
done := f.Done()
// then
assert.ErrorIs(t, err, async.ErrNotReady)
assert.Nil(t, done)
}
func TestPromise_String(t *testing.T) {
t.Parallel()
pending := func() *async.Promise[int] {
p := async.Promise[int]{}
return &p
}
pending2 := func() *async.Promise[int] {
p := async.Promise[int]{}
_ = p.Future().Done()
return &p
}
resolved := func() *async.Promise[int] {
p := async.Promise[int]{}
p.Resolve(1)
return &p
}
tests := []struct {
name string
p *async.Promise[int]
want string
}{
{"Nil", (*async.Promise[int])(nil), "Promise <nil>"},
{"Pending", pending(), "Promise pending"},
{"PendingWithDone", pending2(), "Promise pending"},
{"Resolved", resolved(), "Promise resolved: 1, <nil>"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, tt.p.String(), "String()")
})
}
}
func TestFuture_String(t *testing.T) {
t.Parallel()
pending := func() *async.Future[int] {
p := async.Promise[int]{}
return p.Future()
}
pending2 := func() *async.Future[int] {
p := async.Promise[int]{}
_ = p.Future().Done()
return p.Future()
}
resolved := func() *async.Future[int] {
p := async.Promise[int]{}
p.Reject(errTest)
return p.Future()
}
tests := []struct {
name string
f *async.Future[int]
want string
}{
{"Nil", (*async.Future[int])(nil), "Future <nil>"},
{"Pending", pending(), "Future pending"},
{"PendingWithDone", pending2(), "Future pending"},
{"Resolved", resolved(), "Future resolved: 0, test error"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, tt.f.String(), "String()")
})
}
}