Browse Source

Use subscriptions and unique event names in tests

Update event bus tests to avoid cross-test interference and ensure proper cleanup. In LocalDistributedEventBus_Test and LocalEventBus_Anonymous_Test: reset static handler state in test constructor, subscribe with IDisposable (using var subscription) so handlers are disposed after each test, replace hard-coded event names with generated unique event names, add missing System import, and adjust assertions (remove expectation of AbpException on publish after dispose). Also ensure local event bus subscriptions are stored/disposed. These changes make tests isolated and robust.
pull/25023/head
SALİH ÖZKARA 7 months ago
parent
commit
d7f3015691
  1. 63
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs
  2. 36
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Anonymous_Test.cs

63
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<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
using var subscription = DistributedEventBus.Subscribe<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
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<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
using var subscription = DistributedEventBus.Subscribe<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
var eventName = EventNameAttribute.GetNameOrDefault<MySimpleEventData>();
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<AnonymousEventData>(async (d) =>
{
handleCount++;
await Task.CompletedTask;
})));
await DistributedEventBus.PublishAsync("MyEvent", new MySimpleEventData(1));
await DistributedEventBus.PublishAsync("MyEvent", new Dictionary<string, object>()
await DistributedEventBus.PublishAsync(eventName, new MySimpleEventData(1));
await DistributedEventBus.PublishAsync(eventName, new Dictionary<string, object>()
{
{"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<AnonymousEventData>(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<string, object>()
await DistributedEventBus.PublishAsync(new AnonymousEventData(eventName, new MySimpleEventData(1)));
await DistributedEventBus.PublishAsync(new AnonymousEventData(eventName, new Dictionary<string, object>()
{
{"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<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
using var subscription = DistributedEventBus.Subscribe<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
var eventName = EventNameAttribute.GetNameOrDefault<MySimpleEventData>();
@ -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<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
using var typedSubscription = DistributedEventBus.Subscribe<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
var eventName = EventNameAttribute.GetNameOrDefault<MySimpleEventData>();
var anonymousHandleCount = 0;
DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler<AnonymousEventData>(async (d) =>
using var anonymousSubscription = DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler<AnonymousEventData>(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<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
using var typedSubscription = DistributedEventBus.Subscribe<MySimpleEventData, MySimpleDistributedTransientEventHandler>();
var eventName = EventNameAttribute.GetNameOrDefault<MySimpleEventData>();
var anonymousHandleCount = 0;
DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler<AnonymousEventData>(async (d) =>
using var anonymousSubscription = DistributedEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(new ActionEventHandler<AnonymousEventData>(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<AnonymousEventData>(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<AbpException>(() =>
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<MySimpleEventData>(async (data) =>
using var subscription = DistributedEventBus.Subscribe<MySimpleEventData>(async (data) =>
{
receivedData = data;
await Task.CompletedTask;
@ -207,7 +216,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase
{
var tenantId = Guid.NewGuid();
DistributedEventBus.Subscribe<MySimpleEventData>(GetRequiredService<MySimpleDistributedSingleInstanceEventHandler>());
using var subscription = DistributedEventBus.Subscribe<MySimpleEventData>(GetRequiredService<MySimpleDistributedSingleInstanceEventHandler>());
await DistributedEventBus.PublishAsync(new MySimpleEventData(3, tenantId));
@ -219,7 +228,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase
{
var tenantId = Guid.NewGuid();
DistributedEventBus.Subscribe<EntityCreatedEto<MySimpleEventData>>(GetRequiredService<MySimpleDistributedSingleInstanceEventHandler>());
using var subscription = DistributedEventBus.Subscribe<EntityCreatedEto<MySimpleEventData>>(GetRequiredService<MySimpleDistributedSingleInstanceEventHandler>());
await DistributedEventBus.PublishAsync(new MySimpleEventData(3, tenantId));
@ -231,7 +240,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase
{
var tenantId = Guid.NewGuid();
DistributedEventBus.Subscribe<MySimpleEto>(GetRequiredService<MySimpleDistributedSingleInstanceEventHandler>());
using var subscription = DistributedEventBus.Subscribe<MySimpleEto>(GetRequiredService<MySimpleDistributedSingleInstanceEventHandler>());
await DistributedEventBus.PublishAsync(new MySimpleEto
{
@ -249,10 +258,10 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase
{
var localEventBus = GetRequiredService<ILocalEventBus>();
localEventBus.Subscribe<DistributedEventSent, DistributedEventHandles>();
localEventBus.Subscribe<DistributedEventReceived, DistributedEventHandles>();
using var distributedEventSentSubscription = localEventBus.Subscribe<DistributedEventSent, DistributedEventHandles>();
using var distributedEventReceivedSubscription = localEventBus.Subscribe<DistributedEventReceived, DistributedEventHandles>();
DistributedEventBus.Subscribe<MyEventDate, MyEventHandle>();
using var subscription = DistributedEventBus.Subscribe<MyEventDate, MyEventHandle>();
using (var uow = GetRequiredService<IUnitOfWorkManager>().Begin())
{

36
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<AnonymousEventData>(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<MySimpleEventData>(async (data) =>
using var subscription = LocalEventBus.Subscribe<MySimpleEventData>(async (data) =>
{
handleCount++;
await Task.CompletedTask;
@ -48,7 +50,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase
{
MySimpleEventData? receivedData = null;
LocalEventBus.Subscribe<MySimpleEventData>(async (data) =>
using var subscription = LocalEventBus.Subscribe<MySimpleEventData>(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<MySimpleEventData>(async (data) =>
using var typedSubscription = LocalEventBus.Subscribe<MySimpleEventData>(async (data) =>
{
typedHandleCount++;
await Task.CompletedTask;
@ -78,7 +80,7 @@ public class LocalEventBus_Anonymous_Test : EventBusTestBase
var eventName = EventNameAttribute.GetNameOrDefault<MySimpleEventData>();
LocalEventBus.Subscribe(eventName,
using var anonymousSubscription = LocalEventBus.Subscribe(eventName,
new SingleInstanceHandlerFactory(new ActionEventHandler<AnonymousEventData>(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<AnonymousEventData>(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<AbpException>(() =>
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<AnonymousEventData>(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<Dictionary<string, object?>>();
@ -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<AnonymousEventData>(async (d) =>
{
receivedData = d.ConvertToTypedObject<MySimpleEventData>();
await Task.CompletedTask;
})));
await LocalEventBus.PublishAsync("TestEvent", new MySimpleEventData(99));
await LocalEventBus.PublishAsync(eventName, new MySimpleEventData(99));
receivedData.ShouldNotBeNull();
receivedData.Value.ShouldBe(99);

Loading…
Cancel
Save