This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
pubsub_test.go
439 lines (391 loc) · 11.8 KB
/
pubsub_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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright (c) 2013 Project Iris. All rights reserved.
//
// The current language binding is an official support library of the Iris
// cloud messaging framework, and as such, the same licensing terms apply.
// For details please see http://iris.karalabe.com/downloads#License
package iris
import (
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/project-iris/iris/pool"
)
// Service handler for the publish/subscribe tests.
type publishTestServiceHandler struct {
conn *Connection
}
func (p *publishTestServiceHandler) Init(conn *Connection) error { p.conn = conn; return nil }
func (p *publishTestServiceHandler) HandleBroadcast(msg []byte) { panic("not implemented") }
func (p *publishTestServiceHandler) HandleRequest(req []byte) ([]byte, error) {
panic("not implemented")
}
func (p *publishTestServiceHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (p *publishTestServiceHandler) HandleDrop(reason error) { panic("not implemented") }
// Topic handler for the publish/subscribe tests.
type publishTestTopicHandler struct {
delivers chan []byte
}
func (p *publishTestTopicHandler) HandleEvent(event []byte) { p.delivers <- event }
// Multiple connections subscribe to the same batch of topics and publish to all.
func TestPublish(t *testing.T) {
// Test specific configurations
conf := struct {
clients int
servers int
topics int
events int
}{5, 5, 7, 15}
// Pre-generate the topic names
topics := make([]string, conf.topics)
for i := 0; i < conf.topics; i++ {
topics[i] = fmt.Sprintf("%s-%d", config.topic, i)
}
barrier := newBarrier(conf.clients + conf.servers)
shutdown := new(sync.WaitGroup)
// Start up the concurrent publishing clients
for i := 0; i < conf.clients; i++ {
shutdown.Add(1)
go func(client int) {
defer shutdown.Done()
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
barrier.Exit(fmt.Errorf("connection failed: %v", err))
return
}
defer conn.Close()
// Subscribe to the batch of topics
hands := []*publishTestTopicHandler{}
for _, topic := range topics {
hand := &publishTestTopicHandler{
delivers: make(chan []byte, (conf.clients+conf.servers)*conf.events),
}
if err := conn.Subscribe(topic, hand, nil); err != nil {
barrier.Exit(fmt.Errorf("client subscription failed: %v", err))
return
}
hands = append(hands, hand)
defer func(topic string) {
conn.Unsubscribe(topic)
time.Sleep(100 * time.Millisecond)
}(topic)
}
time.Sleep(100 * time.Millisecond)
barrier.Sync()
// Publish to all subscribers
for i := 0; i < conf.events; i++ {
event := fmt.Sprintf("client #%d, event %d", client, i)
for _, topic := range topics {
if err := conn.Publish(topic, []byte(event)); err != nil {
barrier.Exit(fmt.Errorf("client publish failed: %v", err))
return
}
}
}
barrier.Sync()
// Verify all the topic deliveries
if err := publishVerifyEvents(conf.clients, conf.servers, conf.events, hands); err != nil {
barrier.Exit(err)
return
}
barrier.Exit(nil)
}(i)
}
// Start up the concurrent publishing services
for i := 0; i < conf.servers; i++ {
shutdown.Add(1)
go func(server int) {
defer shutdown.Done()
// Create the service handler
handler := new(publishTestServiceHandler)
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
barrier.Exit(fmt.Errorf("registration failed: %v", err))
return
}
defer serv.Unregister()
// Subscribe to the batch of topics
hands := []*publishTestTopicHandler{}
for _, topic := range topics {
hand := &publishTestTopicHandler{
delivers: make(chan []byte, (conf.clients+conf.servers)*conf.events),
}
if err := handler.conn.Subscribe(topic, hand, nil); err != nil {
barrier.Exit(fmt.Errorf("service subscription failed: %v", err))
return
}
hands = append(hands, hand)
defer func(topic string) {
handler.conn.Unsubscribe(topic)
time.Sleep(100 * time.Millisecond)
}(topic)
}
time.Sleep(100 * time.Millisecond)
barrier.Sync()
// Publish to all subscribers
for i := 0; i < conf.events; i++ {
event := fmt.Sprintf("server #%d, event %d", server, i)
for _, topic := range topics {
if err := handler.conn.Publish(topic, []byte(event)); err != nil {
barrier.Exit(fmt.Errorf("server publish failed: %v", err))
return
}
}
}
barrier.Sync()
// Verify all the topic deliveries
if err := publishVerifyEvents(conf.clients, conf.servers, conf.events, hands); err != nil {
barrier.Exit(err)
return
}
barrier.Exit(nil)
}(i)
}
// Schedule the parallel operations
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("startup phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("publishing phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("verification phase failed: %v.", errs)
}
// Make sure all children terminated
shutdown.Wait()
}
// Verifies the delivered topic events.
func publishVerifyEvents(clients, servers, publishes int, hands []*publishTestTopicHandler) error {
// Verify each topic handler separately
for i := 0; i < len(hands); i++ {
// Retrieve all the published events
events := make(map[string]struct{})
for j := 0; j < (clients+servers)*publishes; j++ {
select {
case event := <-hands[i].delivers:
events[string(event)] = struct{}{}
case <-time.After(time.Second):
return errors.New("event retrieve timeout")
}
}
// Verify all the individual events
for j := 0; j < clients; j++ {
for k := 0; k < publishes; k++ {
msg := fmt.Sprintf("client #%d, event %d", j, k)
if _, ok := events[msg]; !ok {
return fmt.Errorf("event not found: %s", msg)
}
delete(events, msg)
}
}
for j := 0; j < servers; j++ {
for k := 0; k < publishes; k++ {
msg := fmt.Sprintf("server #%d, event %d", j, k)
if _, ok := events[msg]; !ok {
return fmt.Errorf("event not found: %s", msg)
}
delete(events, msg)
}
}
}
return nil
}
// Topic handler for the publish/subscribe limit tests.
type publishLimitTestTopicHandler struct {
delivers chan []byte
sleep time.Duration
}
func (p *publishLimitTestTopicHandler) HandleEvent(event []byte) {
time.Sleep(p.sleep)
p.delivers <- event
}
// Tests the topic subscription thread limitation.
func TestPublishThreadLimit(t *testing.T) {
// Test specific configurations
conf := struct {
messages int
sleep time.Duration
}{4, 100 * time.Millisecond}
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
t.Fatalf("connection failed: %v", err)
}
defer conn.Close()
// Subscribe to a topic and wait for state propagation
handler := &publishLimitTestTopicHandler{
delivers: make(chan []byte, conf.messages),
sleep: conf.sleep,
}
limits := &TopicLimits{EventThreads: 1}
if err := conn.Subscribe(config.topic, handler, limits); err != nil {
t.Fatalf("subscription failed: %v", err)
}
defer conn.Unsubscribe(config.topic)
time.Sleep(100 * time.Millisecond)
// Send a few publishes
for i := 0; i < conf.messages; i++ {
if err := conn.Publish(config.topic, []byte{byte(i)}); err != nil {
t.Fatalf("event publish failed: %v.", err)
}
}
// Wait for half time and verify that only half was processed
time.Sleep(time.Duration(conf.messages/2)*conf.sleep + conf.sleep/2)
for i := 0; i < conf.messages/2; i++ {
select {
case <-handler.delivers:
default:
t.Errorf("event #%d not received", i)
}
}
select {
case <-handler.delivers:
t.Errorf("additional event received")
default:
}
}
// Tests the subscription memory limitation.
func TestPublishMemoryLimit(t *testing.T) {
// Test specific configurations
conf := struct {
messages int
sleep time.Duration
}{4, 100 * time.Millisecond}
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
t.Fatalf("connection failed: %v", err)
}
defer conn.Close()
// Subscribe to a topic and wait for state propagation
handler := &publishTestTopicHandler{
delivers: make(chan []byte, conf.messages),
}
limits := &TopicLimits{EventMemory: 1}
if err := conn.Subscribe(config.topic, handler, limits); err != nil {
t.Fatalf("subscription failed: %v", err)
}
defer conn.Unsubscribe(config.topic)
time.Sleep(100 * time.Millisecond)
// Check that a 1 byte publish passes
if err := conn.Publish(config.topic, []byte{0x00}); err != nil {
t.Fatalf("small publish failed: %v.", err)
}
time.Sleep(time.Millisecond)
select {
case <-handler.delivers:
default:
t.Fatalf("small publish not received.")
}
// Check that a 2 byte publish is dropped
if err := conn.Publish(config.topic, []byte{0x00, 0x00}); err != nil {
t.Fatalf("large publish failed: %v.", err)
}
time.Sleep(time.Millisecond)
select {
case <-handler.delivers:
t.Fatalf("large publish received.")
default:
}
// Check that space freed gets replenished
if err := conn.Publish(config.topic, []byte{0x00}); err != nil {
t.Fatalf("second small publish failed: %v.", err)
}
time.Sleep(time.Millisecond)
select {
case <-handler.delivers:
default:
t.Fatalf("second small publish not received.")
}
}
// Benchmarks the latency of a single publish operation.
func BenchmarkPublishLatency(b *testing.B) {
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
b.Fatalf("connection failed: %v", err)
}
defer conn.Close()
// Subscribe to a topic and wait for state propagation
handler := &publishTestTopicHandler{
delivers: make(chan []byte, b.N),
}
if err := conn.Subscribe(config.topic, handler, nil); err != nil {
b.Fatalf("subscription failed: %v", err)
}
defer conn.Unsubscribe(config.topic)
time.Sleep(100 * time.Millisecond)
// Reset timer and time sync publish
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := conn.Publish(config.topic, []byte{byte(i)}); err != nil {
b.Fatalf("failed to publish: %v.", err)
}
<-handler.delivers
}
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}
// Benchmarks the throughput of a stream of concurrent publishes.
func BenchmarkPublishThroughput1Threads(b *testing.B) {
benchmarkPublishThroughput(1, b)
}
func BenchmarkPublishThroughput2Threads(b *testing.B) {
benchmarkPublishThroughput(2, b)
}
func BenchmarkPublishThroughput4Threads(b *testing.B) {
benchmarkPublishThroughput(4, b)
}
func BenchmarkPublishThroughput8Threads(b *testing.B) {
benchmarkPublishThroughput(8, b)
}
func BenchmarkPublishThroughput16Threads(b *testing.B) {
benchmarkPublishThroughput(16, b)
}
func BenchmarkPublishThroughput32Threads(b *testing.B) {
benchmarkPublishThroughput(32, b)
}
func BenchmarkPublishThroughput64Threads(b *testing.B) {
benchmarkPublishThroughput(64, b)
}
func BenchmarkPublishThroughput128Threads(b *testing.B) {
benchmarkPublishThroughput(128, b)
}
func benchmarkPublishThroughput(threads int, b *testing.B) {
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
b.Fatalf("connection failed: %v", err)
}
defer conn.Close()
// Subscribe to a topic and wait for state propagation
handler := &publishTestTopicHandler{
delivers: make(chan []byte, b.N),
}
if err := conn.Subscribe(config.topic, handler, nil); err != nil {
b.Fatalf("subscription failed: %v", err)
}
defer conn.Unsubscribe(config.topic)
time.Sleep(100 * time.Millisecond)
// Create the thread pool with the concurrent publishes
workers := pool.NewThreadPool(threads)
for i := 0; i < b.N; i++ {
workers.Schedule(func() {
if err := conn.Publish(config.topic, []byte{byte(i)}); err != nil {
b.Fatalf("failed to publish: %v.", err)
}
})
}
// Reset timer and benchmark the message transfer
b.ResetTimer()
workers.Start()
for i := 0; i < b.N; i++ {
<-handler.delivers
}
workers.Terminate(false)
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}