From 895961723fb29c9fe5554093b9ba1ab48e67129d Mon Sep 17 00:00:00 2001 From: dicky Date: Tue, 26 May 2020 23:20:04 -0700 Subject: [PATCH] Add instance id support to multicast (#248). --- DotNetifyLib.Core/VMController/VMFactory.cs | 3 ++ UnitTests/MulticastVMTest.cs | 38 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/DotNetifyLib.Core/VMController/VMFactory.cs b/DotNetifyLib.Core/VMController/VMFactory.cs index 9150b57c..08ce9505 100644 --- a/DotNetifyLib.Core/VMController/VMFactory.cs +++ b/DotNetifyLib.Core/VMController/VMFactory.cs @@ -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 vmCollections)) diff --git a/UnitTests/MulticastVMTest.cs b/UnitTests/MulticastVMTest.cs index e3f24ae4..cb43fa55 100644 --- a/UnitTests/MulticastVMTest.cs +++ b/UnitTests/MulticastVMTest.cs @@ -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() { { nameof(MulticastTestVM.Message), "Goodbye" } }; + client1.Dispatch(update); + + var client2Response = (await client2ResponsesTask).As(); + var client3Response = await client3ResponsesTask; + + Assert.AreEqual("Goodbye", (string) client2Response.Message); + Assert.AreEqual(0, client3Response.Count); + + client2ResponsesTask = client2.ListenAsync(); + client3ResponsesTask = client3.ListenAsync(); + + update = new Dictionary() { { nameof(MulticastTestVM.Message), "Adios" } }; + client4.Dispatch(update); + + var client2Response2 = await client2ResponsesTask; + var client3Response2 = (await client3ResponsesTask).As(); + + Assert.AreEqual(0, client2Response2.Count); + Assert.AreEqual("Adios", (string) client3Response2.Message); + } } } \ No newline at end of file