Permalink
Browse files

Convert Monitor tests.

  • Loading branch information...
1 parent 039584b commit 89cf2a1db964dbabd9e4e6cd99c15c25a30e9bbc @jgoz jgoz committed Nov 11, 2012
View
117 src/ZeroMQ.AcceptanceTests/MonitorFixtures.cs
@@ -0,0 +1,117 @@
+namespace ZeroMQ.AcceptanceTests
+{
+ using System;
+ using System.Threading;
+ using Monitoring;
+ using NUnit.Framework;
+
+ public abstract class UsingMonitor
+ {
+ protected const string ReqEndpoint = "inproc://monitor.req";
+ protected const string RepEndpoint = "inproc://monitor.rep";
+
+ protected bool Fired;
+ protected string Address;
+
+ protected ZmqMonitor ReqMonitor;
+ protected ZmqMonitor RepMonitor;
+ protected Thread ReqThread;
+ protected Thread RepThread;
+ protected ZmqSocket Req;
+ protected ZmqSocket Rep;
+ protected ZmqContext RepContext;
+ protected ZmqContext ReqContext;
+
+ protected ManualResetEvent EventRecorded;
+
+ [TestFixtureSetUp]
+ public void Initialize()
+ {
+ ReqContext = ZmqContext.Create();
+ RepContext = ZmqContext.Create();
+ ReqMonitor = ReqContext.CreateMonitorSocket(ReqEndpoint);
+ RepMonitor = RepContext.CreateMonitorSocket(RepEndpoint);
+ Req = ReqContext.CreateSocket(SocketType.REQ);
+ Rep = RepContext.CreateSocket(SocketType.REP);
+ Req.Monitor(ReqEndpoint);
+ Rep.Monitor(RepEndpoint);
+ EventRecorded = new ManualResetEvent(false);
+ ReqThread = new Thread(ReqMonitor.Start);
+ RepThread = new Thread(RepMonitor.Start);
+
+ ReqThread.Start();
+ RepThread.Start();
+
+ Fired = false;
+ Address = null;
+ }
+
+ [TestFixtureTearDown]
+ public void Cleanup()
+ {
+ ReqMonitor.Stop();
+ RepMonitor.Stop();
+
+ if (!ReqThread.Join(TimeSpan.FromSeconds(1)))
+ ReqThread.Abort();
+
+ if (!RepThread.Join(TimeSpan.FromSeconds(1)))
+ RepThread.Abort();
+
+ ReqMonitor.Dispose();
+ RepMonitor.Dispose();
+ Req.Dispose();
+ Rep.Dispose();
+ ReqContext.Dispose();
+ RepContext.Dispose();
+ }
+ }
+
+ public abstract class UsingMonitorFd : UsingMonitor
+ {
+#if UNIX
+ protected int SocketPtr;
+#else
+ protected IntPtr SocketPtr;
+#endif
+
+ protected void RecordEvent(object sender, ZmqMonitorFileDescriptorEventArgs args)
+ {
+ Fired = true;
+ Address = args.Address;
+ SocketPtr = args.FileDescriptor;
+
+ EventRecorded.Set();
+ }
+
+ [TestFixtureSetUp]
+ public void SocketInit()
+ {
+#if UNIX
+ SocketPtr = 0;
+#else
+ SocketPtr = IntPtr.Zero;
+#endif
+ }
+ }
+
+ public abstract class UsingMonitorError : UsingMonitor
+ {
+ protected int ErrorCode;
+
+ protected void RecordEvent(object sender, ZmqMonitorErrorEventArgs args)
+ {
+ Fired = true;
+ Address = args.Address;
+ ErrorCode = args.ErrorCode;
+
+ EventRecorded.Set();
+ }
+
+ [TestFixtureSetUp]
+ public void ErrorCodeInit()
+ {
+ ErrorCode = 0;
+ }
+ }
+}
View
4 src/ZeroMQ.AcceptanceTests/ZeroMQ.AcceptanceTests.csproj
@@ -118,8 +118,8 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ZmqContextSpecs\ContextOptions.cs" />
- <Compile Include="ZmqMonitorSpecs\MonitorEvents.cs" />
- <Compile Include="ZmqMonitorSpecs\SpecContext.cs" />
+ <Compile Include="ZmqMonitorTests\MonitorEvents.cs" />
+ <Compile Include="MonitorFixtures.cs" />
<Compile Include="ZmqSocketTests\Bind_Connect.cs" />
<Compile Include="ZmqSocketTests\SendMessage_ReceiveMessage.cs" />
<Compile Include="ZmqSocketTests\Send_Receive.cs" />
View
132 src/ZeroMQ.AcceptanceTests/ZmqMonitorSpecs/MonitorEvents.cs
@@ -1,132 +0,0 @@
-namespace ZeroMQ.AcceptanceTests.ZmqMonitorSpecs
-{
- using System;
-
- using Machine.Specifications;
-
- [Subject("Monitor events")]
- class when_monitoring_listening_event : using_monitor_fd
- {
- Establish context = () =>
- repMonitor.Listening += RecordEvent;
-
- Because of = () =>
- {
- rep.Bind("tcp://*:9000");
- eventRecorded.WaitOne(1000);
- };
-
- It should_fire_the_listening_event = () =>
- fired.ShouldBeTrue();
-
- It should_set_the_listening_socket_address = () =>
- address.ShouldEqual("tcp://0.0.0.0:9000");
-
- It should_return_a_socket_pointer = () =>
-#if UNIX
- socketPtr.ShouldNotEqual(0);
-#else
- socketPtr.ShouldNotEqual(IntPtr.Zero);
-#endif
- }
-
- [Subject("Monitor events")]
- class when_monitoring_accepted_event : using_monitor_fd
- {
- Establish context = () =>
- repMonitor.Accepted += RecordEvent;
-
- Because of = () =>
- {
- rep.Bind("tcp://*:9000");
- req.Connect("tcp://127.0.0.1:9000");
- eventRecorded.WaitOne(1000);
- };
-
- It should_fire_the_accepted_event = () =>
- fired.ShouldBeTrue();
-
- It should_set_the_accepted_socket_address = () =>
- address.ShouldEqual("tcp://0.0.0.0:9000");
-
- It should_return_a_socket_pointer = () =>
-#if UNIX
- socketPtr.ShouldNotEqual(0);
-#else
- socketPtr.ShouldNotEqual(IntPtr.Zero);
-#endif
- }
-
- [Subject("Monitor events")]
- class when_monitoring_accepted_event_before_a_connection_is_made : using_monitor_fd
- {
- Establish context = () =>
- repMonitor.Accepted += RecordEvent;
-
- Because of = () =>
- {
- rep.Bind("tcp://*:9000");
- eventRecorded.WaitOne(100);
- };
-
- It should_not_fire_the_accepted_event = () =>
- fired.ShouldBeFalse();
- }
-
- [Subject("Monitor events")]
- class when_monitoring_connected_event : using_monitor_fd
- {
- Establish context = () =>
- reqMonitor.Connected += RecordEvent;
-
- Because of = () =>
- {
- rep.Bind("tcp://*:9000");
- req.Connect("tcp://127.0.0.1:9000");
- eventRecorded.WaitOne(1000);
- };
-
- It should_fire_the_connected_event = () =>
- fired.ShouldBeTrue();
-
- [Ignore("This will occasionally fail in libzmq, as the memory pointed to by 'address' may have been reclaimed.")]
- It should_set_the_connected_socket_address = () =>
- address.ShouldEqual("tcp://127.0.0.1:9000");
-
- It should_return_a_socket_pointer = () =>
-#if UNIX
- socketPtr.ShouldNotEqual(0);
-#else
- socketPtr.ShouldNotEqual(IntPtr.Zero);
-#endif
- }
-
- [Subject("Monitor events")]
- class when_monitoring_closed_event : using_monitor_fd
- {
- Establish context = () =>
- repMonitor.Closed += RecordEvent;
-
- Because of = () =>
- {
- rep.Bind("tcp://*:9000");
- req.Connect("tcp://127.0.0.1:9000");
- rep.Close();
- eventRecorded.WaitOne(1000);
- };
-
- It should_fire_the_closed_event = () =>
- fired.ShouldBeTrue();
-
- [Ignore("This will occasionally fail in libzmq, as the memory pointed to by 'address' may have been reclaimed.")]
- It should_set_the_closed_socket_address = () =>
- address.ShouldEqual("tcp://0.0.0.0:9000");
-
- It should_return_a_socket_pointer = () =>
-#if UNIX
- socketPtr.ShouldNotEqual(0);
-#else
- socketPtr.ShouldNotEqual(IntPtr.Zero);
-#endif
- }
-}
View
125 src/ZeroMQ.AcceptanceTests/ZmqMonitorSpecs/SpecContext.cs
@@ -1,125 +0,0 @@
-namespace ZeroMQ.AcceptanceTests.ZmqMonitorSpecs
-{
- using System;
- using System.Threading;
-
- using Machine.Specifications;
-
- using Monitoring;
-
- abstract class using_monitor
- {
- protected const string ReqEndpoint = "inproc://monitor.req";
- protected const string RepEndpoint = "inproc://monitor.rep";
-
- protected static bool fired;
- protected static string address;
-
- protected static ZmqMonitor reqMonitor;
- protected static ZmqMonitor repMonitor;
- protected static Thread reqThread;
- protected static Thread repThread;
- protected static ZmqSocket req;
- protected static ZmqSocket rep;
- protected static ZmqContext repContext;
- protected static ZmqContext reqContext;
- protected static Exception exception;
-
- protected static ManualResetEvent eventRecorded;
-
- Establish context = () =>
- {
- reqContext = ZmqContext.Create();
- repContext = ZmqContext.Create();
- reqMonitor = reqContext.CreateMonitorSocket(ReqEndpoint);
- repMonitor = repContext.CreateMonitorSocket(RepEndpoint);
- req = reqContext.CreateSocket(SocketType.REQ);
- rep = repContext.CreateSocket(SocketType.REP);
- req.Monitor(ReqEndpoint);
- rep.Monitor(RepEndpoint);
- eventRecorded = new ManualResetEvent(false);
- reqThread = new Thread(reqMonitor.Start);
- repThread = new Thread(repMonitor.Start);
-
- reqThread.Start();
- repThread.Start();
-
- fired = false;
- address = null;
- };
-
- Cleanup resources = () =>
- {
- reqMonitor.Stop();
- repMonitor.Stop();
-
- if (!reqThread.Join(TimeSpan.FromSeconds(1)))
- reqThread.Abort();
-
- if (!repThread.Join(TimeSpan.FromSeconds(1)))
- repThread.Abort();
-
- exception = null;
- reqMonitor.Dispose();
- repMonitor.Dispose();
- req.Dispose();
- rep.Dispose();
- reqContext.Dispose();
- repContext.Dispose();
- };
- }
-
- abstract class using_monitor_fd : using_monitor
- {
-#if UNIX
- protected static int socketPtr;
-#else
- protected static IntPtr socketPtr;
-#endif
-
- protected static void RecordEvent(object sender, ZmqMonitorFileDescriptorEventArgs args)
- {
- fired = true;
- address = args.Address;
- socketPtr = args.FileDescriptor;
-
- eventRecorded.Set();
- }
-
- Establish context = () =>
- {
-#if UNIX
- socketPtr = 0;
-#else
- socketPtr = IntPtr.Zero;
-#endif
- };
-
- Cleanup resources = () =>
- {
- };
- }
-
- abstract class using_monitor_error : using_monitor
- {
- protected static int errorCode;
-
- protected static void RecordEvent(object sender, ZmqMonitorErrorEventArgs args)
- {
- fired = true;
- address = args.Address;
- errorCode = args.ErrorCode;
-
- eventRecorded.Set();
- }
-
- Establish context = () =>
- {
- errorCode = 0;
- };
-
- Cleanup resources = () =>
- {
- };
- }
-}
View
162 src/ZeroMQ.AcceptanceTests/ZmqMonitorTests/MonitorEvents.cs
@@ -0,0 +1,162 @@
+namespace ZeroMQ.AcceptanceTests.ZmqMonitorTests
+{
+ using System;
+ using NUnit.Framework;
+
+ [TestFixture]
+ public class MonitorTests
+ {
+ public class WhenMonitoringListeningEvent : UsingMonitorFd
+ {
+ [TestFixtureSetUp]
+ public void SetUp()
+ {
+ RepMonitor.Listening += RecordEvent;
+ Rep.Bind("tcp://*:9000");
+ EventRecorded.WaitOne(1000);
+ }
+
+ [Test]
+ public void ShouldFireTheListeningEvent()
+ {
+ Assert.IsTrue(Fired);
+ }
+
+ [Test]
+ public void ShouldSetTheListeningSocketAddress()
+ {
+ Assert.AreEqual("tcp://0.0.0.0:9000", Address);
+ }
+
+ [Test]
+ public void ShouldReturnASocketPointer()
+ {
+#if UNIX
+ Assert.AreNotEqual(0, SocketPtr);
+#else
+ Assert.AreNotEqual(IntPtr.Zero, SocketPtr);
+#endif
+ }
+ }
+
+ public class WhenMonitoringAcceptedEvent : UsingMonitorFd
+ {
+ [TestFixtureSetUp]
+ public void SetUp()
+ {
+ RepMonitor.Accepted += RecordEvent;
+ Rep.Bind("tcp://*:9000");
+ Req.Connect("tcp://127.0.0.1:9000");
+ EventRecorded.WaitOne(1000);
+ }
+
+ [Test]
+ public void ShouldFireTheAcceptedEvent()
+ {
+ Assert.IsTrue(Fired);
+ }
+
+ [Test]
+ public void ShouldSetTheAcceptedSocketAddress()
+ {
+ Assert.AreEqual("tcp://0.0.0.0:9000", Address);
+ }
+
+ [Test]
+ public void ShouldReturnASocketPointer()
+ {
+#if UNIX
+ Assert.AreNotEqual(0, SocketPtr);
+#else
+ Assert.AreNotEqual(IntPtr.Zero, SocketPtr);
+#endif
+ }
+ }
+
+ public class WhenMonitoringAcceptedEventBeforeAConnectionIsMade : UsingMonitorFd
+ {
+ [TestFixtureSetUp]
+ public void SetUp()
+ {
+ RepMonitor.Accepted += RecordEvent;
+ Rep.Bind("tcp://*:9000");
+ EventRecorded.WaitOne(100);
+ }
+
+ [Test]
+ public void ShouldNotFireTheAcceptedEvent()
+ {
+ Assert.IsFalse(Fired);
+ }
+ }
+
+ public class WhenMonitoringConnectedEvent : UsingMonitorFd
+ {
+ [TestFixtureSetUp]
+ public void SetUp()
+ {
+ ReqMonitor.Connected += RecordEvent;
+ Rep.Bind("tcp://*:9000");
+ Req.Connect("tcp://127.0.0.1:9000");
+ EventRecorded.WaitOne(1000);
+ }
+
+ [Test]
+ public void ShouldFireTheConnectedEvent()
+ {
+ Assert.IsTrue(Fired);
+ }
+
+ [Test]
+ public void ShouldSetTheConnectedSocketAddress()
+ {
+ Assert.AreEqual("tcp://127.0.0.1:9000", Address);
+ }
+
+ [Test]
+ public void ShouldReturnASocketPointer()
+ {
+#if UNIX
+ Assert.AreNotEqual(0, SocketPtr);
+#else
+ Assert.AreNotEqual(IntPtr.Zero, SocketPtr);
+#endif
+ }
+ }
+
+ public class WhenMonitoringClosedEvent : UsingMonitorFd
+ {
+ [TestFixtureSetUp]
+ public void SetUp()
+ {
+ RepMonitor.Closed += RecordEvent;
+ Rep.Bind("tcp://*:9000");
+ Req.Connect("tcp://127.0.0.1:9000");
+ Rep.Close();
+ EventRecorded.WaitOne(1000);
+ }
+
+ [Test]
+ public void ShouldFireTheClosedEvent()
+ {
+ Assert.IsTrue(Fired);
+ }
+
+ [Test]
+ public void ShouldSetTheClosedSocketAddress()
+ {
+ Assert.AreEqual("tcp://0.0.0.0:9000", Address);
+ }
+
+ [Test]
+ public void ShouldReturnASocketPointer()
+ {
+#if UNIX
+ Assert.AreNotEqual(0, SocketPtr);
+#else
+ Assert.AreNotEqual(IntPtr.Zero, SocketPtr);
+#endif
+ }
+ }
+ }
+}

0 comments on commit 89cf2a1

Please sign in to comment.