diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs index b63133a388..2551fb54fa 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs @@ -28,30 +28,6 @@ public class LocalDistributedEventBus : IDistributedEventBus, ISingletonDependen ServiceScopeFactory = serviceScopeFactory; AbpDistributedEventBusOptions = distributedEventBusOptions.Value; Subscribe(distributedEventBusOptions.Value.Handlers); - - // For unit testing - if (localEventBus is LocalEventBus eventBus) - { - eventBus.OnEventHandleInvoking = async (eventType, eventData) => - { - await localEventBus.PublishAsync(new DistributedEventReceived() - { - Source = DistributedEventSource.Direct, - EventName = EventNameAttribute.GetNameOrDefault(eventType), - EventData = eventData - }, onUnitOfWorkComplete: false); - }; - - eventBus.OnPublishing = async (eventType, eventData) => - { - await localEventBus.PublishAsync(new DistributedEventSent() - { - Source = DistributedEventSource.Direct, - EventName = EventNameAttribute.GetNameOrDefault(eventType), - EventData = eventData - }, onUnitOfWorkComplete: false); - }; - } } public virtual void Subscribe(ITypeList handlers) diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs index c24627cdc5..48f70ac8c1 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; -using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.Reflection; using Volo.Abp.Threading; @@ -175,45 +174,4 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency return false; } - - // Internal for unit testing - internal Func? OnEventHandleInvoking { get; set; } - - // Internal for unit testing - protected async override Task InvokeEventHandlerAsync(IEventHandler eventHandler, object eventData, Type eventType) - { - if (OnEventHandleInvoking != null && eventType != typeof(DistributedEventSent) && eventType != typeof(DistributedEventReceived)) - { - await OnEventHandleInvoking(eventType, eventData); - } - - await base.InvokeEventHandlerAsync(eventHandler, eventData, eventType); - } - - // Internal for unit testing - internal Func? OnPublishing { get; set; } - - // For unit testing - public async override Task PublishAsync( - Type eventType, - object eventData, - bool onUnitOfWorkComplete = true) - { - if (onUnitOfWorkComplete && UnitOfWorkManager.Current != null) - { - AddToUnitOfWork( - UnitOfWorkManager.Current, - new UnitOfWorkEventRecord(eventType, eventData, EventOrderGenerator.GetNext()) - ); - return; - } - - // For unit testing - if (OnPublishing != null && eventType != typeof(DistributedEventSent) && eventType != typeof(DistributedEventReceived)) - { - await OnPublishing(eventType, eventData); - } - - await PublishToEventBusAsync(eventType, eventData); - } } 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 bbb8838cf6..880e2810cd 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 @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.EventBus.Local; using Volo.Abp.Uow; @@ -9,6 +11,12 @@ namespace Volo.Abp.EventBus.Distributed; public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase { + protected override void AfterAddApplication(IServiceCollection services) + { + services.Replace(ServiceDescriptor.Singleton()); + base.AfterAddApplication(services); + } + [Fact] public async Task Should_Call_Handler_AndDispose() { @@ -67,6 +75,30 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase [Fact] public async Task DistributedEventSentAndReceived_Test() { + var localEventBus = GetRequiredService(); + if (localEventBus is UnitTestLocalEventBus eventBus) + { + eventBus.OnEventHandleInvoking = async (eventType, eventData) => + { + await localEventBus.PublishAsync(new DistributedEventReceived() + { + Source = DistributedEventSource.Direct, + EventName = EventNameAttribute.GetNameOrDefault(eventType), + EventData = eventData + }, onUnitOfWorkComplete: false); + }; + + eventBus.OnPublishing = async (eventType, eventData) => + { + await localEventBus.PublishAsync(new DistributedEventSent() + { + Source = DistributedEventSource.Direct, + EventName = EventNameAttribute.GetNameOrDefault(eventType), + EventData = eventData + }, onUnitOfWorkComplete: false); + }; + } + GetRequiredService().Subscribe(); GetRequiredService().Subscribe(); diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/UnitTestLocalEventBus.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/UnitTestLocalEventBus.cs new file mode 100644 index 0000000000..1511d6a071 --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/UnitTestLocalEventBus.cs @@ -0,0 +1,62 @@ +using System; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.EventBus.Local; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Uow; + +namespace Volo.Abp.EventBus.Distributed; + +/// +/// This class is used in unit tests and supports to publish DistributedEventSent and DistributedEventReceived events. +/// +public class UnitTestLocalEventBus : LocalEventBus +{ + public UnitTestLocalEventBus( + [NotNull] IOptions options, + [NotNull] IServiceScopeFactory serviceScopeFactory, + [NotNull] ICurrentTenant currentTenant, + [NotNull] IUnitOfWorkManager unitOfWorkManager, + [NotNull] IEventHandlerInvoker eventHandlerInvoker) + : base(options, serviceScopeFactory, currentTenant, unitOfWorkManager, eventHandlerInvoker) + { + } + + public Func OnEventHandleInvoking { get; set; } + + protected async override Task InvokeEventHandlerAsync(IEventHandler eventHandler, object eventData, Type eventType) + { + if (OnEventHandleInvoking != null && eventType != typeof(DistributedEventSent) && eventType != typeof(DistributedEventReceived)) + { + await OnEventHandleInvoking(eventType, eventData); + } + + await base.InvokeEventHandlerAsync(eventHandler, eventData, eventType); + } + + public Func OnPublishing { get; set; } + + public async override Task PublishAsync( + Type eventType, + object eventData, + bool onUnitOfWorkComplete = true) + { + if (onUnitOfWorkComplete && UnitOfWorkManager.Current != null) + { + AddToUnitOfWork( + UnitOfWorkManager.Current, + new UnitOfWorkEventRecord(eventType, eventData, EventOrderGenerator.GetNext()) + ); + return; + } + + if (OnPublishing != null && eventType != typeof(DistributedEventSent) && eventType != typeof(DistributedEventReceived)) + { + await OnPublishing(eventType, eventData); + } + + await PublishToEventBusAsync(eventType, eventData); + } +}