Exemple #1
0
        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);
            }
        }
Exemple #2
0
        public void TwoTimers()
        {
            using (NetMQContext contex = NetMQContext.Create())
            using (Poller poller = new Poller())
            {

                int count = 0;

                NetMQTimer timer = new NetMQTimer(TimeSpan.FromMilliseconds(50));

                NetMQTimer timer2 = new NetMQTimer(TimeSpan.FromMilliseconds(24));

                timer.Elapsed += (a, s) =>
                {
                    count++;

                    if (count == 3)
                    {
                        timer.Enable = false;
                        timer2.Enable = false;
                    }
                };

                poller.AddTimer(timer);

                int count2 = 0;

                timer2.Elapsed += (s, a) => { count2++; };
                poller.AddTimer(timer2);

                Task.Factory.StartNew(poller.Start);

                Thread.Sleep(300);

                poller.Stop();

                Assert.AreEqual(3, count);
                Assert.AreEqual(6, count2);
            }
        }
Exemple #3
0
        public void ChangeTimerInterval()
        {
            using (NetMQContext contex = NetMQContext.Create())
            using (Poller poller = new Poller())
            {

                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.AddTimer(timer);

                Task.Factory.StartNew(poller.Start);

                Thread.Sleep(500);

                poller.Stop();

                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);
            }
        }
Exemple #4
0
        public void SimpleTimer()
        {
            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 messageArrived = false;

                        router.ReceiveReady += (s, a) =>
                                                                        {
                                                                            bool isMore;
                                                                            router.Receive(out isMore);
                                                                            router.Receive(out isMore);

                                                                            messageArrived = true;
                                                                        };

                        poller.AddSocket(router);

                        bool timerTriggered = false;

                        int count = 0;

                        NetMQTimer timer = new NetMQTimer(TimeSpan.FromMilliseconds(100));
                        timer.Elapsed += (a, s) =>
                                                            {
                                                                // the timer should jump before the message
                                                                Assert.IsFalse(messageArrived);
                                                                timerTriggered = true;
                                                                timer.Enable = false;
                                                                count++;
                                                            };
                        poller.AddTimer(timer);

                        Task.Factory.StartNew(poller.Start);

                        Thread.Sleep(150);

                        dealer.Send("hello");

                        Thread.Sleep(300);

                        poller.Stop();

                        Assert.IsTrue(messageArrived);
                        Assert.IsTrue(timerTriggered);
                        Assert.AreEqual(1, count);
                    }
                }
            }
        }
Exemple #5
0
 public void AddTimer(long timeout, int id)
 {
     m_poller.AddTimer(timeout, this, id);
 }
Exemple #6
0
        public void EnableTimer()
        {
            using (NetMQContext contex = NetMQContext.Create())
            {
                Poller poller = new Poller();

                int count = 0;

                NetMQTimer timer = new NetMQTimer(TimeSpan.FromMilliseconds(20));

                NetMQTimer timer2 = new NetMQTimer(TimeSpan.FromMilliseconds(20));

                timer.Elapsed += (a, s) =>
                {
                    count++;

                    if (count == 1)
                    {
                        timer2.Enable = true;
                        timer.Enable = false;
                    }
                    else if (count == 2)
                    {
                        timer.Enable = false;
                    }
                };

                int count2 = 0;

                timer2.Elapsed += (s, a) =>
                                                        {
                                                            timer.Enable = true;
                                                            timer2.Enable = false;

                                                            count2++;
                                                        };

                timer2.Enable = false;

                poller.AddTimer(timer);
                poller.AddTimer(timer2);

                Task.Factory.StartNew(poller.Start);

                Thread.Sleep(300);

                poller.Stop();

                Assert.AreEqual(2, count);
                Assert.AreEqual(1, count2);
            }
        }
Exemple #7
0
        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));
        }
Exemple #8
0
        public void SimpleTimer()
        {
            // TODO it is not really clear what this test is actually testing -- maybe split it into a few smaller tests

            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 messageArrived = false;

                router.ReceiveReady += (s, a) =>
                {
                    Assert.IsFalse(messageArrived);
                    router.Receive();
                    router.Receive();
                    messageArrived = true;
                };

                bool timerTriggered = false;

                int count = 0;

                const int timerIntervalMillis = 100;

                var timer = new NetMQTimer(TimeSpan.FromMilliseconds(timerIntervalMillis));
                timer.Elapsed += (a, s) =>
                {
                    // the timer should jump before the message
                    Assert.IsFalse(messageArrived);
                    timerTriggered = true;
                    timer.Enable = false;
                    count++;
                };
                poller.AddTimer(timer);

                poller.PollTillCancelledNonBlocking();

                Thread.Sleep(150);

                dealer.Send("hello");

                Thread.Sleep(300);

                poller.CancelAndJoin();

                Assert.IsTrue(messageArrived);
                Assert.IsTrue(timerTriggered);
                Assert.AreEqual(1, count);
            }
        }
Exemple #9
0
        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);
            }
        }