Browse Source

Merge pull request #22056 from abpframework/AddOrReplaceLocalOrDistributedEvent

pull/22074/head
maliming 2 years ago
committed by GitHub
parent
commit
71b644628a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs
  2. 58
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs

8
framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs

@ -139,15 +139,16 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
_isCompleting = true; _isCompleting = true;
await SaveChangesAsync(cancellationToken); await SaveChangesAsync(cancellationToken);
DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates));
LocalEvents.AddRange(GetEventsRecords(LocalEventWithPredicates)); LocalEvents.AddRange(GetEventsRecords(LocalEventWithPredicates));
LocalEventWithPredicates.Clear();
DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates));
DistributedEventWithPredicates.Clear();
while (LocalEvents.Any() || DistributedEvents.Any()) while (LocalEvents.Any() || DistributedEvents.Any())
{ {
if (LocalEvents.Any()) if (LocalEvents.Any())
{ {
var localEventsToBePublished = LocalEvents.OrderBy(e => e.EventOrder).ToArray(); var localEventsToBePublished = LocalEvents.OrderBy(e => e.EventOrder).ToArray();
LocalEventWithPredicates.Clear();
LocalEvents.Clear(); LocalEvents.Clear();
await UnitOfWorkEventPublisher.PublishLocalEventsAsync( await UnitOfWorkEventPublisher.PublishLocalEventsAsync(
localEventsToBePublished localEventsToBePublished
@ -157,7 +158,6 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
if (DistributedEvents.Any()) if (DistributedEvents.Any())
{ {
var distributedEventsToBePublished = DistributedEvents.OrderBy(e => e.EventOrder).ToArray(); var distributedEventsToBePublished = DistributedEvents.OrderBy(e => e.EventOrder).ToArray();
DistributedEventWithPredicates.Clear();
DistributedEvents.Clear(); DistributedEvents.Clear();
await UnitOfWorkEventPublisher.PublishDistributedEventsAsync( await UnitOfWorkEventPublisher.PublishDistributedEventsAsync(
distributedEventsToBePublished distributedEventsToBePublished
@ -167,7 +167,9 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
await SaveChangesAsync(cancellationToken); await SaveChangesAsync(cancellationToken);
LocalEvents.AddRange(GetEventsRecords(LocalEventWithPredicates)); LocalEvents.AddRange(GetEventsRecords(LocalEventWithPredicates));
LocalEventWithPredicates.Clear();
DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates)); DistributedEvents.AddRange(GetEventsRecords(DistributedEventWithPredicates));
DistributedEventWithPredicates.Clear();
} }
await CommitTransactionsAsync(cancellationToken); await CommitTransactionsAsync(cancellationToken);

58
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs

@ -21,6 +21,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
protected readonly IRepository<AppEntityWithNavigations, Guid> AppEntityWithNavigationsRepository; protected readonly IRepository<AppEntityWithNavigations, Guid> AppEntityWithNavigationsRepository;
protected readonly ILocalEventBus LocalEventBus; protected readonly ILocalEventBus LocalEventBus;
protected readonly IDistributedEventBus DistributedEventBus; protected readonly IDistributedEventBus DistributedEventBus;
protected readonly IUnitOfWorkManager UnitOfWorkManager;
protected DomainEvents_Tests() protected DomainEvents_Tests()
{ {
@ -28,6 +29,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
AppEntityWithNavigationsRepository = GetRequiredService<IRepository<AppEntityWithNavigations, Guid>>(); AppEntityWithNavigationsRepository = GetRequiredService<IRepository<AppEntityWithNavigations, Guid>>();
LocalEventBus = GetRequiredService<ILocalEventBus>(); LocalEventBus = GetRequiredService<ILocalEventBus>();
DistributedEventBus = GetRequiredService<IDistributedEventBus>(); DistributedEventBus = GetRequiredService<IDistributedEventBus>();
UnitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
} }
[Fact] [Fact]
@ -176,6 +178,52 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
isDistributedEventTriggered.ShouldBeTrue(); isDistributedEventTriggered.ShouldBeTrue();
} }
[Fact]
public async Task Should_Trigger_Event_That_Publish_In_Event_Handler()
{
//Arrange
var event1Triggered = false;
var event2Triggered = false;
var event3Triggered = false;
var event4Triggered = false;
LocalEventBus.Subscribe<MyCustomEventData>(async data =>
{
event1Triggered = true;
await DistributedEventBus.PublishAsync(new MyCustomEventData3 { Value = "42" });
});
DistributedEventBus.Subscribe<MyCustomEventData2>(async data =>
{
event2Triggered = true;
await LocalEventBus.PublishAsync(new MyCustomEventData4 { Value = "42" });
});
LocalEventBus.Subscribe<MyCustomEventData3>(async data =>
{
event3Triggered = true;
});
DistributedEventBus.Subscribe<MyCustomEventData4>(async data =>
{
event4Triggered = true;
});
//Act
using (var uow = UnitOfWorkManager.Begin(requiresNew: true))
{
await LocalEventBus.PublishAsync(new MyCustomEventData { Value = "42" });
await DistributedEventBus.PublishAsync(new MyCustomEventData2 { Value = "42" });
await uow.CompleteAsync();
}
//Assert
event1Triggered.ShouldBeTrue();
event2Triggered.ShouldBeTrue();
event3Triggered.ShouldBeTrue();
event4Triggered.ShouldBeTrue();
}
private class MyCustomEventData private class MyCustomEventData
{ {
public string Value { get; set; } public string Value { get; set; }
@ -185,6 +233,16 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
{ {
public string Value { get; set; } 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<TStartupModule> : TestAppTestBase<TStartupModule> public abstract class AbpEntityChangeOptions_DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStartupModule>

Loading…
Cancel
Save