public void TestPollerDispose() { using (NetMQContext contex = NetMQContext.Create()) { int count = 0; NetMQTimer timer = new NetMQTimer(TimeSpan.FromMilliseconds(10)); Stopwatch stopwatch = new Stopwatch(); long length1 = 0; long length2 = 0; timer.Elapsed += (a, s) => { count++; if (count == 1) { stopwatch.Start(); } else if (count == 2) { length1 = stopwatch.ElapsedMilliseconds; timer.Interval = 20; stopwatch.Restart(); } else if (count == 3) { length2 = stopwatch.ElapsedMilliseconds; stopwatch.Stop(); timer.Enable = false; } }; Poller poller; using (poller = new Poller(timer)) { Task task = Task.Factory.StartNew(poller.Start); Thread.Sleep(500); Assert.Throws<InvalidOperationException>(() => { poller.Start(); }); } Assert.That(!poller.IsStarted); Assert.Throws<ObjectDisposedException>(() => { poller.Start(); }); Assert.Throws<ObjectDisposedException>(() => { poller.Stop(); }); Assert.Throws<ObjectDisposedException>(() => { poller.AddTimer(timer); }); Assert.Throws<ObjectDisposedException>(() => { poller.RemoveTimer(timer); }); Assert.AreEqual(3, count); Console.WriteLine("Length1:{0}, Length2:{1}", length1, length2); Assert.GreaterOrEqual(length1, 8); Assert.LessOrEqual(length1, 12); Assert.GreaterOrEqual(length2, 18); Assert.LessOrEqual(length2, 22); } }
public void TestPollerDispose() { const int timerIntervalMillis = 10; var timer = new NetMQTimer(TimeSpan.FromMilliseconds(timerIntervalMillis)); var signal = new ManualResetEvent(false); var count = 0; timer.Elapsed += (a, s) => { if (count++ == 5) signal.Set(); }; Poller poller; using (poller = new Poller(timer) { PollTimeout = TestPollTimeoutMillis }) { poller.PollTillCancelledNonBlocking(); Assert.IsTrue(signal.WaitOne(500)); Assert.IsTrue(poller.IsStarted); Assert.Throws<InvalidOperationException>(() => poller.PollTillCancelled()); } Assert.IsFalse(poller.IsStarted); Assert.Throws<ObjectDisposedException>(() => poller.PollTillCancelled()); Assert.Throws<ObjectDisposedException>(() => poller.CancelAndJoin()); Assert.Throws<ObjectDisposedException>(() => poller.AddTimer(timer)); Assert.Throws<ObjectDisposedException>(() => poller.RemoveTimer(timer)); }
public void CancelTimer() { using (NetMQContext contex = NetMQContext.Create()) { // we are using three responses to make sure we actually move the correct socket and other sockets still work using (var router = contex.CreateRouterSocket()) { router.Bind("tcp://127.0.0.1:5002"); using (var dealer = contex.CreateDealerSocket()) using (Poller poller = new Poller()) { dealer.Connect("tcp://127.0.0.1:5002"); bool timerTriggered = false; NetMQTimer timer = new NetMQTimer(TimeSpan.FromMilliseconds(100)); timer.Elapsed += (a, s) => { timerTriggered = true; }; // the timer will jump after 100ms poller.AddTimer(timer); bool messageArrived = false; router.ReceiveReady += (s, a) => { bool isMore; router.Receive(out isMore); router.Receive(out isMore); messageArrived = true; poller.RemoveTimer(timer); }; poller.AddSocket(router); Task.Factory.StartNew(poller.Start); Thread.Sleep(20); dealer.Send("hello"); Thread.Sleep(300); poller.Stop(); Assert.IsTrue(messageArrived); Assert.IsFalse(timerTriggered); } } } }
public void RemoveTimer() { using (var context = NetMQContext.Create()) using (var router = context.CreateRouterSocket()) using (var dealer = context.CreateDealerSocket()) using (var poller = new Poller(router) { PollTimeout = TestPollTimeoutMillis }) { int port = router.BindRandomPort("tcp://127.0.0.1"); dealer.Connect("tcp://127.0.0.1:" + port); bool timerTriggered = false; var timer = new NetMQTimer(TimeSpan.FromMilliseconds(100)); timer.Elapsed += (a, s) => { timerTriggered = true; }; // the timer will jump after 100ms poller.AddTimer(timer); bool messageArrived = false; router.ReceiveReady += (s, a) => { bool isMore; router.Receive(out isMore); router.Receive(out isMore); messageArrived = true; poller.RemoveTimer(timer); }; poller.PollTillCancelledNonBlocking(); Thread.Sleep(20); dealer.Send("hello"); Thread.Sleep(300); poller.CancelAndJoin(); Assert.IsTrue(messageArrived); Assert.IsFalse(timerTriggered); } }