forked from zeromq/netmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXPubSubTests.cs
539 lines (417 loc) · 20 KB
/
XPubSubTests.cs
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
using System.Linq;
using System.Threading;
using Xunit;
using NetMQ.Sockets;
// ReSharper disable ExceptionNotDocumented
namespace NetMQ.Tests
{
public class XPubSubTests : IClassFixture<CleanupAfterFixture>
{
public XPubSubTests() => NetMQConfig.Cleanup();
[Fact]
public void TopicPubSub()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame(new byte[] { 1, (byte)'A' });
// let the subscriber connect to the publisher before sending a message
Thread.Sleep(500);
var msg = pub.ReceiveFrameBytes();
Assert.Equal(2, msg.Length);
Assert.Equal(1, msg[0]);
Assert.Equal((int)'A', msg[1]);
pub.SendMoreFrame("A");
pub.SendFrame("Hello");
Assert.Equal("A", sub.ReceiveFrameString(out bool more));
Assert.True(more);
Assert.Equal("Hello", sub.ReceiveFrameString(out more));
Assert.False(more);
}
}
[Fact]
public void Census()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame("Message from subscriber");
// let the subscriber connect to the publisher before sending a message
Thread.Sleep(500);
var txt = pub.ReceiveFrameString();
Assert.Equal("Message from subscriber", txt);
sub.SendFrame(new byte[] { });
var msg = pub.ReceiveFrameBytes();
Assert.True(msg.Length == 0);
}
}
[Fact]
public void SimplePubSub()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame(new byte[] { 1 });
// let the subscriber connect to the publisher before sending a message
Thread.Sleep(500);
pub.SendFrame("Hello");
Assert.Equal("Hello", sub.ReceiveFrameString(out bool more));
Assert.False(more);
}
}
[Fact]
public void NotSubscribed()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
// let the subscriber connect to the publisher before sending a message
Thread.Sleep(500);
pub.SendFrame("Hello");
Assert.False(sub.TrySkipFrame());
}
}
/// <summary>
/// This test trying to reproduce bug #45 NetMQ.zmq.Utils.Realloc broken!
/// </summary>
[Fact]
public void MultipleSubscriptions()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame(new byte[] { 1, (byte)'C' });
sub.SendFrame(new byte[] { 1, (byte)'B' });
sub.SendFrame(new byte[] { 1, (byte)'A' });
sub.SendFrame(new byte[] { 1, (byte)'D' });
sub.SendFrame(new byte[] { 1, (byte)'E' });
Thread.Sleep(500);
sub.SendFrame(new byte[] { 0, (byte)'C' });
sub.SendFrame(new byte[] { 0, (byte)'B' });
sub.SendFrame(new byte[] { 0, (byte)'A' });
sub.SendFrame(new byte[] { 0, (byte)'D' });
sub.SendFrame(new byte[] { 0, (byte)'E' });
Thread.Sleep(500);
}
}
[Fact]
public void MultipleSubscribers()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
using (var sub2 = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame(new byte[] { 1, (byte)'A' });
sub.SendFrame(new byte[] { 1, (byte)'A', (byte)'B' });
sub.SendFrame(new byte[] { 1, (byte)'B' });
sub.SendFrame(new byte[] { 1, (byte)'C' });
sub2.Connect("tcp://127.0.0.1:" + port);
sub2.SendFrame(new byte[] { 1, (byte)'A' });
sub2.SendFrame(new byte[] { 1, (byte)'A', (byte)'B' });
sub2.SendFrame(new byte[] { 1, (byte)'C' });
Thread.Sleep(500);
pub.SendMoreFrame("AB");
pub.SendFrame("1");
// First subscriber is expected to receive the message
Assert.Equal("AB", sub.ReceiveMultipartStrings().First());
// Second subscriber is expected to receive the message
Assert.Equal("AB", sub2.ReceiveMultipartStrings().First());
}
}
[Fact]
public void MultiplePublishers()
{
using (var pub = new XPublisherSocket())
using (var pub2 = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
using (var sub2 = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
var port2 = pub2.BindRandomPort("tcp://127.0.0.1");
// see comments below why verbose option is needed in this test
pub.Options.XPubVerbose = true;
pub2.Options.XPubVerbose = true;
sub.Connect("tcp://127.0.0.1:" + port);
sub.Connect("tcp://127.0.0.1:" + port2);
sub2.Connect("tcp://127.0.0.1:" + port);
sub2.Connect("tcp://127.0.0.1:" + port2);
// should subscribe to both
sub.SendFrame(new byte[] { 1, (byte)'A' });
sub2.SendFrame(new byte[] { 1, (byte)'A' });
Thread.Sleep(500);
var msg = pub.ReceiveFrameString();
Assert.Equal(2, msg.Length);
Assert.Equal(1, msg[0]);
Assert.Equal('A', msg[1]);
var msg2 = pub2.ReceiveFrameBytes();
Assert.Equal(2, msg2.Length);
Assert.Equal(1, msg2[0]);
Assert.Equal((int)'A', msg2[1]);
// Next two blocks (pub(2).Receive) will hang without XPub verbose option:
// sub and sub2 both have sent `.Send(new byte[] { 1, (byte)'A' });` messages
// which are the same, so XPub will discard the second message for .Receive()
// because it is normally used to pass topics upstream.
// (un)subs are done in XPub.cs at line 150-175, quote:
// >> If the subscription is not a duplicate, store it so that it can be
// >> passed to used on next recv call.
// For verbose:
// >> If true, send all subscription messages upstream, not just unique ones
// These options must be set before sub2.Send(new byte[] { 1, (byte)'A' });
// pub.Options.XPubVerbose = true;
// pub2.Options.XPubVerbose = true;
// Note that resending sub2.Send(..) here wont help because XSub won't resent existing subs to XPub - quite sane behavior
// Comment out the verbose options and the next 8 lines and the test will
// still pass, even with non-unique messages from subscribers (see the bottom of the test)
msg = pub.ReceiveFrameString();
Assert.Equal(2, msg.Length);
Assert.Equal(1, msg[0]);
Assert.Equal('A', msg[1]);
msg2 = pub2.ReceiveFrameBytes();
Assert.Equal(2, msg2.Length);
Assert.Equal(1, msg2[0]);
Assert.Equal((int)'A', msg2[1]);
pub.SendMoreFrame("A");
pub.SendFrame("Hello from the first publisher");
Assert.Equal("A", sub.ReceiveFrameString(out bool more));
Assert.True(more);
Assert.Equal("Hello from the first publisher", sub.ReceiveFrameString(out more));
Assert.False(more);
// this returns the result of the latest
// connect - address2, not the source of the message
// This is documented here: http://api.zeromq.org/3-2:zmq-getsockopt
//var ep = sub2.Options.LastEndpoint;
//Assert.Equal(address, ep);
// same for sub2
Assert.Equal("A", sub2.ReceiveFrameString(out more));
Assert.True(more);
Assert.Equal("Hello from the first publisher", sub2.ReceiveFrameString(out more));
Assert.False(more);
pub2.SendMoreFrame("A");
pub2.SendFrame("Hello from the second publisher");
Assert.Equal("A", sub.ReceiveFrameString(out more));
Assert.True(more);
Assert.Equal("Hello from the second publisher", sub.ReceiveFrameString(out more));
Assert.False(more);
Assert.Equal("tcp://127.0.0.1:" + port2, sub2.Options.LastEndpoint);
// same for sub2
Assert.Equal("A", sub2.ReceiveFrameString(out more));
Assert.True(more);
Assert.Equal("Hello from the second publisher", sub2.ReceiveFrameString(out more));
Assert.False(more);
// send both to address and address2
sub.SendFrame("Message from subscriber");
sub2.SendFrame("Message from subscriber 2");
Assert.Equal("Message from subscriber", pub.ReceiveFrameString());
Assert.Equal("Message from subscriber", pub2.ReceiveFrameString());
// Does not hang even though is the same as above, but the first byte is not 1 or 0.
// Won't hang even when messages are equal
Assert.Equal("Message from subscriber 2", pub.ReceiveFrameString());
Assert.Equal("Message from subscriber 2", pub2.ReceiveFrameString());
}
}
[Fact]
public void Unsubscribe()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame(new byte[] { 1, (byte)'A' });
// let the subscriber connect to the publisher before sending a message
Thread.Sleep(500);
pub.SendMoreFrame("A");
pub.SendFrame("Hello");
Assert.Equal("A", sub.ReceiveFrameString(out bool more));
Assert.True(more);
Assert.Equal("Hello", sub.ReceiveFrameString(out more));
Assert.False(more);
sub.SendFrame(new byte[] { 0, (byte)'A' });
Thread.Sleep(500);
pub.SendMoreFrame("A");
pub.SendFrame("Hello");
Assert.False(sub.TryReceiveFrameString(out string str));
}
}
[Fact]
public void Manual()
{
using (var pub = new XPublisherSocket())
using (var sub = new XSubscriberSocket())
{
pub.Bind("inproc://manual");
pub.Options.ManualPublisher = true;
sub.Connect("inproc://manual");
sub.SendFrame(new byte[] { 1, (byte)'A' });
var subscription = pub.ReceiveFrameBytes();
Assert.Equal(subscription[1], (byte)'A');
pub.Subscribe("B");
pub.SendFrame("A");
pub.SendFrame("B");
Assert.Equal("B", sub.ReceiveFrameString());
}
}
[Fact]
public void WelcomeMessage()
{
using (var pub = new XPublisherSocket())
using (var sub = new SubscriberSocket())
{
pub.Bind("inproc://welcome");
pub.SetWelcomeMessage("W");
sub.Subscribe("W");
sub.Connect("inproc://welcome");
var subscription = pub.ReceiveFrameBytes();
Assert.Equal(subscription[1], (byte)'W');
Assert.Equal("W", sub.ReceiveFrameString());
}
}
[Fact]
public void ClearWelcomeMessage()
{
using (var pub = new XPublisherSocket())
using (var sub = new SubscriberSocket())
{
pub.Bind("inproc://welcome");
pub.SetWelcomeMessage("W");
pub.ClearWelcomeMessage();
sub.Subscribe("W");
sub.Connect("inproc://welcome");
var subscription = pub.ReceiveFrameBytes();
Assert.Equal(subscription[1], (byte)'W');
Assert.False(sub.TrySkipFrame());
}
}
[Fact]
public void BroadcastEnabled()
{
using (var pub = new XPublisherSocket())
using (var sub1 = new XSubscriberSocket())
using (var sub2 = new XSubscriberSocket())
{
pub.Bind("inproc://manual");
pub.Options.XPubBroadcast = true;
sub1.Connect("inproc://manual");
sub2.Connect("inproc://manual");
sub1.SendFrame(new byte[] {1, (byte) 'A'});
sub2.SendFrame(new byte[] {1, (byte) 'A'});
var payload = new[] {(byte) 42};
var msg = new Msg();
msg.InitEmpty();
// add prefix 2 to the topic, this indicates a broadcast message and it will not be sent to sub1
sub1.SendFrame(new byte[] {2, (byte) 'A'}, true);
sub1.SendFrame(new[] {(byte) 42});
var subscription = pub.ReceiveFrameBytes();
var topic = pub.ReceiveFrameBytes();
var message = pub.ReceiveFrameBytes();
Assert.Equal(2, topic[0]);
// we must skip the first byte if we have detected a broadcast message
// the sender of this message is already marked for exclusion
// but the match logic in Send should work with normal topic.
topic = topic.Skip(1).ToArray();
pub.SendFrame(topic, true);
pub.SendFrame(message);
var broadcast2 = sub2.ReceiveFrameBytes();
Assert.True(broadcast2[0] == 65);
broadcast2 = sub2.ReceiveFrameBytes();
Assert.True(broadcast2.SequenceEqual(payload));
// this message SHOULD NOT be resent to sub1
var received = sub1.TryReceive(ref msg, System.TimeSpan.FromMilliseconds(500));
Assert.False(received);
}
}
[Fact]
public void BroadcastDisabled()
{
using (var pub = new XPublisherSocket())
using (var sub1 = new XSubscriberSocket())
using (var sub2 = new XSubscriberSocket())
{
pub.Bind("inproc://manual");
pub.Options.XPubBroadcast = false;
sub1.Connect("inproc://manual");
sub2.Connect("inproc://manual");
Thread.Sleep(50);
sub1.SendFrame(new byte[] { 1, (byte)'A' });
sub2.SendFrame(new byte[] { 1, (byte)'A' });
var payload = new[] { (byte)42 };
sub1.SendFrame(new[] { (byte)'A' }, true);
sub1.SendFrame(new[] { (byte)42 });
var subscription = pub.ReceiveFrameBytes();
var topic = pub.ReceiveFrameBytes();
var message = pub.ReceiveFrameBytes();
pub.SendFrame(topic, true);
pub.SendFrame(message);
var broadcast2 = sub2.ReceiveFrameBytes();
Assert.True(broadcast2[0] == 65);
broadcast2 = sub2.ReceiveFrameBytes();
Assert.True(broadcast2.SequenceEqual(payload));
// sub1 should receive a message normally
var broadcast1 = sub1.ReceiveFrameBytes();
Assert.True(broadcast1[0] == 65);
broadcast1 = sub1.ReceiveFrameBytes();
Assert.True(broadcast1.SequenceEqual(payload));
}
}
[Fact]
public void CouldTrackSubscriberIdentityInXPubSocket() {
using (var pub = new XPublisherSocket())
using (var sub1 = new XSubscriberSocket())
using (var sub2 = new XSubscriberSocket()) {
pub.Bind("inproc://manual");
pub.Options.ManualPublisher = true;
sub1.Connect("inproc://manual");
sub2.Connect("inproc://manual");
Thread.Sleep(50);
sub1.SendFrame(new byte[] { 1, (byte)'A' });
var identity1 = new byte[] { 1 };
var identity2 = new byte[] { 2 };
sub1.SendFrame(new[] { (byte)'A' }, true);
sub1.SendFrame(new[] { (byte)42 });
var subscription = pub.ReceiveFrameBytes();
// NB Identity must be set before pub.Subscribe/Unsubscribe/Send, because these operations clear a private field with last subscriber
// set identity to sub1
pub.Options.Identity = identity1;
Assert.True(identity1.SequenceEqual(pub.Options.Identity), "Cannot read identity that was just set");
pub.Subscribe(subscription);
Assert.True(identity1.SequenceEqual(pub.Options.Identity), "Identity must be kept after Subscribe/Unsubscribe/Send operations (which clear m_lastPipe)");
var topic = pub.ReceiveFrameBytes();
var message = pub.ReceiveFrameBytes();
sub2.SendFrame(new byte[] { 1, (byte)'A' });
sub2.SendFrame(new[] { (byte)'A' }, true);
sub2.SendFrame(new[] { (byte)43 });
subscription = pub.ReceiveFrameBytes();
// Id of sub2 is not set yet
Assert.Null(pub.Options.Identity);
pub.Options.Identity = identity2;
Assert.True(identity2.SequenceEqual(pub.Options.Identity), "Cannot read identity that was just set");
pub.Subscribe(subscription);
Assert.True(identity2.SequenceEqual(pub.Options.Identity), "Identity must be kept after Subscribe/Unsubscribe/Send operations (which clear m_lastPipe)");
topic = pub.ReceiveFrameBytes();
message = pub.ReceiveFrameBytes();
sub1.SendFrame(new[] { (byte)'A' }, true);
sub1.SendFrame(new[] { (byte)44 });
topic = pub.ReceiveFrameBytes();
message = pub.ReceiveFrameBytes();
Assert.True(identity1.SequenceEqual(pub.Options.Identity), "Identity option must be set to the identity of sub1 here");
sub2.SendFrame(new[] { (byte)'A' }, true);
sub2.SendFrame(new[] { (byte)45 });
topic = pub.ReceiveFrameBytes();
message = pub.ReceiveFrameBytes();
Assert.True(identity2.SequenceEqual(pub.Options.Identity), "Identity option must be set to the identity of sub2 here");
}
}
}
}