-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
future_test.go
326 lines (241 loc) · 6.63 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
package mo
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func assertAndIncrement(is *assert.Assertions, expected int, i *int32) {
got := atomic.LoadInt32(i)
is.Equal(int32(expected), got)
atomic.AddInt32(i, 1)
}
func TestFuture(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
resolve(42)
}).Then(func(value int) (int, error) {
is.Equal(42, value)
return 21, assert.AnError
}).Catch(func(err error) (int, error) {
is.Equal(assert.AnError, err)
return 0, nil
}).Then(func(value int) (int, error) {
is.Equal(0, value)
return 84, nil
}).Collect()
is.Equal(84, result)
is.Nil(err)
}
func TestFutureSimpleResolve(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
resolve(42)
}).Collect()
is.Equal(42, result)
is.Nil(err)
}
func TestFutureSimpleReject(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
reject(assert.AnError)
}).Collect()
is.Equal(0, result)
is.NotNil(err)
is.Equal(assert.AnError, err)
}
func TestFutureMultipleResolve(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
resolve(42)
}).Then(func(value int) (int, error) {
is.Equal(42, value)
return 84, nil
}).Then(func(value int) (int, error) {
is.Equal(84, value)
return 21, nil
}).Collect()
is.Equal(21, result)
is.Nil(err)
}
func TestFutureMultipleReject(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
resolve(42)
}).Catch(func(err error) (int, error) {
is.Fail("should not enter here")
return 84, assert.AnError
}).Then(func(value int) (int, error) {
is.Equal(42, value)
return 21, assert.AnError
}).Catch(func(err error) (int, error) {
is.Equal(assert.AnError, err)
return 1, nil
}).Collect()
is.Equal(1, result)
is.Nil(err)
}
func TestFutureSingleReject(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
reject(assert.AnError)
}).Catch(func(err error) (int, error) {
is.Equal(assert.AnError, err)
return 84, nil
}).Collect()
is.Equal(84, result)
is.Nil(err)
}
func TestFutureErrorResult(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
reject(assert.AnError)
}).Collect()
is.Equal(0, result)
is.NotNil(err)
is.Equal(assert.AnError, err)
}
func TestFutureFinallyResolve(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
resolve(21)
}).Finally(func(value int, err error) (int, error) {
is.Equal(21, value)
is.Nil(err)
return 42, nil
}).Collect()
is.Equal(42, result)
is.Nil(err)
}
func TestFutureFinallyReject(t *testing.T) {
is := assert.New(t)
result, err := NewFuture[int](func(resolve func(int), reject func(error)) {
reject(assert.AnError)
}).Finally(func(value int, err error) (int, error) {
is.Equal(0, value)
is.NotNil(err)
is.Equal(assert.AnError, err)
return 42, nil
}).Collect()
is.Equal(42, result)
is.Nil(err)
}
func TestFutureOrder(t *testing.T) {
is := assert.New(t)
var i int32 = 0
_ = NewFuture[int](func(resolve func(int), reject func(error)) {
assertAndIncrement(is, 1, &i)
resolve(42)
}).Then(func(value int) (int, error) {
assertAndIncrement(is, 2, &i)
return 21, assert.AnError
}).Catch(func(err error) (int, error) {
assertAndIncrement(is, 3, &i)
return 1, nil
}).Finally(func(value int, err error) (int, error) {
assertAndIncrement(is, 4, &i)
return 21, nil
})
assertAndIncrement(is, 0, &i)
}
func TestFutureOrderCollect(t *testing.T) {
is := assert.New(t)
var i int32 = 0
_, _ = NewFuture[int](func(resolve func(int), reject func(error)) {
assertAndIncrement(is, 0, &i)
resolve(42)
}).Then(func(value int) (int, error) {
assertAndIncrement(is, 1, &i)
return 21, assert.AnError
}).Catch(func(err error) (int, error) {
assertAndIncrement(is, 2, &i)
return 1, nil
}).Finally(func(value int, err error) (int, error) {
assertAndIncrement(is, 3, &i)
return 1, nil
}).Collect()
assertAndIncrement(is, 4, &i)
}
func TestFutureCancel(t *testing.T) {
is := assert.New(t)
var i int32 = 0
future := NewFuture[int](func(resolve func(int), reject func(error)) {
assertAndIncrement(is, 0, &i)
time.Sleep(5 * time.Millisecond)
resolve(42)
}).Then(func(value int) (int, error) {
assertAndIncrement(is, 3, &i)
is.Fail("should not enter here")
return 21, assert.AnError
})
time.Sleep(1 * time.Millisecond)
assertAndIncrement(is, 1, &i)
future.Cancel()
time.Sleep(10 * time.Millisecond)
assertAndIncrement(is, 2, &i)
}
func TestFutureCancelDelayed(t *testing.T) {
is := assert.New(t)
var i int32 = 0
future := NewFuture[int](func(resolve func(int), reject func(error)) {
time.Sleep(1 * time.Millisecond)
assertAndIncrement(is, 1, &i)
resolve(42)
}).Then(func(value int) (int, error) {
assertAndIncrement(is, 2, &i)
return 21, assert.AnError
})
assertAndIncrement(is, 0, &i)
time.Sleep(10 * time.Millisecond)
future.Cancel()
assertAndIncrement(is, 3, &i)
}
func TestFutureCancelTerminated(t *testing.T) {
is := assert.New(t)
var i int32 = 0
future := NewFuture[int](func(resolve func(int), reject func(error)) {
time.Sleep(1 * time.Millisecond)
assertAndIncrement(is, 1, &i)
resolve(42)
}).Then(func(value int) (int, error) {
assertAndIncrement(is, 2, &i)
return 21, assert.AnError
})
assertAndIncrement(is, 0, &i)
_, _ = future.Collect()
assertAndIncrement(is, 3, &i)
future.Cancel()
assertAndIncrement(is, 4, &i)
}
func TestFutureResultResult(t *testing.T) {
is := assert.New(t)
result := NewFuture[int](func(resolve func(int), reject func(error)) {
reject(assert.AnError)
}).Result()
is.Equal(Err[int](assert.AnError), result)
is.NotNil(result.Error())
is.Equal(assert.AnError, result.Error())
}
func TestFutureResultEither(t *testing.T) {
is := assert.New(t)
either := NewFuture[int](func(resolve func(int), reject func(error)) {
reject(assert.AnError)
}).Either()
is.Equal(Left[error, int](assert.AnError), either)
is.NotNil(either.Left())
is.Equal(assert.AnError, either.MustLeft())
}
func TestFutureCompleteBeforeThen(t *testing.T) {
completed := make(chan struct{})
fut := NewFuture(func(resolve func(int), reject func(error)) {
resolve(1)
close(completed)
})
<-completed
//nolint:errcheck
fut.Then(func(in int) (int, error) {
fmt.Println(in) // will never been print
return in, nil
}).Collect() // deadlock
}