public void TopicPubSub() { using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { var port = pub.BindRandomPort("tcp://127.0.0.1"); sub.Connect("tcp://127.0.0.1:" + port); sub.Subscribe("A"); // let the subscriber connect to the publisher before sending a message Thread.Sleep(500); pub.SendMoreFrame("A").SendFrame("Hello"); CollectionAssert.AreEqual( new[] {"A", "Hello"}, sub.ReceiveMultipartStrings()); } }
public void MultiplePublishersAndSubscribersOnSameTopic() { using (var pub1 = new PublisherSocket()) using (var pub2 = new PublisherSocket()) using (var sub1 = new SubscriberSocket()) using (var sub2 = new SubscriberSocket()) { int port1 = pub1.BindRandomPort("tcp://127.0.0.1"); int port2 = pub2.BindRandomPort("tcp://127.0.0.1"); sub1.Connect("tcp://127.0.0.1:" + port1); sub1.Connect("tcp://127.0.0.1:" + port2); sub2.Connect("tcp://127.0.0.1:" + port1); sub2.Connect("tcp://127.0.0.1:" + port2); // should subscribe to both sub1.Subscribe("A"); sub2.Subscribe("A"); Thread.Sleep(500); // Send from pub 1 pub1.SendMoreFrame("A").SendFrame("Hello from the first publisher"); CollectionAssert.AreEqual(new[] { "A", "Hello from the first publisher" }, sub1.ReceiveMultipartStrings()); CollectionAssert.AreEqual(new[] { "A", "Hello from the first publisher" }, sub2.ReceiveMultipartStrings()); // Send from pub 2 pub2.SendMoreFrame("A").SendFrame("Hello from the second publisher"); CollectionAssert.AreEqual(new[] { "A", "Hello from the second publisher" }, sub1.ReceiveMultipartStrings()); CollectionAssert.AreEqual(new[] { "A", "Hello from the second publisher" }, sub2.ReceiveMultipartStrings()); } }
public void MultipleSubscribersOnDifferentTopics() { using (var pub = new PublisherSocket()) using (var sub1 = new SubscriberSocket()) using (var sub2 = new SubscriberSocket()) { var port = pub.BindRandomPort("tcp://127.0.0.1"); sub1.Connect("tcp://127.0.0.1:" + port); sub2.Connect("tcp://127.0.0.1:" + port); sub1.Subscribe("1"); sub1.Subscribe("1&2"); sub2.Subscribe("2"); sub2.Subscribe("1&2"); Thread.Sleep(500); pub.SendMoreFrame("1").SendFrame("A"); CollectionAssert.AreEqual(new[] { "1", "A" }, sub1.ReceiveMultipartStrings()); Assert.IsFalse(sub2.TrySkipFrame()); pub.SendMoreFrame("2").SendFrame("B"); Assert.IsFalse(sub1.TrySkipFrame()); CollectionAssert.AreEqual(new[] { "2", "B" }, sub2.ReceiveMultipartStrings()); pub.SendMoreFrame("1&2").SendFrame("C"); CollectionAssert.AreEqual(new[] { "1&2", "C" }, sub1.ReceiveMultipartStrings()); CollectionAssert.AreEqual(new[] { "1&2", "C" }, sub2.ReceiveMultipartStrings()); } }