-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathfactory_test.go
426 lines (372 loc) · 11.4 KB
/
factory_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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package rxgo
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func collect(ctx context.Context, ch <-chan Item) ([]interface{}, error) {
s := make([]interface{}, 0)
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case item, ok := <-ch:
if !ok {
return s, nil
}
if item.Error() {
s = append(s, item.E)
} else {
s = append(s, item.V)
}
}
}
}
func Test_Amb1(t *testing.T) {
obs := Amb([]Observable{testObservable(1, 2, 3), Empty()})
Assert(context.Background(), t, obs, HasItems(1, 2, 3))
}
func Test_Amb2(t *testing.T) {
obs := Amb([]Observable{Empty(), testObservable(1, 2, 3), Empty(), Empty()})
Assert(context.Background(), t, obs, HasItems(1, 2, 3))
}
func Test_CombineLatest(t *testing.T) {
obs := CombineLatest(func(ii ...interface{}) interface{} {
sum := 0
for _, v := range ii {
if v == nil {
continue
}
sum += v.(int)
}
return sum
}, []Observable{testObservable(1, 2), testObservable(10, 11)})
Assert(context.Background(), t, obs, IsNotEmpty())
}
func Test_CombineLatest_Empty(t *testing.T) {
obs := CombineLatest(func(ii ...interface{}) interface{} {
sum := 0
for _, v := range ii {
sum += v.(int)
}
return sum
}, []Observable{testObservable(1, 2), Empty()})
Assert(context.Background(), t, obs, IsEmpty())
}
func Test_CombineLatest_Error(t *testing.T) {
obs := CombineLatest(func(ii ...interface{}) interface{} {
sum := 0
for _, v := range ii {
sum += v.(int)
}
return sum
}, []Observable{testObservable(1, 2), testObservable(errFoo)})
Assert(context.Background(), t, obs, IsEmpty(), HasError(errFoo))
}
func Test_Concat_SingleObservable(t *testing.T) {
obs := Concat([]Observable{testObservable(1, 2, 3)})
Assert(context.Background(), t, obs, HasItems(1, 2, 3))
}
func Test_Concat_TwoObservables(t *testing.T) {
obs := Concat([]Observable{testObservable(1, 2, 3), testObservable(4, 5, 6)})
Assert(context.Background(), t, obs, HasItems(1, 2, 3, 4, 5, 6))
}
func Test_Concat_MoreThanTwoObservables(t *testing.T) {
obs := Concat([]Observable{testObservable(1, 2, 3), testObservable(4, 5, 6), testObservable(7, 8, 9)})
Assert(context.Background(), t, obs, HasItems(1, 2, 3, 4, 5, 6, 7, 8, 9))
}
func Test_Concat_EmptyObservables(t *testing.T) {
obs := Concat([]Observable{Empty(), Empty(), Empty()})
Assert(context.Background(), t, obs, IsEmpty())
}
func Test_Concat_OneEmptyObservable(t *testing.T) {
obs := Concat([]Observable{Empty(), testObservable(1, 2, 3)})
Assert(context.Background(), t, obs, HasItems(1, 2, 3))
obs = Concat([]Observable{testObservable(1, 2, 3), Empty()})
Assert(context.Background(), t, obs, HasItems(1, 2, 3))
}
func Test_Create(t *testing.T) {
obs := Create([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Of(3)
}})
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
}
func Test_Create_SingleDup(t *testing.T) {
obs := Create([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Of(3)
}})
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
Assert(context.Background(), t, obs, IsEmpty(), HasNoError())
}
func Test_Create_ContextCancelled(t *testing.T) {
closed1 := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
Create([]Producer{
func(ctx context.Context, next chan<- Item) {
cancel()
}, func(ctx context.Context, next chan<- Item) {
<-ctx.Done()
closed1 <- struct{}{}
},
}, WithContext(ctx)).Run()
select {
case <-time.Tick(time.Second):
assert.FailNow(t, "producer not closed")
case <-closed1:
}
}
func Test_Defer(t *testing.T) {
obs := Defer([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Of(3)
}})
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
}
func Test_Defer_Multiple(t *testing.T) {
obs := Defer([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
}, func(ctx context.Context, next chan<- Item) {
next <- Of(10)
next <- Of(20)
}})
Assert(context.Background(), t, obs, HasItemsNoOrder(1, 2, 10, 20), HasNoError())
}
func Test_Defer_ContextCancelled(t *testing.T) {
closed1 := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
Defer([]Producer{
func(ctx context.Context, next chan<- Item) {
cancel()
}, func(ctx context.Context, next chan<- Item) {
<-ctx.Done()
closed1 <- struct{}{}
},
}, WithContext(ctx)).Run()
select {
case <-time.Tick(time.Second):
assert.FailNow(t, "producer not closed")
case <-closed1:
}
}
func Test_Defer_SingleDup(t *testing.T) {
obs := Defer([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Of(3)
}})
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
}
func Test_Defer_ComposedDup(t *testing.T) {
obs := Defer([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Of(3)
}}).Map(func(_ context.Context, i interface{}) (_ interface{}, _ error) {
return i.(int) + 1, nil
}).Map(func(_ context.Context, i interface{}) (_ interface{}, _ error) {
return i.(int) + 1, nil
})
Assert(context.Background(), t, obs, HasItems(3, 4, 5), HasNoError())
Assert(context.Background(), t, obs, HasItems(3, 4, 5), HasNoError())
}
func Test_Defer_ComposedDup_EagerObservation(t *testing.T) {
obs := Defer([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Of(3)
}}).Map(func(_ context.Context, i interface{}) (_ interface{}, _ error) {
return i.(int) + 1, nil
}, WithObservationStrategy(Eager)).Map(func(_ context.Context, i interface{}) (_ interface{}, _ error) {
return i.(int) + 1, nil
})
Assert(context.Background(), t, obs, HasItems(3, 4, 5), HasNoError())
// In the case of an eager observation, we already consumed the items produced by Defer
// So if we create another subscription, it will be empty
Assert(context.Background(), t, obs, IsEmpty(), HasNoError())
}
func Test_Defer_Error(t *testing.T) {
obs := Defer([]Producer{func(ctx context.Context, next chan<- Item) {
next <- Of(1)
next <- Of(2)
next <- Error(errFoo)
}})
Assert(context.Background(), t, obs, HasItems(1, 2), HasError(errFoo))
}
func Test_Empty(t *testing.T) {
obs := Empty()
Assert(context.Background(), t, obs, IsEmpty())
}
func Test_FromChannel(t *testing.T) {
ch := make(chan Item)
go func() {
ch <- Of(1)
ch <- Of(2)
ch <- Of(3)
close(ch)
}()
obs := FromChannel(ch)
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
}
func Test_FromChannel_SimpleCapacity(t *testing.T) {
ch := FromChannel(make(chan Item, 10)).Observe()
assert.Equal(t, 10, cap(ch))
}
func Test_FromChannel_ComposedCapacity(t *testing.T) {
obs1 := FromChannel(make(chan Item, 10)).
Map(func(_ context.Context, _ interface{}) (interface{}, error) {
return 1, nil
}, WithBufferedChannel(11))
assert.Equal(t, 11, cap(obs1.Observe()))
obs2 := obs1.Map(func(_ context.Context, _ interface{}) (interface{}, error) {
return 1, nil
}, WithBufferedChannel(12))
assert.Equal(t, 12, cap(obs2.Observe()))
}
func Test_FromEventSource_ObservationAfterAllSent(t *testing.T) {
const max = 10
next := make(chan Item, max)
obs := FromEventSource(next, WithBackPressureStrategy(Drop))
go func() {
for i := 0; i < max; i++ {
next <- Of(i)
}
close(next)
}()
time.Sleep(50 * time.Millisecond)
Assert(context.Background(), t, obs, CustomPredicate(func(items []interface{}) error {
if len(items) != 0 {
return errors.New("items should be nil")
}
return nil
}))
}
func Test_FromEventSource_Drop(t *testing.T) {
const max = 100000
next := make(chan Item, max)
obs := FromEventSource(next, WithBackPressureStrategy(Drop))
go func() {
for i := 0; i < max; i++ {
next <- Of(i)
}
close(next)
}()
Assert(context.Background(), t, obs, CustomPredicate(func(items []interface{}) error {
if len(items) == max {
return errors.New("some items should be dropped")
}
if len(items) == 0 {
return errors.New("no items")
}
return nil
}))
}
func Test_Interval(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
obs := Interval(WithDuration(time.Nanosecond), WithContext(ctx))
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
Assert(context.Background(), t, obs, IsNotEmpty())
}
func Test_JustItem(t *testing.T) {
single := JustItem(1)
Assert(context.Background(), t, single, HasItem(1), HasNoError())
Assert(context.Background(), t, single, HasItem(1), HasNoError())
}
func Test_Just(t *testing.T) {
obs := Just([]int{1, 2, 3})
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
Assert(context.Background(), t, obs, HasItems(1, 2, 3), HasNoError())
}
func Test_Just_CustomStructure(t *testing.T) {
type customer struct {
id int
}
obs := Just([]customer{
{id: 1},
{id: 2},
{id: 3},
})
Assert(context.Background(), t, obs, HasItems(customer{id: 1}, customer{id: 2}, customer{id: 3}), HasNoError())
Assert(context.Background(), t, obs, HasItems(customer{id: 1}, customer{id: 2}, customer{id: 3}), HasNoError())
}
func Test_Just_SimpleCapacity(t *testing.T) {
ch := Just([]Item{Of(1)}, WithBufferedChannel(5)).Observe()
assert.Equal(t, 5, cap(ch))
}
func Test_Just_ComposedCapacity(t *testing.T) {
obs1 := Just([]Item{Of(1)}).Map(func(_ context.Context, _ interface{}) (interface{}, error) {
return 1, nil
}, WithBufferedChannel(11))
assert.Equal(t, 11, cap(obs1.Observe()))
obs2 := obs1.Map(func(_ context.Context, _ interface{}) (interface{}, error) {
return 1, nil
}, WithBufferedChannel(12))
assert.Equal(t, 12, cap(obs2.Observe()))
}
func Test_Merge(t *testing.T) {
obs := Merge([]Observable{testObservable(1, 2), testObservable(3, 4)})
Assert(context.Background(), t, obs, HasItemsNoOrder(1, 2, 3, 4))
}
func Test_Merge_Error(t *testing.T) {
obs := Merge([]Observable{testObservable(1, 2), testObservable(3, errFoo)})
// The content is not deterministic, hence we just test if we have some items
Assert(context.Background(), t, obs, IsNotEmpty(), HasError(errFoo))
}
func Test_Range(t *testing.T) {
obs := Range(5, 3)
Assert(context.Background(), t, obs, HasItems(5, 6, 7, 8))
// Test whether the observable is reproducible
Assert(context.Background(), t, obs, HasItems(5, 6, 7, 8))
}
func Test_Range_NegativeCount(t *testing.T) {
obs := Range(1, -5)
Assert(context.Background(), t, obs, HasAnError())
}
func Test_Range_MaximumExceeded(t *testing.T) {
obs := Range(1<<31, 1)
Assert(context.Background(), t, obs, HasAnError())
}
func Test_Start(t *testing.T) {
obs := Start([]Supplier{func(ctx context.Context) Item {
return Of(1)
}, func(ctx context.Context) Item {
return Of(2)
}})
Assert(context.Background(), t, obs, HasItemsNoOrder(1, 2))
}
func Test_Thrown(t *testing.T) {
obs := Thrown(errFoo)
Assert(context.Background(), t, obs, HasError(errFoo))
}
func Test_Timer(t *testing.T) {
obs := Timer(WithDuration(time.Nanosecond))
select {
case <-time.Tick(time.Second):
assert.FailNow(t, "observable not closed")
case <-obs.Observe():
}
}
func Test_Timer_Empty(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
obs := Timer(WithDuration(time.Hour), WithContext(ctx))
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
select {
case <-time.Tick(time.Second):
assert.FailNow(t, "observable not closed")
case <-obs.Observe():
}
}