From a407deffbc36e8df1c79f33763ab90db43547714 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 14 Aug 2018 18:02:05 +1200 Subject: [PATCH] test(PubSubApi): multiple topics --- test/CoreApi/PubSubApiTest.cs | 61 +++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/test/CoreApi/PubSubApiTest.cs b/test/CoreApi/PubSubApiTest.cs index 631e7a9..a54276c 100644 --- a/test/CoreApi/PubSubApiTest.cs +++ b/test/CoreApi/PubSubApiTest.cs @@ -2,6 +2,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; @@ -91,7 +92,7 @@ await ipfs.PubSub.Subscribe(topic, msg => { cs.Cancel(); } - } + } [TestMethod] public async Task Subscribe_Mutiple_Messages() @@ -129,7 +130,7 @@ public async Task Multiple_Subscribe_Mutiple_Messages() var ipfs = TestFixture.Ipfs; var topic = "net-ipfs-api-test-" + Guid.NewGuid().ToString(); var cs = new CancellationTokenSource(); - Action processMessage = (msg) => + Action processMessage = (msg) => { Interlocked.Increment(ref messageCount); }; @@ -173,5 +174,61 @@ await ipfs.PubSub.Subscribe(topic, msg => await Task.Delay(1000); Assert.AreEqual(1, messageCount1); } + + [TestMethod] + public async Task Subscribe_Multiple_Topics() + { + messageCount = 0; + var topicCount = 4; + var topics = new string[topicCount]; + var cancels = new CancellationTokenSource[topicCount]; + var ipfs = TestFixture.Ipfs; + + for (int i = 0; i < topicCount; ++i) + { + topics[i] = "net-ipfs-api-test-" + Guid.NewGuid().ToString(); + cancels[i] = new CancellationTokenSource(); + } + Action processMessage = (msg) => + { + Interlocked.Increment(ref messageCount); + }; + + try + { + // Subscribe to N topics. + for (int i = 0; i < topicCount; ++i) + { + await ipfs.PubSub.Subscribe(topics[i], msg => + { + Interlocked.Increment(ref messageCount); + }, cancels[i].Token); + } + + // Verify topics. + var actualTopics = await ipfs.PubSub.SubscribedTopicsAsync(); + foreach (var topic in actualTopics) + { + CollectionAssert.Contains(topics, topic); + } + + // Publish a message to each topic. + for (int i = 0; i < topicCount; ++i) + { + await ipfs.PubSub.Publish(topics[i], "hello world!"); + } + + // Verify that all messages have been received. + await Task.Delay(1000); + Assert.AreEqual(topicCount, messageCount); + } + finally + { + foreach (var cs in cancels) + { + cs.Cancel(); + } + } + } } }