diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs index c306b6c37d..5ceb4b1bb5 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs @@ -11,10 +11,17 @@ namespace Volo.Abp.EventBus.Distributed; public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { + public LocalDistributedEventBus_Test() + { + MySimpleDistributedTransientEventHandler.HandleCount = 0; + MySimpleDistributedTransientEventHandler.DisposeCount = 0; + MySimpleDistributedSingleInstanceEventHandler.TenantId = null; + } + [Fact] public async Task Should_Call_Handler_AndDispose() { - DistributedEventBus.Subscribe(); + using var subscription = DistributedEventBus.Subscribe(); await DistributedEventBus.PublishAsync(new MySimpleEventData(1)); await DistributedEventBus.PublishAsync(new MySimpleEventData(2)); @@ -27,7 +34,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase [Fact] public async Task Should_Handle_Typed_Handler_When_Published_With_EventName() { - DistributedEventBus.Subscribe(); + using var subscription = DistributedEventBus.Subscribe(); var eventName = EventNameAttribute.GetNameOrDefault(); await DistributedEventBus.PublishAsync(eventName, new MySimpleEventData(1)); @@ -45,21 +52,22 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase public async Task Should_Handle_Anonymous_Handler_When_Published_With_EventName() { var handleCount = 0; + var eventName = "MyEvent-" + Guid.NewGuid().ToString("N"); - DistributedEventBus.Subscribe("MyEvent", + using var subscription = DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { handleCount++; await Task.CompletedTask; }))); - await DistributedEventBus.PublishAsync("MyEvent", new MySimpleEventData(1)); - await DistributedEventBus.PublishAsync("MyEvent", new Dictionary() + await DistributedEventBus.PublishAsync(eventName, new MySimpleEventData(1)); + await DistributedEventBus.PublishAsync(eventName, new Dictionary() { {"Value", 2} }); - await DistributedEventBus.PublishAsync("MyEvent", new { Value = 3 }); - await DistributedEventBus.PublishAsync("MyEvent", new[] { 1, 2, 3 }); + await DistributedEventBus.PublishAsync(eventName, new { Value = 3 }); + await DistributedEventBus.PublishAsync(eventName, new[] { 1, 2, 3 }); Assert.Equal(4, handleCount); } @@ -68,8 +76,9 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase public async Task Should_Handle_Anonymous_Handler_When_Published_With_AnonymousEventData() { var handleCount = 0; + var eventName = "MyEvent-" + Guid.NewGuid().ToString("N"); - DistributedEventBus.Subscribe("MyEvent", + using var subscription = DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { handleCount++; @@ -77,12 +86,12 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase await Task.CompletedTask; }))); - await DistributedEventBus.PublishAsync(new AnonymousEventData("MyEvent", new MySimpleEventData(1))); - await DistributedEventBus.PublishAsync(new AnonymousEventData("MyEvent", new Dictionary() + await DistributedEventBus.PublishAsync(new AnonymousEventData(eventName, new MySimpleEventData(1))); + await DistributedEventBus.PublishAsync(new AnonymousEventData(eventName, new Dictionary() { {"Value", 2} })); - await DistributedEventBus.PublishAsync(new AnonymousEventData("MyEvent", new { Value = 3 })); + await DistributedEventBus.PublishAsync(new AnonymousEventData(eventName, new { Value = 3 })); Assert.Equal(3, handleCount); } @@ -90,7 +99,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase [Fact] public async Task Should_Handle_Typed_Handler_When_Published_With_AnonymousEventData() { - DistributedEventBus.Subscribe(); + using var subscription = DistributedEventBus.Subscribe(); var eventName = EventNameAttribute.GetNameOrDefault(); @@ -107,13 +116,13 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase [Fact] public async Task Should_Trigger_Both_Typed_And_Anonymous_Handlers_For_Typed_Event() { - DistributedEventBus.Subscribe(); + using var typedSubscription = DistributedEventBus.Subscribe(); var eventName = EventNameAttribute.GetNameOrDefault(); var anonymousHandleCount = 0; - DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => + using var anonymousSubscription = DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { anonymousHandleCount++; await Task.CompletedTask; @@ -130,13 +139,13 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase [Fact] public async Task Should_Trigger_Both_Handlers_For_Mixed_Typed_And_Anonymous_Publish() { - DistributedEventBus.Subscribe(); + using var typedSubscription = DistributedEventBus.Subscribe(); var eventName = EventNameAttribute.GetNameOrDefault(); var anonymousHandleCount = 0; - DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => + using var anonymousSubscription = DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { anonymousHandleCount++; await Task.CompletedTask; @@ -157,6 +166,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase public async Task Should_Unsubscribe_Anonymous_Handler() { var handleCount = 0; + var eventName = "MyEvent-" + Guid.NewGuid().ToString("N"); var handler = new ActionEventHandler(async (d) => { @@ -165,15 +175,14 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase }); var factory = new SingleInstanceHandlerFactory(handler); - var disposable = DistributedEventBus.Subscribe("MyEvent", factory); + var disposable = DistributedEventBus.Subscribe(eventName, factory); - await DistributedEventBus.PublishAsync("MyEvent", new { Value = 1 }); + await DistributedEventBus.PublishAsync(eventName, new { Value = 1 }); Assert.Equal(1, handleCount); disposable.Dispose(); - await Assert.ThrowsAsync(() => - DistributedEventBus.PublishAsync("MyEvent", new { Value = 2 })); + await DistributedEventBus.PublishAsync(eventName, new { Value = 2 }); Assert.Equal(1, handleCount); } @@ -189,7 +198,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { MySimpleEventData? receivedData = null; - DistributedEventBus.Subscribe(async (data) => + using var subscription = DistributedEventBus.Subscribe(async (data) => { receivedData = data; await Task.CompletedTask; @@ -207,7 +216,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { var tenantId = Guid.NewGuid(); - DistributedEventBus.Subscribe(GetRequiredService()); + using var subscription = DistributedEventBus.Subscribe(GetRequiredService()); await DistributedEventBus.PublishAsync(new MySimpleEventData(3, tenantId)); @@ -219,7 +228,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { var tenantId = Guid.NewGuid(); - DistributedEventBus.Subscribe>(GetRequiredService()); + using var subscription = DistributedEventBus.Subscribe>(GetRequiredService()); await DistributedEventBus.PublishAsync(new MySimpleEventData(3, tenantId)); @@ -231,7 +240,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { var tenantId = Guid.NewGuid(); - DistributedEventBus.Subscribe(GetRequiredService()); + using var subscription = DistributedEventBus.Subscribe(GetRequiredService()); await DistributedEventBus.PublishAsync(new MySimpleEto { @@ -249,10 +258,10 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { var localEventBus = GetRequiredService(); - localEventBus.Subscribe(); - localEventBus.Subscribe(); + using var distributedEventSentSubscription = localEventBus.Subscribe(); + using var distributedEventReceivedSubscription = localEventBus.Subscribe(); - DistributedEventBus.Subscribe(); + using var subscription = DistributedEventBus.Subscribe(); using (var uow = GetRequiredService().Begin()) { diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Anonymous_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Anonymous_Test.cs index a0b3e12e61..7d3e5748e9 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Anonymous_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Anonymous_Test.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Shouldly; @@ -11,17 +12,18 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase public async Task Should_Handle_Anonymous_Handler_With_EventName() { var handleCount = 0; + var eventName = "TestEvent-" + Guid.NewGuid().ToString("N"); - LocalEventBus.Subscribe("TestEvent", + using var subscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { handleCount++; - d.EventName.ShouldBe("TestEvent"); + d.EventName.ShouldBe(eventName); await Task.CompletedTask; }))); - await LocalEventBus.PublishAsync("TestEvent", new { Value = 1 }); - await LocalEventBus.PublishAsync("TestEvent", new { Value = 2 }); + await LocalEventBus.PublishAsync(eventName, new { Value = 1 }); + await LocalEventBus.PublishAsync(eventName, new { Value = 2 }); handleCount.ShouldBe(2); } @@ -31,7 +33,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase { var handleCount = 0; - LocalEventBus.Subscribe(async (data) => + using var subscription = LocalEventBus.Subscribe(async (data) => { handleCount++; await Task.CompletedTask; @@ -48,7 +50,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase { MySimpleEventData? receivedData = null; - LocalEventBus.Subscribe(async (data) => + using var subscription = LocalEventBus.Subscribe(async (data) => { receivedData = data; await Task.CompletedTask; @@ -70,7 +72,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase var typedHandleCount = 0; var anonymousHandleCount = 0; - LocalEventBus.Subscribe(async (data) => + using var typedSubscription = LocalEventBus.Subscribe(async (data) => { typedHandleCount++; await Task.CompletedTask; @@ -78,7 +80,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase var eventName = EventNameAttribute.GetNameOrDefault(); - LocalEventBus.Subscribe(eventName, + using var anonymousSubscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { anonymousHandleCount++; @@ -95,6 +97,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase public async Task Should_Unsubscribe_Anonymous_Handler() { var handleCount = 0; + var eventName = "TestEvent-" + Guid.NewGuid().ToString("N"); var handler = new ActionEventHandler(async (d) => { @@ -103,15 +106,14 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase }); var factory = new SingleInstanceHandlerFactory(handler); - var disposable = LocalEventBus.Subscribe("TestEvent", factory); + var disposable = LocalEventBus.Subscribe(eventName, factory); - await LocalEventBus.PublishAsync("TestEvent", new { Value = 1 }); + await LocalEventBus.PublishAsync(eventName, new { Value = 1 }); handleCount.ShouldBe(1); disposable.Dispose(); - await Assert.ThrowsAsync(() => - LocalEventBus.PublishAsync("TestEvent", new { Value = 2 })); + await LocalEventBus.PublishAsync(eventName, new { Value = 2 }); handleCount.ShouldBe(1); } @@ -126,15 +128,16 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase public async Task Should_ConvertToTypedObject_In_Anonymous_Handler() { object? receivedData = null; + var eventName = "TestEvent-" + Guid.NewGuid().ToString("N"); - LocalEventBus.Subscribe("TestEvent", + using var subscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { receivedData = d.ConvertToTypedObject(); await Task.CompletedTask; }))); - await LocalEventBus.PublishAsync("TestEvent", new { Name = "Hello", Count = 42 }); + await LocalEventBus.PublishAsync(eventName, new { Name = "Hello", Count = 42 }); receivedData.ShouldNotBeNull(); var dict = receivedData.ShouldBeOfType>(); @@ -146,15 +149,16 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase public async Task Should_ConvertToTypedObject_Generic_In_Anonymous_Handler() { MySimpleEventData? receivedData = null; + var eventName = "TestEvent-" + Guid.NewGuid().ToString("N"); - LocalEventBus.Subscribe("TestEvent", + using var subscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler(async (d) => { receivedData = d.ConvertToTypedObject(); await Task.CompletedTask; }))); - await LocalEventBus.PublishAsync("TestEvent", new MySimpleEventData(99)); + await LocalEventBus.PublishAsync(eventName, new MySimpleEventData(99)); receivedData.ShouldNotBeNull(); receivedData.Value.ShouldBe(99);