forked from ReactiveX/RxGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject_test.go
182 lines (144 loc) · 3.58 KB
/
subject_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
package rxgo
import (
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestDefaultOptions verifies that multiple observers receive the same number of items
func TestDefaultOptions(t *testing.T) {
subject := NewSubject()
itemCount1 := 0
_, obs1 := subject.Subscribe()
obs1.DoOnNext(func(i interface{}) {
itemCount1++
})
itemCount2 := 0
_, obs2 := subject.Subscribe()
obs2.DoOnNext(func(i interface{}) {
itemCount2++
})
items := 10
for i := 0; i < items; i++ {
subject.Next(i)
}
// short delay to give subscribers time to process last element
time.Sleep(10 * time.Millisecond)
assert.Equal(t, items, itemCount1)
assert.Equal(t, items, itemCount2)
}
// TestBackPressure verifies messages are dropped with a blocked observer
func TestBackPressure(t *testing.T) {
subject := NewSubject(WithBackPressureStrategy(Drop))
_, obs1 := subject.Subscribe()
_, obs2 := subject.Subscribe()
// first observer starts receiving
itemCount1 := 0
obs1.DoOnNext(func(i interface{}) {
itemCount1++
})
var wg sync.WaitGroup
items := 5
// send first batch
wg.Add(1)
go func() {
for i := 0; i < items; i++ {
// slow down to avoid dropped messages
time.Sleep(1 * time.Millisecond)
subject.Next(i)
}
wg.Done()
}()
// wait for first batch of messages sent
wg.Wait()
// short delay to give go routines time to process last element
time.Sleep(10 * time.Millisecond)
// first observer should have received all items
assert.Equal(t, items, itemCount1)
// second observer starts receiving
itemCount2 := 0
obs2.DoOnNext(func(i interface{}) {
itemCount2++
})
// send second batch
wg.Add(1)
go func() {
for i := 0; i < items; i++ {
// slow down to avoid dropped messages
time.Sleep(1 * time.Millisecond)
subject.Next(i + items)
}
wg.Done()
}()
// wait for second batch of messages sent
wg.Wait()
// short delay to give go routines time to process last element
time.Sleep(10 * time.Millisecond)
assert.Equal(t, 2*items, itemCount1)
assert.Equal(t, items, itemCount2)
}
// TestSubscriberBuffer verify no messages dropped with buffer attached
func TestSubscriberBuffer(t *testing.T) {
subject := NewSubject(WithBufferedChannel(10), WithBackPressureStrategy(Drop))
_, obs := subject.Subscribe()
items := 10
itemCount := 0
obs.DoOnNext(func(i interface{}) {
itemCount++
})
for i := 0; i < items; i++ {
// short delay to let subscriber catch up
time.Sleep(1 * time.Millisecond)
subject.Next(i)
}
// short delay to give go routines time to process last element
time.Sleep(10 * time.Millisecond)
assert.Equal(t, items, itemCount)
}
func TestUnsubscribe(t *testing.T) {
subject := NewSubject()
sub, obs := subject.Subscribe()
items := 10
itemCount := 0
obs.DoOnNext(func(i interface{}) {
itemCount += 1
})
for i := 0; i < items; i++ {
subject.Next(i)
}
// items after unsubscribe will be lost
sub.Unsubscribe()
for i := 0; i < 5; i++ {
subject.Next(i)
}
assert.Equal(t, items, itemCount)
}
func TestReceiveError(t *testing.T) {
subject := NewSubject()
_, obs := subject.Subscribe()
err := errors.New("test")
obs.DoOnNext(func(i interface{}) {
fmt.Printf("next %v", i)
})
var errRcvd error
obs.DoOnError(func(e error) {
fmt.Printf("error received: %v", e)
errRcvd = e
})
subject.Error(err)
time.Sleep(10 * time.Millisecond)
assert.Equal(t, err, errRcvd)
}
func TestCompletion(t *testing.T) {
subject := NewSubject()
_, obs := subject.Subscribe()
obs.DoOnNext(func(i interface{}) {
fmt.Printf("next %v", i)
})
obs.DoOnCompleted(func() {
fmt.Println("completed")
})
subject.Complete()
}