forked from ReactiveX/RxGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observable_operator_random_test.go
139 lines (129 loc) · 3.7 KB
/
observable_operator_random_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
// +build !all
package rxgo
import (
"context"
"errors"
"fmt"
"math/rand"
"testing"
"time"
"go.uber.org/goleak"
)
const maxSleepNs = 10_000_000 // 10 ms
// TODO Keep enriching tests
func TestLeak(t *testing.T) {
var (
count = 100
fooErr = errors.New("")
)
observables := map[string]func(context.Context) Observable{
"Amb": func(ctx context.Context) Observable {
obs := FromChannel(make(chan Item), WithContext(ctx))
return Amb([]Observable{obs}, WithContext(ctx))
},
"CombineLatest": func(ctx context.Context) Observable {
return CombineLatest(func(i ...interface{}) interface{} {
sum := 0
for _, v := range i {
if v == nil {
continue
}
sum += v.(int)
}
return sum
}, []Observable{
Just(1, 2)(),
Just(10, 11)(),
})
},
"Concat": func(ctx context.Context) Observable {
return Concat([]Observable{
Just(1, 2, 3)(),
Just(4, 5, 6)(),
})
},
"FromChannel": func(ctx context.Context) Observable {
return FromChannel(getChannel(ctx), WithContext(ctx))
},
"FromEventSource": func(ctx context.Context) Observable {
return FromEventSource(getChannel(ctx), WithContext(ctx))
},
}
actions := map[string]func(context.Context, Observable){
"All": func(ctx context.Context, obs Observable) {
obs.All(func(_ interface{}) bool {
return true
}, WithContext(ctx))
},
"Average": func(ctx context.Context, obs Observable) {
obs.AverageInt(WithContext(ctx))
},
"BufferWithTime": func(ctx context.Context, obs Observable) {
obs.BufferWithTime(WithDuration(time.Millisecond), WithContext(ctx))
},
"Connect": func(ctx context.Context, obs Observable) {
obs.Connect(ctx)
},
"Contains": func(ctx context.Context, obs Observable) {
obs.Contains(func(i interface{}) bool {
return i == 2
}, WithContext(ctx))
},
"For each": func(_ context.Context, obs Observable) {
obs.ForEach(func(_ interface{}) {}, func(_ error) {}, func() {})
},
}
defer goleak.VerifyNone(t)
for testObservable, factory := range observables {
for testAction, action := range actions {
for i := 0; i < count; i++ {
waitTime := randomTime()
factory := factory
action := action
t.Run(fmt.Sprintf("%s - %s - %v - single", testObservable, testAction, waitTime), func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
action(ctx, factory(ctx))
})
t.Run(fmt.Sprintf("%s - %s - %v - composed", testObservable, testAction, waitTime), func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
action(ctx, factory(ctx).Map(func(_ context.Context, i interface{}) (interface{}, error) {
return i, nil
}))
})
t.Run(fmt.Sprintf("%s - %s - %v - erritem", testObservable, testAction, waitTime), func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
action(ctx, factory(ctx).Map(func(_ context.Context, i interface{}) (interface{}, error) {
return nil, fooErr
}))
})
}
t.Run(fmt.Sprintf("%s - %s - already cancelled", testObservable, testAction), func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
cancel()
action(ctx, factory(ctx))
})
}
}
}
func getChannel(ctx context.Context) chan Item {
ch := make(chan Item, 3)
go func() {
time.Sleep(randomTime())
Of(1).SendContext(ctx, ch)
time.Sleep(randomTime())
Of(2).SendContext(ctx, ch)
time.Sleep(randomTime())
Of(3).SendContext(ctx, ch)
}()
return ch
}
func randomTime() time.Duration {
return time.Duration(rand.Intn(maxSleepNs)) * time.Nanosecond
}