-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobservable_test.go
332 lines (329 loc) · 6.3 KB
/
observable_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
package rxgo
import (
"errors"
"reflect"
"testing"
"time"
)
var testCases = []struct {
name string
Observable
}{
{
"Of should emit the provided values",
Of(1, 2, 3).
Pipe(
Assert(1, 2, 3),
),
},
{
"Range should emit the provided range",
Range(0, 5).
Pipe(
Assert(0, 1, 2, 3, 4, 5),
),
},
{
"Interval should emit sequential numbers",
Interval(time.Millisecond).
Pipe(
Take(5),
Assert(0, 1, 2, 3, 4),
),
},
{
"Merge should merge the output of the provided observables",
Merge(
Of(1),
Of(1),
Of(1),
).
Pipe(
Assert(1, 1, 1),
),
},
{
"Pipe should pipe via the provided operators",
Of(1, 2, 3).
Pipe(
Pipe(
Assert(1, 2, 3),
),
),
},
{
"ToArray should append all emitted values to slice and emit once",
Of(1, 2, 3).
Pipe(
ToArray(),
Assert([]Value{1, 2, 3}),
),
},
{
"Last should emit the last value",
Of(1, 2, 3).
Pipe(
Last(),
Assert(3),
),
},
{
"Reduce should apply accumulator func to values",
Range(0, 5).
Pipe(
Reduce(func(acc, value Value) Value {
return acc.(int) + value.(int)
}, nil),
Assert(15),
),
},
{
"Scan should apply accumulator func to values and emit on each",
Range(0, 5).
Pipe(
Scan(func(acc, value Value) Value {
return acc.(int) + value.(int)
}, nil),
Assert(1, 3, 6, 10, 15),
),
},
{
"Map should apply map func to values",
Range(0, 5).
Pipe(
Map(func(value Value) Value {
return value.(int) * 2
}),
Assert(0, 2, 4, 6, 8, 10),
),
},
{
"MapTo should map all values to the provided value",
Range(0, 5).
Pipe(
MapTo(1),
Assert(1, 1, 1, 1, 1, 1),
),
},
{
"ConcatMap should subscribe to inner observables sequentially",
Of(1, 2, 3).
Pipe(
ConcatMap(func(value Value) Observable {
return Of(value, value, value)
}),
Assert(1, 1, 1, 2, 2, 2, 3, 3, 3),
),
},
{
"ConcatMapTo should subscribe to the provided observable",
Of(1, 2, 3).
Pipe(
ConcatMapTo(
Of(1, 2, 3),
),
Assert(1, 2, 3, 1, 2, 3, 1, 2, 3),
),
},
{
// The effect of this test should be to reverse the values provided
// to Of, as each is delayed according to it's value in milliseconds.
"MergeMap should subscribe to inner observables in parallel",
Of(300, 200, 100).
Pipe(
MergeMap(func(value Value) Observable {
return Of(value).Pipe(
Delay(time.Duration(value.(int)) * time.Millisecond),
)
}),
Assert(100, 200, 300),
),
},
{
"SwitchMap should subscribe to inner observable and cancel if pending",
Range(0, 10).
Pipe(
SwitchMap(func(value Value) Observable {
return Of(value).
Pipe(
Delay(300 * time.Millisecond),
)
}),
Assert(10),
),
},
{
"BehaviorSubject should start with the provided value",
NewBehaviorSubject("start").
Pipe(
Take(1),
Assert("start"),
),
},
{
"PairWise should emit the last and current value",
Range(0, 4).Pipe(
PairWise(),
Assert([]Value{0, 1}, []Value{1, 2}, []Value{2, 3}, []Value{3, 4}),
),
},
{
"Buffer should emit the a slice of values",
Range(0, 6).Pipe(
Buffer(3),
Assert([]Value{0, 1, 2}, []Value{3, 4, 5}),
),
},
{
"TakeUntil should take values until the provided Observable emits",
Interval(40*time.Millisecond).
Pipe(
TakeUntil(
Interval(100*time.Millisecond),
),
Assert(0, 1),
),
},
{
"TakeWhile should take values until the provided fn returns false (inclusive)",
Range(1, 5).
Pipe(
TakeWhile(func(v Value) bool { return v.(int) < 3 }, true),
Assert(1, 2, 3),
),
},
{
"TakeWhile should take values until the provided fn returns false (exclusive)",
Range(1, 5).
Pipe(
TakeWhile(func(v Value) bool { return v.(int) < 3 }, false),
Assert(1, 2),
),
},
{
"ForkJoin should return an array containing the results in order",
ForkJoin(
Of(0, 1),
Of(0, 2),
Of(0, 3),
Of(0, 4),
).Pipe(
Assert([]Value{1, 2, 3, 4}),
),
},
{
"GroupBy should return an array containing",
Of(1, 1, 1).
Pipe(
GroupBy(func(v Value) Value {
return v
},
ToArray(),
),
).
Pipe(
Assert([]Value{1, 1, 1}),
),
},
{
"Share should share source between multiple subscribers",
Of(1, 2, 3).
Pipe(
Share(),
Assert(1, 2, 3),
),
},
{
"IgnoreElements should ignore all elements",
Of(1, 2, 3).
Pipe(
IgnoreElements(),
Assert(),
),
},
{
"DistinctUntilChanged should only emit when the current value is different than the last.",
Of(1, 1, 2, 2, 3, 4, 5, 5).
Pipe(
DistinctUntilChanged(DeepEqual),
Assert(1, 2, 3, 4, 5),
),
},
{
"Distinct should emit only the distinct items",
Of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6).
Pipe(
Distinct(Sprint),
Assert(1, 2, 3, 4, 5, 6),
),
},
{
"Catch should catch an error",
Of(1).
Pipe(
ConcatMap(func(val Value) Observable {
return Throw(errors.New("err"))
}),
Catch(func(e error) (Value, error) {
if reflect.DeepEqual(e, errors.New("err")) {
return e, nil
}
return nil, e
}),
Assert(errors.New("err")),
),
},
{
"Retry should retry the observable on receiving an error",
Of(1, 2, 3).
Pipe(
ConcatMap(func(val Value) Observable {
if val.(int) > 2 {
return Throw(errors.New("err"))
}
return Of(val)
}),
Retry(2),
Catch(func(e error) (Value, error) {
if reflect.DeepEqual(e, errors.New("err")) {
return e, nil
}
return nil, e
}),
Assert(1, 2, 1, 2, 1, 2, errors.New("err")),
),
},
{
"ExhaustMap should map to inner observable, ignore other values until that observable completes.",
Interval(time.Millisecond).
Pipe(
Take(2),
ExhaustMap(func(val Value) Observable {
return Of(1, 2, 3).Pipe(
Delay(10 * time.Millisecond),
)
}),
Assert(1, 2, 3),
),
},
{
"Timeout should timeout if the observable does not emit within the provided timeout duration.",
Interval(time.Second).
Pipe(
Take(3),
Timeout(time.Millisecond),
Assert(),
),
},
}
func TestObservables(t *testing.T) {
for _, tc := range testCases {
tc.Subscribe(
OnNext(func(v Value) {
t.Logf("SUCCESS `%s` -> %v", tc.name, v)
}).OnErr(func(e error) {
t.Errorf("FAILED `%s` -> %v", tc.name, e)
}),
).Wait()
}
}