Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple pubsub topics #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions test/CoreApi/PubSubApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,7 +92,7 @@ await ipfs.PubSub.Subscribe(topic, msg =>
{
cs.Cancel();
}
}
}

[TestMethod]
public async Task Subscribe_Mutiple_Messages()
Expand Down Expand Up @@ -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<IPublishedMessage> processMessage = (msg) =>
Action<IPublishedMessage> processMessage = (msg) =>
{
Interlocked.Increment(ref messageCount);
};
Expand Down Expand Up @@ -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<IPublishedMessage> 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();
}
}
}
}
}