forked from zeromq/netmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeaconTests.cs
196 lines (153 loc) · 5.63 KB
/
BeaconTests.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
using System;
using System.Threading;
using Xunit;
// ReSharper disable ExceptionNotDocumented
namespace NetMQ.Tests
{
[Trait("Category", "Beacon")]
public class BeaconTests : IClassFixture<CleanupAfterFixture>
{
private static readonly TimeSpan s_publishInterval = TimeSpan.FromMilliseconds(100);
public BeaconTests() => NetMQConfig.Cleanup();
[Fact]
public void SimplePublishSubscribe()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(9999);
speaker.Publish("Hello", s_publishInterval);
listener.Configure(9999);
listener.Subscribe("H");
var message = listener.Receive();
Assert.Equal("Hello", message.String);
}
}
[Fact]
public void Silence()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(9999);
listener.Configure(9999);
listener.Subscribe("H");
// this should send one broadcast message and stop
speaker.Publish("Hello", s_publishInterval);
Thread.Sleep(10);
speaker.Silence();
Assert.Equal("Hello", listener.Receive().String);
Assert.False(listener.TryReceive(TimeSpan.FromMilliseconds(300), out BeaconMessage message));
}
}
[Fact]
public void Unsubscribe()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(9999);
listener.Configure(9999);
listener.Subscribe("H");
// this should send one broadcast message and stop
speaker.Publish("Hello", s_publishInterval);
Thread.Sleep(10);
listener.Unsubscribe();
Assert.Equal("Hello", listener.Receive().String);
Assert.False(listener.TryReceive(TimeSpan.FromMilliseconds(300), out BeaconMessage message));
}
}
[Fact]
public void SubscribeToDifferentTopic()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(9999);
listener.Configure(9999);
listener.Subscribe("B");
// this should send one broadcast message and stop
speaker.Publish("Hello", s_publishInterval);
Assert.False(listener.TryReceive(TimeSpan.FromMilliseconds(300), out BeaconMessage message));
}
}
[Fact]
public void Polling()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(9999);
speaker.Publish("Hello", s_publishInterval);
var manualResetEvent = new ManualResetEvent(false);
listener.Configure(9999);
listener.Subscribe("H");
string message = "";
listener.ReceiveReady += (sender, args) =>
{
message = listener.Receive().String;
manualResetEvent.Set();
};
using (var poller = new NetMQPoller { listener })
{
poller.RunAsync();
manualResetEvent.WaitOne();
Assert.Equal("Hello", message);
}
}
}
[Fact]
public void NeverConfigured()
{
using (new NetMQBeacon())
{}
}
[Fact]
public void ConfigureTwice()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(5555);
speaker.Configure(9999);
speaker.Publish("Hello", s_publishInterval);
listener.Configure(9999);
listener.Subscribe("H");
string message = listener.Receive().String;
Assert.Equal("Hello", message);
}
}
[Fact]
public void BothSpeakerAndListener()
{
using (var beacon1 = new NetMQBeacon())
using (var beacon2 = new NetMQBeacon())
{
beacon1.Configure(9999);
beacon1.Publish("H1", s_publishInterval);
beacon1.Subscribe("H");
beacon2.Configure(9999);
beacon2.Publish("H2", s_publishInterval);
beacon2.Subscribe("H");
Assert.Equal("H2", beacon1.Receive().String);
Assert.Equal("H1", beacon2.Receive().String);
}
}
[Fact]
public void BothSpeakerAndListenerOverLoopback()
{
using (var beacon1 = new NetMQBeacon())
using (var beacon2 = new NetMQBeacon())
{
beacon1.Configure(9998, "loopback");
beacon1.Publish("H1", s_publishInterval);
beacon1.Subscribe("H");
beacon2.Configure(9998, "loopback");
beacon2.Publish("H2", s_publishInterval);
beacon2.Subscribe("H");
Assert.Equal("H2", beacon1.Receive().String);
Assert.Equal("H1", beacon2.Receive().String);
}
}
}
}