From 5aaf55579142c6915dc8d22a6c9b0896676e793d Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 1 Feb 2025 13:37:25 +0800 Subject: [PATCH 1/2] Clear event predicates after `GetEventsRecords` method. Resolve #22054 --- .../Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs | 8 ++- .../Abp/TestApp/Testing/DomainEvents_Tests.cs | 58 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs b/framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs index 6d809a73bc9..92134e5c975 100644 --- a/framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs +++ b/framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs @@ -139,15 +139,16 @@ public virtual async Task CompleteAsync(CancellationToken cancellationToken = de _isCompleting = true; await SaveChangesAsync(cancellationToken); - DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates)); LocalEvents.AddRange(GetEventsRecords(LocalEventWithPredicates)); + LocalEventWithPredicates.Clear(); + DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates)); + DistributedEventWithPredicates.Clear(); while (LocalEvents.Any() || DistributedEvents.Any()) { if (LocalEvents.Any()) { var localEventsToBePublished = LocalEvents.OrderBy(e => e.EventOrder).ToArray(); - LocalEventWithPredicates.Clear(); LocalEvents.Clear(); await UnitOfWorkEventPublisher.PublishLocalEventsAsync( localEventsToBePublished @@ -157,7 +158,6 @@ await UnitOfWorkEventPublisher.PublishLocalEventsAsync( if (DistributedEvents.Any()) { var distributedEventsToBePublished = DistributedEvents.OrderBy(e => e.EventOrder).ToArray(); - DistributedEventWithPredicates.Clear(); DistributedEvents.Clear(); await UnitOfWorkEventPublisher.PublishDistributedEventsAsync( distributedEventsToBePublished @@ -167,7 +167,9 @@ await UnitOfWorkEventPublisher.PublishDistributedEventsAsync( await SaveChangesAsync(cancellationToken); LocalEvents.AddRange(GetEventsRecords(LocalEventWithPredicates)); + LocalEventWithPredicates.Clear(); DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates)); + DistributedEventWithPredicates.Clear(); } await CommitTransactionsAsync(cancellationToken); diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs index e5e3ad5cb60..8720f1ceb69 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs @@ -21,6 +21,7 @@ public abstract class DomainEvents_Tests : TestAppTestBase AppEntityWithNavigationsRepository; protected readonly ILocalEventBus LocalEventBus; protected readonly IDistributedEventBus DistributedEventBus; + protected readonly IUnitOfWorkManager UnitOfWorkManager; protected DomainEvents_Tests() { @@ -28,6 +29,7 @@ protected DomainEvents_Tests() AppEntityWithNavigationsRepository = GetRequiredService>(); LocalEventBus = GetRequiredService(); DistributedEventBus = GetRequiredService(); + UnitOfWorkManager = GetRequiredService(); } [Fact] @@ -176,6 +178,52 @@ await WithUnitOfWorkAsync(async () => isDistributedEventTriggered.ShouldBeTrue(); } + [Fact] + public async Task Should_AddOrReplace_Event_Records_In_Uow_Test() + { + //Arrange + var event1Triggered = false; + var event2Triggered = false; + var event3Triggered = false; + var event4Triggered = false; + + LocalEventBus.Subscribe(async data => + { + event1Triggered = true; + UnitOfWorkManager.Current!.AddOrReplaceDistributedEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData3), new MyCustomEventData3 { Value = "42" }, 2)); + }); + + DistributedEventBus.Subscribe(async data => + { + event2Triggered = true; + UnitOfWorkManager.Current!.AddOrReplaceLocalEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData4), new MyCustomEventData4 { Value = "42" }, 2)); + }); + + LocalEventBus.Subscribe(async data => + { + event3Triggered = true; + }); + + DistributedEventBus.Subscribe(async data => + { + event4Triggered = true; + }); + + //Act + using (var uow = UnitOfWorkManager.Begin(requiresNew: true)) + { + UnitOfWorkManager.Current!.AddOrReplaceLocalEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData), new MyCustomEventData { Value = "42" }, 1)); + UnitOfWorkManager.Current!.AddOrReplaceDistributedEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData2), new MyCustomEventData2 { Value = "42" }, 1)); + await uow.CompleteAsync(); + } + + //Assert + event1Triggered.ShouldBeTrue(); + event2Triggered.ShouldBeTrue(); + event3Triggered.ShouldBeTrue(); + event4Triggered.ShouldBeTrue(); + } + private class MyCustomEventData { public string Value { get; set; } @@ -185,6 +233,16 @@ private class MyCustomEventData2 { public string Value { get; set; } } + + private class MyCustomEventData3 + { + public string Value { get; set; } + } + + private class MyCustomEventData4 + { + public string Value { get; set; } + } } public abstract class AbpEntityChangeOptions_DomainEvents_Tests : TestAppTestBase From 2fb1d150387b5d80cd802616054d6518438236f1 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 1 Feb 2025 14:15:29 +0800 Subject: [PATCH 2/2] Update DomainEvents_Tests.cs --- .../Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs index 8720f1ceb69..8ac64d40c9a 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs @@ -179,7 +179,7 @@ await WithUnitOfWorkAsync(async () => } [Fact] - public async Task Should_AddOrReplace_Event_Records_In_Uow_Test() + public async Task Should_Trigger_Event_That_Publish_In_Event_Handler() { //Arrange var event1Triggered = false; @@ -190,13 +190,13 @@ public async Task Should_AddOrReplace_Event_Records_In_Uow_Test() LocalEventBus.Subscribe(async data => { event1Triggered = true; - UnitOfWorkManager.Current!.AddOrReplaceDistributedEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData3), new MyCustomEventData3 { Value = "42" }, 2)); + await DistributedEventBus.PublishAsync(new MyCustomEventData3 { Value = "42" }); }); DistributedEventBus.Subscribe(async data => { event2Triggered = true; - UnitOfWorkManager.Current!.AddOrReplaceLocalEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData4), new MyCustomEventData4 { Value = "42" }, 2)); + await LocalEventBus.PublishAsync(new MyCustomEventData4 { Value = "42" }); }); LocalEventBus.Subscribe(async data => @@ -212,8 +212,8 @@ public async Task Should_AddOrReplace_Event_Records_In_Uow_Test() //Act using (var uow = UnitOfWorkManager.Begin(requiresNew: true)) { - UnitOfWorkManager.Current!.AddOrReplaceLocalEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData), new MyCustomEventData { Value = "42" }, 1)); - UnitOfWorkManager.Current!.AddOrReplaceDistributedEvent(new UnitOfWorkEventRecord(typeof(MyCustomEventData2), new MyCustomEventData2 { Value = "42" }, 1)); + await LocalEventBus.PublishAsync(new MyCustomEventData { Value = "42" }); + await DistributedEventBus.PublishAsync(new MyCustomEventData2 { Value = "42" }); await uow.CompleteAsync(); }