Skip to content

Commit

Permalink
Add instance id support to multicast (#248).
Browse files Browse the repository at this point in the history
  • Loading branch information
dicky authored and dicky committed May 27, 2020
1 parent 2a35574 commit 8959617
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DotNetifyLib.Core/VMController/VMFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private BaseVM GetMulticastInstance(TypeHelper vmType, string vmInstanceId)
string key = vmType.FullName;
MulticastVM vm = null;

if (!string.IsNullOrWhiteSpace(vmInstanceId))
key += $"${vmInstanceId}";

lock (_keyBasedLock.GetOrAdd(key, _ => new object()))
{
if (!_memoryCache.TryGetValue(key, out HashSet<MulticastVM> vmCollections))
Expand Down
38 changes: 38 additions & 0 deletions UnitTests/MulticastVMTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,43 @@ public void MulticastVM_Group()
Assert.IsFalse(vm.DisposeCalls[0]);
Assert.IsTrue(vm.DisposeCalls[1]);
}

[TestMethod]
public async Task MulticastVM_MultiInstances()
{
var client1 = _hubEmulator.CreateClient();
var client2 = _hubEmulator.CreateClient();
var client3 = _hubEmulator.CreateClient();
var client4 = _hubEmulator.CreateClient();

client1.Connect(nameof(MulticastTestVM) + "$A");
client2.Connect(nameof(MulticastTestVM) + "$A");
client3.Connect(nameof(MulticastTestVM) + "$B");
client4.Connect(nameof(MulticastTestVM) + "$B");

var client2ResponsesTask = client2.ListenAsync();
var client3ResponsesTask = client3.ListenAsync();

var update = new Dictionary<string, object>() { { nameof(MulticastTestVM.Message), "Goodbye" } };
client1.Dispatch(update);

var client2Response = (await client2ResponsesTask).As<dynamic>();
var client3Response = await client3ResponsesTask;

Assert.AreEqual("Goodbye", (string) client2Response.Message);
Assert.AreEqual(0, client3Response.Count);

client2ResponsesTask = client2.ListenAsync();
client3ResponsesTask = client3.ListenAsync();

update = new Dictionary<string, object>() { { nameof(MulticastTestVM.Message), "Adios" } };
client4.Dispatch(update);

var client2Response2 = await client2ResponsesTask;
var client3Response2 = (await client3ResponsesTask).As<dynamic>();

Assert.AreEqual(0, client2Response2.Count);
Assert.AreEqual("Adios", (string) client3Response2.Message);
}
}
}

0 comments on commit 8959617

Please sign in to comment.