-
Notifications
You must be signed in to change notification settings - Fork 1
/
operators_test.go
325 lines (266 loc) · 7.96 KB
/
operators_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package stream_test
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
. "github.com/cilium/stream"
)
func TestMap(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
double := func(x int) int { return x * 2 }
// 1. mapping a non-empty source
{
src := Range(0, 5)
src = Map(src, double)
result, err := ToSlice(ctx, src)
assertNil(t, "case 1", err)
assertSlice(t, "case 1", []int{0, 2, 4, 6, 8}, result)
}
// 2. mapping an empty source
{
src := Map(Empty[int](), double)
result, err := ToSlice(ctx, src)
assertNil(t, "case 2", err)
assertSlice(t, "case 2", []int{}, result)
}
// 3. cancelled context
checkCancelled(t, "case 3", Map(Range(0, 100), double))
}
func TestFilter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
isOdd := func(x int) bool { return x%2 != 0 }
// 1. filtering a non-empty source
{
src := Range(0, 5)
src = Filter(src, isOdd)
result, err := ToSlice(ctx, src)
assertNil(t, "case 1", err)
assertSlice(t, "case 1", []int{1, 3}, result)
}
// 2. filtering an empty source
{
src := Filter(Empty[int](), isOdd)
result, err := ToSlice(ctx, src)
assertNil(t, "case 2", err)
assertSlice(t, "case 2", []int{}, result)
}
// 3. cancelled context
checkCancelled(t, "case 3", Filter(Range(0, 100), isOdd))
}
func TestReduce(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sum := func(result, x int) int { return result + x }
// 1. Reducing a non-empty source
{
src := Range(0, 5)
src = Reduce(src, 0, sum)
result, err := ToSlice(ctx, src)
assertNil(t, "case 1", err)
assertSlice(t, "case 1", []int{0 + 0 + 1 + 2 + 3 + 4}, result)
}
// 2. Reducing an empty source
{
src := Reduce(Empty[int](), 0, sum)
result, err := ToSlice(ctx, src)
assertNil(t, "case 2", err)
assertSlice(t, "case 2", []int{0}, result)
}
// 3. cancelled context
checkCancelled(t, "case 3", Reduce(Range(0, 100), 0, sum))
}
func TestThrottle(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
item, err := First(ctx, Throttle(Range(42, 1000), 100.0, 1))
assertNil(t, "First", err)
if item != 42 {
t.Fatalf("expected 42, got %d", item)
}
}
func TestFlatMap(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
negated := func(x int) Observable[int] {
return FromSlice([]int{x, -x})
}
// 1. mapping a non-empty source
{
src := Range(0, 3)
src = FlatMap(src, negated)
result, err := ToSlice(ctx, src)
assertNil(t, "case 1", err)
assertSlice(t, "case 1", []int{0, 0, 1, -1, 2, -2}, result)
}
// 2. mapping an empty source
{
src := FlatMap(Empty[int](), negated)
result, err := ToSlice(ctx, src)
assertNil(t, "case 2", err)
assertSlice(t, "case 2", []int{}, result)
}
// 3. cancelled context
checkCancelled(t, "case 3", FlatMap(Range(0, 100), negated))
}
func TestConcat(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 1. successful case
res1, err := ToSlice(ctx, Concat(Just(1), Just(2), Just(3)))
if err != nil {
t.Fatalf("case 1 errored: %s", err)
}
assertSlice(t, "case 1", res1, []int{1, 2, 3})
// 2. test cancelled concat
checkCancelled(t, "case 2", Concat(Just(1), Stuck[int]()))
// 3. test empty concat
res3, err := ToSlice(ctx, Concat[int]())
if err != nil {
t.Fatalf("case 3 errored: %s", err)
}
assertSlice(t, "case 3", []int{}, res3)
}
func TestRetry(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var (
err1 = errors.New("err1")
err2 = errors.New("err2")
)
emit, complete, obs := fromCallback[int](1)
shouldRetry := func(err error) bool {
return errors.Is(err, err1)
}
// Retry if error is 'err1', otherwise stop.
obs = Retry(obs, shouldRetry)
errs := make(chan error)
items := ToChannel(ctx, obs, WithErrorChan(errs))
emit(1)
if item := <-items; item != 1 {
t.Fatalf("expected 1, got %d", item)
}
emit(2)
if item := <-items; item != 2 {
t.Fatalf("expected 2, got %d", item)
}
complete(err1) // this should be retried
emit(3)
if item := <-items; item != 3 {
t.Fatalf("expected 3, got %d", item)
}
complete(err2) // this should stop the observing
if item, ok := <-items; ok {
t.Fatalf("expected items channel to be closed, got item %d", item)
}
if err := <-errs; !errors.Is(err, err2) {
t.Fatalf("expected error %s, got %s", err2, err)
}
}
func TestRetryFuncs(t *testing.T) {
err := errors.New("err")
// Retry 10 times with exponential backoff up to 10ms.
var retry RetryFunc
retry = AlwaysRetry
retry = BackoffRetry(retry, time.Millisecond, 10*time.Millisecond)
retry = LimitRetries(retry, 6)
t0 := time.Now()
for i := 0; i < 10; i++ {
if i < 6 {
if !retry(err) {
t.Fatalf("expected retry to succeed at attempt %d", i)
}
} else {
if retry(err) {
t.Fatalf("expected retry to fail at attempt %d", i)
}
}
}
tdiff := time.Since(t0)
expectedDiff := time.Duration(1+2+4+8+10+10) * time.Millisecond
if tdiff < expectedDiff || tdiff > 2*expectedDiff {
t.Fatalf("expected backoff duration to be ~%s, it was %s", expectedDiff, tdiff)
}
}
func TestDistict(t *testing.T) {
items := FromSlice([]int{1, 2, 3, 3, 2, 1})
result, err := ToSlice(context.TODO(), Distinct(items))
assertNil(t, "ToSlice+Distict", err)
assertSlice(t, "Distinct", []int{1, 2, 3, 2, 1}, result)
}
func TestDebounce(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int, 16)
defer close(in)
src := FromChannel(in)
src = Debounce(src, 5*time.Millisecond)
errs := make(chan error)
defer close(errs)
out := ToChannel(ctx, src, WithErrorChan(errs))
in <- -1
x := <-out // first item not delayed
if x != -1 {
t.Fatalf("expected -1, got %d", x)
}
// Emit 10 batches of 3 items. We should only
// observe the last item of each batch.
for i := 0; i < 10*3; i += 3 {
in <- i
in <- i + 1
in <- i + 2
time.Sleep(10 * time.Millisecond)
x := <-out
if x != i+2 {
t.Fatalf("expected %d, got %d", i+2, x)
}
}
// Wait until the debounce period expired before canceling it, to make
// sure we don't emit any spurious events at that point (the test would
// block, as the out channel is unbuffered, and no one is receiving).
time.Sleep(10 * time.Millisecond)
cancel()
err := <-errs
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected Canceled error, got %s", err)
}
}
func TestBuffer(t *testing.T) {
// Buffer range 1..8 into buckets of at most 3 items
bufferItem := func(buf []int, x int) []int { return append(buf, x) }
src := Buffer(Range(1, 9), 3, time.Millisecond, bufferItem)
buckets, err := ToSlice(context.TODO(), src)
assert.NoError(t, err, "ToSlice")
assert.True(t, len(buckets) > 2, "expected at least 3 buckets")
// Concat the buckets. Since Buffer is time-based we don't know exactly
// how the buckets will look like.
all := []int{}
for _, bucket := range buckets {
assert.True(t, len(bucket) > 0, "expected non-empty buckets")
all = append(all, bucket...)
}
assert.EqualValues(t, []int{1, 2, 3, 4, 5, 6, 7, 8}, all)
// Buffer empty stream
src = Buffer(Empty[int](), 10, time.Millisecond, bufferItem)
buckets, err = ToSlice(context.TODO(), src)
assert.NoError(t, err, "ToSlice")
assert.Len(t, buckets, 0, "expected no buckets with Empty")
// Buffer an errored stream
testError := errors.New("error")
src = Buffer(Error[int](testError), 10, time.Millisecond, bufferItem)
buckets, err = ToSlice(context.TODO(), src)
assert.ErrorIs(t, err, testError)
assert.Len(t, buckets, 0, "expected no buckets with Error")
// Buffer a cancelled stream
ctx, cancel := context.WithCancel(context.Background())
cancel()
src = Buffer(Stuck[int](), 10, time.Millisecond, bufferItem)
buckets, err = ToSlice(ctx, src)
assert.ErrorIs(t, err, context.Canceled)
assert.Len(t, buckets, 0, "expected no buckets with Stuck and canceled")
}