Exemplo n.º 1
0
        public void MonitoringPoll()
        {
            bool listening = false;
            bool connected = false;
            bool accepted = false;

            using (Context contex = Context.Create())
            {
                Poller poller = new Poller(contex);

                using (ResponseSocket rep = contex.CreateResponseSocket())
                {
                    MonitoringEventsHandler repMonitor = new MonitoringEventsHandler();
                    repMonitor.OnAccepted = (addr, fd) => accepted = true;
                    repMonitor.OnListening = (addr, fd) => listening = true;

                    poller.AddMonitor(rep, "inproc://rep.inproc", repMonitor, true);
                    rep.Bind("tcp://127.0.0.1:5002");

                    using (RequestSocket req = contex.CreateRequestSocket())
                    {
                        MonitoringEventsHandler reqMonitor = new MonitoringEventsHandler();
                        reqMonitor.OnConnected = (addr, fd) => connected = true;

                        poller.AddMonitor(req, "inproc://req.inproc", reqMonitor, true);

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

                        req.Connect("tcp://127.0.0.1:5002");
                        req.Send("a");

                        bool more;

                        string m = rep.ReceiveString(out more);

                        rep.Send("b");

                        string m2 = req.ReceiveString(out more);

                        Assert.IsTrue(listening);
                        Assert.IsTrue(connected);
                        Assert.IsTrue(accepted);

                        poller.Stop();
                        Thread.Sleep(100);

                        Assert.IsTrue(pollerTask.IsCompleted);
                    }
                }
            }
        }