public void ErrorCodeTest() { using (var req = new RequestSocket()) using (var rep = new ResponseSocket()) using (var monitor = new NetMQMonitor(req, "inproc://rep.inproc", SocketEvents.ConnectDelayed)) { var eventArrived = false; monitor.ConnectDelayed += (s, a) => { eventArrived = true; }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var monitorTask = Task.Factory.StartNew(monitor.Start); var port = rep.BindRandomPort("tcp://127.0.0.1"); req.Connect("tcp://127.0.0.1:" + port); req.SendFrame("a"); rep.SkipFrame(); rep.SendFrame("b"); req.SkipFrame(); Thread.Sleep(200); Assert.IsTrue(eventArrived); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(monitorTask.IsCompleted); } }
public void SubsriberCleanupOnUnbind(string address) { for (var i = 0; i < 10; i++) { using (var sub = new SubscriberSocket()) { sub.Bind(address); using (var monitor = new NetMQMonitor(sub, String.Format("inproc://cleanup.test{0}", Guid.NewGuid()), SocketEvents.Closed)) { var monitorTask = Task.Factory.StartNew(monitor.Start); var closed = new ManualResetEventSlim(); monitor.Closed += (sender, args) => closed.Set(); var time = DateTime.Now; sub.Unbind(address); Assert.That(closed.Wait(1000), Is.True, "Unbind failed to report Closed event to the Monitor"); var duration = DateTime.Now - time; Console.WriteLine("Run {0}: {1} ms", i, duration.TotalMilliseconds); monitor.Stop(); monitorTask.Wait(); } } } }
public void Monitoring() { bool listening = false; bool accepted = false; using (NetMQContext contex = NetMQContext.Create()) { using (var rep = contex.CreateResponseSocket()) { using (NetMQMonitor monitor = new NetMQMonitor(contex, rep, "inproc://rep.inproc", SocketEvent.Accepted | SocketEvent.Listening)) { monitor.Accepted += (s, a) => { accepted = true; //Console.WriteLine(a.Socket.LocalEndPoint.ToString()); }; monitor.Listening += (s, a) => { listening = true; Console.WriteLine(a.Socket.LocalEndPoint.ToString()); }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var pollerTask = Task.Factory.StartNew(monitor.Start); rep.Bind("tcp://127.0.0.1:5002"); using (var req = contex.CreateRequestSocket()) { 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); Thread.Sleep(200); Assert.IsTrue(listening); Assert.IsTrue(accepted); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(pollerTask.IsCompleted); } } } } }
public void Monitoring() { bool listening = false; bool accepted = false; using (NetMQContext contex = NetMQContext.Create()) { using (var rep = contex.CreateResponseSocket()) { using (NetMQMonitor monitor = new NetMQMonitor(contex, rep, "inproc://rep.inproc", SocketEvent.Accepted | SocketEvent.Listening)) { monitor.Accepted += (s, a) => { accepted = true; Console.WriteLine(a.Socket.RemoteEndPoint.ToString()); }; monitor.Listening += (s, a) => { listening = true; Console.WriteLine(a.Socket.LocalEndPoint.ToString()); }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var pollerTask = Task.Factory.StartNew(monitor.Start); rep.Bind("tcp://127.0.0.1:5002"); using (var req = contex.CreateRequestSocket()) { 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); Thread.Sleep(200); Assert.IsTrue(listening); Assert.IsTrue(accepted); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(pollerTask.IsCompleted); } } } } }
public void StartAsync() { using (var rep = new ResponseSocket()) using (var monitor = new NetMQMonitor(rep, "inproc://foo", SocketEvents.Closed)) { var task = monitor.StartAsync(); Thread.Sleep(200); Assert.AreEqual(TaskStatus.Running, task.Status); monitor.Stop(); Assert.True(task.Wait(TimeSpan.FromMilliseconds(1000))); } }
public void ErrorCodeTest() { bool eventArrived = false; using (NetMQContext contex = NetMQContext.Create()) { using (var req = contex.CreateRequestSocket()) { using (var rep = contex.CreateResponseSocket()) { using (NetMQMonitor monitor = new NetMQMonitor(contex, req, "inproc://rep.inproc", SocketEvent.ConnectDelayed)) { monitor.ConnectDelayed += (s, a) => { eventArrived = true; }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var pollerTask = Task.Factory.StartNew(monitor.Start); rep.Bind("tcp://127.0.0.1:5002"); 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); Thread.Sleep(200); Assert.IsTrue(eventArrived); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(pollerTask.IsCompleted); } } } } }
public void NoHangWhenMonitoringUnboundInprocAddress() { using (var monitor = new NetMQMonitor(new PairSocket(), "inproc://unbound-inproc-address", ownsSocket: true)) { var task = Task.Factory.StartNew(monitor.Start); monitor.Stop(); try { task.Wait(TimeSpan.FromMilliseconds(1000)); Assert.Fail("Exception expected"); } catch (AggregateException ex) { Assert.AreEqual(1, ex.InnerExceptions.Count); Assert.IsTrue(ex.InnerExceptions.Single() is EndpointNotFoundException); } } }
/// <summary> /// Unbind the socket from last endpoint and wait until the underlaying socket was unbound and disposed. /// It will also dispose the NetMQSocket /// </summary> /// <param name="sub"></param> public static void Unbind(this NetMQSocket sub) { using (var monitor = new NetMQMonitor(sub, $"inproc://unbind.wait.{Counter++}", SocketEvents.Closed)) { var monitorTask = Task.Factory.StartNew(monitor.Start); var closed = new ManualResetEventSlim(); monitor.Closed += (sender, args) => closed.Set(); Assert.IsNotNull(sub.Options.LastEndpoint); sub.Unbind(sub.Options.LastEndpoint); closed.Wait(1000); monitor.Stop(); monitorTask.Wait(); } }
public void Monitoring() { using (var rep = new ResponseSocket()) using (var req = new RequestSocket()) using (var monitor = new NetMQMonitor(rep, $"inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening)) { var listening = false; var accepted = false; monitor.Accepted += (s, a) => { accepted = true; }; monitor.Listening += (s, a) => { listening = true; }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var monitorTask = Task.Factory.StartNew(monitor.Start); Thread.Sleep(10); var port = rep.BindRandomPort("tcp://127.0.0.1"); req.Connect("tcp://127.0.0.1:" + port); req.SendFrame("a"); rep.SkipFrame(); rep.SendFrame("b"); req.SkipFrame(); Thread.Sleep(200); Assert.IsTrue(listening); Assert.IsTrue(accepted); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(monitorTask.IsCompleted); } }
public void ErrorCodeTest() { using (var context = NetMQContext.Create()) using (var req = context.CreateRequestSocket()) using (var rep = context.CreateResponseSocket()) using (var monitor = new NetMQMonitor(context, req, "inproc://rep.inproc", SocketEvents.ConnectDelayed)) { var eventArrived = false; monitor.ConnectDelayed += (s, a) => { eventArrived = true; }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var monitorTask = Task.Factory.StartNew(monitor.Start); var port = rep.BindRandomPort("tcp://127.0.0.1"); req.Connect("tcp://127.0.0.1:" + port); req.Send("a"); rep.SkipFrame(); rep.Send("b"); req.SkipFrame(); Thread.Sleep(200); Assert.IsTrue(eventArrived); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(monitorTask.IsCompleted); } }
public void SubsriberCleanupOnUnbind(string address) { for (var i = 0; i < 10; i++) { using (var sub = new SubscriberSocket()) { sub.Bind(address); using (var monitor = new NetMQMonitor(sub, String.Format("inproc://cleanup.test{0}", Guid.NewGuid()), SocketEvents.Closed)) { var monitorTask = Task.Factory.StartNew(monitor.Start); var closed = new ManualResetEventSlim(); monitor.Closed += (sender, args) => closed.Set(); var time = DateTime.Now; sub.Unbind(address); Assert.That(closed.Wait(1000), Is.True, "Unbind failed to report Closed event to the Monitor"); var duration = DateTime.Now - time; monitor.Stop(); monitorTask.Wait(); } } } }
public void Monitoring() { using (var context = NetMQContext.Create()) using (var rep = context.CreateResponseSocket()) using (var req = context.CreateRequestSocket()) using (var monitor = new NetMQMonitor(context, rep, "inproc://rep.inproc", SocketEvent.Accepted | SocketEvent.Listening)) { bool listening = false; bool accepted = false; monitor.Accepted += (s, a) => { accepted = true; }; monitor.Listening += (s, a) => { listening = true; }; monitor.Timeout = TimeSpan.FromMilliseconds(100); var pollerTask = Task.Factory.StartNew(monitor.Start); var port = rep.BindRandomPort("tcp://127.0.0.1"); req.Connect("tcp://127.0.0.1:" + port); req.Send("a"); rep.ReceiveString(); rep.Send("b"); req.ReceiveString(); Thread.Sleep(200); Assert.IsTrue(listening); Assert.IsTrue(accepted); monitor.Stop(); Thread.Sleep(200); Assert.IsTrue(pollerTask.IsCompleted); } }