Skip to content

Commit 9d19a46

Browse files
authored
Merge pull request #7 from somdoron/master
cherry picks fixes from NetMQ4
2 parents 2dbb8f3 + ad5059e commit 9d19a46

File tree

12 files changed

+208
-37
lines changed

12 files changed

+208
-37
lines changed

src/NetMQ.Tests/CleanupTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using NetMQ.Sockets;
9+
using NUnit.Framework;
10+
11+
namespace NetMQ.Tests
12+
{
13+
[TestFixture]
14+
public class CleanupTests
15+
{
16+
[Test]
17+
public void Block()
18+
{
19+
const int count = 1000;
20+
21+
NetMQConfig.Linger = TimeSpan.FromSeconds(0.5);
22+
23+
using (var client = new DealerSocket(">tcp://localhost:5557"))
24+
{
25+
// Sending a lot of messages
26+
client.Options.SendHighWatermark = count;
27+
for (int i = 0; i < count; i++)
28+
{
29+
client.SendFrame("Hello");
30+
}
31+
}
32+
33+
Stopwatch stopwatch = Stopwatch.StartNew();
34+
NetMQConfig.Cleanup();
35+
stopwatch.Stop();
36+
37+
Assert.Greater(stopwatch.ElapsedMilliseconds, 500);
38+
}
39+
40+
[Test]
41+
public void NoBlock()
42+
{
43+
const int count = 1000;
44+
45+
NetMQConfig.Linger = TimeSpan.FromSeconds(0.5);
46+
47+
using (var client = new DealerSocket(">tcp://localhost:5557"))
48+
{
49+
// Sending a lot of messages
50+
client.Options.SendHighWatermark = count;
51+
for (int i = 0; i < count; i++)
52+
{
53+
client.SendFrame("Hello");
54+
}
55+
}
56+
57+
Stopwatch stopwatch = Stopwatch.StartNew();
58+
NetMQConfig.Cleanup(false);
59+
stopwatch.Stop();
60+
61+
Assert.Less(stopwatch.ElapsedMilliseconds, 500);
62+
}
63+
64+
[Test]
65+
public void NoBlockNoDispose()
66+
{
67+
var client = new DealerSocket(">tcp://localhost:5557");
68+
NetMQConfig.Cleanup(false);
69+
}
70+
}
71+
}

src/NetMQ.Tests/NetMQ.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="ActorTests.cs" />
7373
<Compile Include="BeaconTests.cs" />
7474
<Compile Include="ByteArraySegmentTests.cs" />
75+
<Compile Include="CleanupTests.cs" />
7576
<Compile Include="EventDelegatorTests.cs" />
7677
<Compile Include="ExceptionTests.cs" />
7778
<Compile Include="InProcActors\AccountJSON\Account.cs" />
@@ -91,6 +92,7 @@
9192
<Compile Include="ReceivingSocketExtensionsTests.cs" />
9293
<Compile Include="RequestWithRetryTests.cs" />
9394
<Compile Include="RouterTests.cs" />
95+
<Compile Include="Setup.cs" />
9496
<Compile Include="XPubSubTests.cs" />
9597
<Compile Include="Devices\DeviceTestBase.cs" />
9698
<Compile Include="Devices\ForwarderDeviceTests.cs" />

src/NetMQ.Tests/NetMQ3.5.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="ActorTests.cs" />
7474
<Compile Include="BeaconTests.cs" />
7575
<Compile Include="ByteArraySegmentTests.cs" />
76+
<Compile Include="CleanupTests.cs" />
7677
<Compile Include="Core\YQueueTests.cs" />
7778
<Compile Include="EventDelegatorTests.cs" />
7879
<Compile Include="ExceptionTests.cs" />
@@ -84,6 +85,7 @@
8485
<Compile Include="NetMQPollerTest.cs" />
8586
<Compile Include="ReceivingSocketExtensionsTests.cs" />
8687
<Compile Include="RouterTests.cs" />
88+
<Compile Include="Setup.cs" />
8789
<Compile Include="SocketOptionsTests.cs" />
8890
<Compile Include="XPubSubTests.cs" />
8991
<Compile Include="MessageTests.cs" />

src/NetMQ.Tests/NetMQMonitorTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
namespace NetMQ.Tests
1010
{
11-
[TestFixture]
11+
[TestFixture(Category = "Monitor")]
1212
public class NetMQMonitorTests
1313
{
1414
[Test]
1515
public void Monitoring()
1616
{
1717
using (var rep = new ResponseSocket())
1818
using (var req = new RequestSocket())
19-
using (var monitor = new NetMQMonitor(rep, "inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening))
19+
using (var monitor = new NetMQMonitor(rep, $"inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening))
2020
{
2121
var listening = false;
2222
var accepted = false;
@@ -28,6 +28,8 @@ public void Monitoring()
2828

2929
var monitorTask = Task.Factory.StartNew(monitor.Start);
3030

31+
Thread.Sleep(10);
32+
3133
var port = rep.BindRandomPort("tcp://127.0.0.1");
3234

3335
req.Connect("tcp://127.0.0.1:" + port);
@@ -47,8 +49,8 @@ public void Monitoring()
4749

4850
Thread.Sleep(200);
4951

50-
Assert.IsTrue(monitorTask.IsCompleted);
51-
}
52+
Assert.IsTrue(monitorTask.IsCompleted);
53+
}
5254
}
5355

5456
#if !NET35
@@ -102,6 +104,7 @@ public void ErrorCodeTest()
102104
monitor.Timeout = TimeSpan.FromMilliseconds(100);
103105

104106
var monitorTask = Task.Factory.StartNew(monitor.Start);
107+
Thread.Sleep(10);
105108

106109
var port = rep.BindRandomPort("tcp://127.0.0.1");
107110

src/NetMQ.Tests/Setup.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
7+
namespace NetMQ.Tests
8+
{
9+
[SetUpFixture]
10+
public class Setup
11+
{
12+
[TearDown]
13+
public void TearDown()
14+
{
15+
NetMQConfig.Cleanup(false);
16+
}
17+
}
18+
}

src/NetMQ/Core/Command.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Command([CanBeNull] ZObject destination, CommandType type, [CanBeNull] ob
5252
/// Get the argument to this command.
5353
/// </summary>
5454
[CanBeNull]
55-
public object Arg { get; private set; }
55+
public object Arg { get; private set; }
5656

5757
/// <summary>
5858
/// Override of ToString, which returns a string in the form [ command-type, destination ].

src/NetMQ/Core/CommandType.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ internal enum CommandType
101101
/// Sent by reaper thread to the term thread when all the sockets
102102
/// have successfully been deallocated.
103103
/// </summary>
104-
Done
104+
Done,
105+
106+
/// <summary>
107+
/// Send to reaper to stop the reaper immediatly
108+
/// </summary>
109+
ForceStop
105110
}
106111
}

src/NetMQ/Core/Ctx.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace NetMQ.Core
3636
/// <remarks>Internal analog of the public <see cref="NetMQContext"/> class.</remarks>
3737
internal sealed class Ctx
3838
{
39-
private const int DefaultIOThreads = 1;
40-
private const int DefaultMaxSockets = 1024;
39+
internal const int DefaultIOThreads = 1;
40+
internal const int DefaultMaxSockets = 1024;
4141

4242
#region Nested class: Endpoint
4343

@@ -217,8 +217,12 @@ public void Terminate()
217217
foreach (var socket in m_sockets)
218218
socket.Stop();
219219

220-
if (m_sockets.Count == 0)
221-
m_reaper.Stop();
220+
if (!Block)
221+
{
222+
m_reaper.ForceStop();
223+
}
224+
else if (m_sockets.Count == 0)
225+
m_reaper.Stop();
222226
}
223227
finally
224228
{
@@ -234,8 +238,7 @@ public void Terminate()
234238

235239
Debug.Assert(found);
236240
Debug.Assert(command.CommandType == CommandType.Done);
237-
Monitor.Enter(m_slotSync);
238-
Debug.Assert(m_sockets.Count == 0);
241+
Monitor.Enter(m_slotSync);
239242
}
240243
else
241244
Monitor.Enter(m_slotSync);

src/NetMQ/Core/Reaper.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public void Stop()
114114
SendStop();
115115
}
116116

117+
public void ForceStop()
118+
{
119+
SendForceStop();
120+
}
121+
117122
/// <summary>
118123
/// Handle input-ready events, by receiving and processing any commands
119124
/// that are waiting in the mailbox.
@@ -168,6 +173,14 @@ protected override void ProcessStop()
168173
}
169174
}
170175

176+
protected override void ProcessForceStop()
177+
{
178+
m_terminating = true;
179+
SendDone();
180+
m_poller.RemoveHandle(m_mailboxHandle);
181+
m_poller.Stop();
182+
}
183+
171184
/// <summary>
172185
/// Add the given socket to the list to be reaped (terminated).
173186
/// </summary>
@@ -196,6 +209,6 @@ protected override void ProcessReaped()
196209
m_poller.RemoveHandle(m_mailboxHandle);
197210
m_poller.Stop();
198211
}
199-
}
212+
}
200213
}
201214
}

src/NetMQ/Core/Utils/Poller.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ public void Destroy()
135135
if (!m_stopped)
136136
{
137137
try
138-
{
139-
m_stopping = true;
138+
{
140139
m_workerThread.Join();
141140
}
142141
catch (Exception)

0 commit comments

Comments
 (0)