예제 #1
0
        public void CancelSocket()
        {
            using (Context contex = Context.Create())
            {
                // we are using three responses to make sure we actually move the correct socket and other sockets still work
                using (RouterSocket router = contex.CreateRouterSocket())
                using (RouterSocket router2 = contex.CreateRouterSocket())
                using (RouterSocket router3 = contex.CreateRouterSocket())
                {
                    router.Bind("tcp://127.0.0.1:5002");
                    router2.Bind("tcp://127.0.0.1:5003");
                    router3.Bind("tcp://127.0.0.1:5004");

                    using (DealerSocket dealer = contex.CreateDealerSocket())
                    using (DealerSocket dealer2 = contex.CreateDealerSocket())
                    using (DealerSocket dealer3 = contex.CreateDealerSocket())
                    {
                        dealer.Connect("tcp://127.0.0.1:5002");
                        dealer2.Connect("tcp://127.0.0.1:5003");
                        dealer3.Connect("tcp://127.0.0.1:5004");

                        Poller poller = new Poller(contex);

                        bool first = true;

                        poller.AddSocket(router2, s =>
                        {
                            bool more;

                            // identity
                            byte[] identity = s.Receive(out more);

                            // message
                            s.Receive(out more);

                            s.SendMore(identity);
                            s.Send("2");
                        });

                        poller.AddSocket(router, r =>
                        {
                            if (!first)
                            {
                                Assert.Fail("This should happen because we cancelled the socket");
                            }
                            first = false;

                            bool more;

                            // identity
                            r.Receive(out more);

                            string m = r.ReceiveString(out more);

                            Assert.False(more);
                            Assert.AreEqual("Hello", m);

                            // cancellign the socket
                            poller.CancelSocket(r);
                        });

                        poller.AddSocket(router3, s =>
                        {
                            bool more;

                            // identity
                            byte[] identity = s.Receive(out more);

                            // message
                            s.Receive(out more);

                            s.SendMore(identity); s.Send("3");
                        });

                        Task pollerTask = Task.Factory.StartNew(poller.Start);

                        dealer.Send("Hello");

                        // sending this should not arrive on the poller, therefore response for this will never arrive
                        dealer.Send("Hello2");

                        Thread.Sleep(100);

                        // sending this should not arrive on the poller, therefore response for this will never arrive
                        dealer.Send("Hello3");

                        Thread.Sleep(500);

                        bool more2;

                        // making sure the socket defined before the one cancelled still works
                        dealer2.Send("1");
                        string msg = dealer2.ReceiveString(out more2);
                        Assert.AreEqual("2", msg);

                        // making sure the socket defined after the one cancelled still works
                        dealer3.Send("1");
                        msg = dealer3.ReceiveString(out more2);
                        Assert.AreEqual("3", msg);

                        // we have to give this some time if we want to make sure it's really not happening and it not only because of time
                        Thread.Sleep(300);

                        poller.Stop();

                        Thread.Sleep(100);
                        Assert.IsTrue(pollerTask.IsCompleted);
                    }
                }
            }
        }
예제 #2
0
        public void AddSocketAfterRemoving()
        {
            using (Context contex = Context.Create())
            {
                // we are using three responses to make sure we actually move the correct socket and other sockets still work
                using (RouterSocket router = contex.CreateRouterSocket())
                using (RouterSocket router2 = contex.CreateRouterSocket())
                using (RouterSocket router3 = contex.CreateRouterSocket())
                {
                    router.Bind("tcp://127.0.0.1:5002");
                    router2.Bind("tcp://127.0.0.1:5003");
                    router3.Bind("tcp://127.0.0.1:5004");

                    using (DealerSocket dealer = contex.CreateDealerSocket())
                    using (DealerSocket dealer2 = contex.CreateDealerSocket())
                    using (DealerSocket dealer3 = contex.CreateDealerSocket())
                    {
                        dealer.Connect("tcp://127.0.0.1:5002");
                        dealer2.Connect("tcp://127.0.0.1:5003");
                        dealer3.Connect("tcp://127.0.0.1:5004");

                        bool router1arrived = false;
                        bool router2arrived = false;
                        bool router3arrived = false;

                        Poller poller = new Poller(contex);

                        bool more;

                        poller.AddSocket(router, (r) =>
                        {
                            router1arrived = true;

                            router.Receive(out more);
                            router.Receive(out more);

                            poller.CancelSocket(router);

                        });

                        poller.AddSocket(router2, (r) =>
                                                                                {
                                                                                    router2arrived = true;
                                                                                    router2.Receive(out more);
                                                                                    router2.Receive(out more);

                                                                                    poller.AddSocket(router3, (r2) =>
                        {
                            router3.Receive(out more);
                            router3.Receive(out more);
                            router3arrived = true;
                        });
                                                                                });

                        Task task = Task.Factory.StartNew(poller.Start);

                        dealer.Send("1");
                        Thread.Sleep(100);
                        dealer2.Send("2");
                        Thread.Sleep(100);
                        dealer3.Send("3");
                        Thread.Sleep(100);

                        poller.Stop(true);
                        task.Wait();

                        Assert.IsTrue(router1arrived);
                        Assert.IsTrue(router2arrived);
                        Assert.IsTrue(router3arrived);
                    }
                }
            }
        }