forked from nats-io/nats.go
/
netchan_test.go
283 lines (215 loc) · 5.86 KB
/
netchan_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
// Copyright 2013-2014 Apcera Inc. All rights reserved.
package nats
import (
"runtime"
"testing"
"time"
)
func TestBadChan(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
if err := ec.BindSendChan("foo", "not a chan"); err == nil {
t.Fatalf("Expected an Error when sending a non-channel\n")
}
if _, err := ec.BindRecvChan("foo", "not a chan"); err == nil {
t.Fatalf("Expected an Error when sending a non-channel\n")
}
if err := ec.BindSendChan("foo", "not a chan"); err != ErrChanArg {
t.Fatalf("Expected an ErrChanArg when sending a non-channel\n")
}
if _, err := ec.BindRecvChan("foo", "not a chan"); err != ErrChanArg {
t.Fatalf("Expected an ErrChanArg when sending a non-channel\n")
}
}
func TestSimpleSendChan(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
recv := make(chan bool)
numSent := int32(22)
ch := make(chan int32)
if err := ec.BindSendChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a send channel: %v\n", err)
}
ec.Subscribe("foo", func(num int32) {
if num != numSent {
t.Fatalf("Failed to receive correct value: %d vs %d\n", num, numSent)
}
recv <- true
})
// Send to 'foo'
ch <- numSent
if e := wait(recv); e != nil {
if ec.LastError() != nil {
e = ec.LastError()
}
t.Fatalf("Did not receive the message: %s", e)
}
close(ch)
}
func TestFailedChannelSend(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
nc := ec.Conn
ch := make(chan bool)
wch := make(chan bool)
nc.Opts.AsyncErrorCB = func(c *Conn, s *Subscription, e error) {
wch <- true
}
if err := ec.BindSendChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a receive channel: %v\n", err)
}
nc.Flush()
nc.Close()
// This should trigger an async error.
ch <- true
if e := wait(wch); e != nil {
t.Fatal("Failed to call async err handler")
}
}
func TestSimpleRecvChan(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
numSent := int32(22)
ch := make(chan int32)
if _, err := ec.BindRecvChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a receive channel: %v\n", err)
}
ec.Publish("foo", numSent)
// Receive from 'foo'
select {
case num := <-ch:
if num != numSent {
t.Fatalf("Failed to receive correct value: %d vs %d\n", num, numSent)
}
case <-time.After(1 * time.Second):
t.Fatalf("Failed to receive a value, timed-out\n")
}
close(ch)
}
func TestQueueRecvChan(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
numSent := int32(22)
ch := make(chan int32)
if _, err := ec.BindRecvQueueChan("foo", "bar", ch); err != nil {
t.Fatalf("Failed to bind to a queue receive channel: %v\n", err)
}
ec.Publish("foo", numSent)
// Receive from 'foo'
select {
case num := <-ch:
if num != numSent {
t.Fatalf("Failed to receive correct value: %d vs %d\n", num, numSent)
}
case <-time.After(1 * time.Second):
t.Fatalf("Failed to receive a value, timed-out\n")
}
close(ch)
}
func TestDecoderErrRecvChan(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
nc := ec.Conn
wch := make(chan bool)
nc.Opts.AsyncErrorCB = func(c *Conn, s *Subscription, e error) {
wch <- true
}
ch := make(chan *int32)
if _, err := ec.BindRecvChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a send channel: %v\n", err)
}
ec.Publish("foo", "Hello World")
if e := wait(wch); e != nil {
t.Fatal("Failed to call async err handler")
}
}
func TestRecvChanPanicOnClosedChan(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
ch := make(chan int)
if _, err := ec.BindRecvChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a send channel: %v\n", err)
}
close(ch)
ec.Publish("foo", 22)
ec.Flush()
}
func TestRecvChanAsyncLeakGoRoutines(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
before := runtime.NumGoroutine()
ch := make(chan int)
if _, err := ec.BindRecvChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a send channel: %v\n", err)
}
// Close the receive Channel
close(ch)
// The publish will trugger the close and shutdown of the Go routines
ec.Publish("foo", 22)
ec.Flush()
time.Sleep(50 * time.Millisecond)
after := runtime.NumGoroutine()
if before != after {
t.Fatalf("Leaked Go routine(s) : %d, closing channel should have closed them\n", after-before)
}
}
func TestRecvChanLeakGoRoutines(t *testing.T) {
ec := NewEConn(t)
defer ec.Close()
before := runtime.NumGoroutine()
ch := make(chan int)
sub, err := ec.BindRecvChan("foo", ch)
if err != nil {
t.Fatalf("Failed to bind to a send channel: %v\n", err)
}
sub.Unsubscribe()
// Sleep a bit to wait for the Go routine to exit.
time.Sleep(100 * time.Millisecond)
after := runtime.NumGoroutine()
if before != after {
t.Fatalf("Leaked Go routine(s) : %d, closing channel should have closed them\n", after-before)
}
}
func TestRecvChanMultipleMessages(t *testing.T) {
// Make sure we can receive more than one message.
// In response to #25, which is a bug from fixing #22.
ec := NewEConn(t)
defer ec.Close()
// Num to send, should == len of messages queued.
size := 10
ch := make(chan int, size)
if _, err := ec.BindRecvChan("foo", ch); err != nil {
t.Fatalf("Failed to bind to a send channel: %v\n", err)
}
for i := 0; i < size; i++ {
ec.Publish("foo", 22)
}
ec.Flush()
time.Sleep(10 * time.Millisecond)
if lch := len(ch); lch != size {
t.Fatalf("Expected %d messages queued, got %d.", size, lch)
}
}
func BenchmarkPublishSpeedViaChan(b *testing.B) {
b.StopTimer()
server := startServer(b, DefaultPort, "")
defer server.stopServer()
nc, err := Connect(DefaultURL)
if err != nil {
b.Fatalf("Could not connect: %v\n", err)
}
ec, err := NewEncodedConn(nc, "default")
defer ec.Close()
ch := make(chan int32, 1024)
if err := ec.BindSendChan("foo", ch); err != nil {
b.Fatalf("Failed to bind to a send channel: %v\n", err)
}
b.StartTimer()
num := int32(22)
for i := 0; i < b.N; i++ {
ch <- num
}
// Make sure they are all processed.
nc.Flush()
b.StopTimer()
}