committed by
GitHub
22 changed files with 645 additions and 60 deletions
@ -0,0 +1,153 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.Abp.EventBus.Boxes.Dtm.Models; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore; |
|||
|
|||
public class DtmOutboxTests : DtmEntityFrameworkCoreTestBase |
|||
{ |
|||
protected IUnitOfWorkManager UnitOfWorkManager { get; } |
|||
protected IDistributedEventBus DistributedEventBus { get; } |
|||
protected IDtmMsgBarrierManager<IEfCoreDbContext> DtmMsgBarrierManager { get; } |
|||
|
|||
public DtmOutboxTests() |
|||
{ |
|||
UnitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
|||
DistributedEventBus = ServiceProvider.GetRequiredService<IDistributedEventBus>(); |
|||
DtmMsgBarrierManager = ServiceProvider.GetRequiredService<IDtmMsgBarrierManager<IEfCoreDbContext>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Set_DefaultMessage_With_Non_Trans_Uow() |
|||
{ |
|||
using var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(false)); |
|||
|
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
uow.IsCompleted.ShouldBeTrue(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.ShouldBeEmpty(); |
|||
bag.DefaultMessage.ShouldNotBeNull(); |
|||
bag.DefaultMessage.EventInfos.Count.ShouldBe(1); |
|||
bag.DefaultMessage.EventInfos[0].EventName.ShouldBe("test"); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Not_Insert_Barrier_As_Rollback_If_Trans_Succeeded() |
|||
{ |
|||
IDtmMessageInfoModel messageInfoModel; |
|||
|
|||
using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true))) |
|||
{ |
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
uow.IsCompleted.ShouldBeTrue(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.Count.ShouldBe(1); |
|||
messageInfoModel = bag.TransMessages.First().Value; |
|||
} |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var dbContextProvider = ServiceProvider.GetRequiredService<IDbContextProvider<DtmTestDbContext>>(); |
|||
var dbContext = await dbContextProvider.GetDbContextAsync(); |
|||
|
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(false); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Insert_Barrier_As_Rollback_If_Trans_Rolled_Back() |
|||
{ |
|||
IDtmMessageInfoModel messageInfoModel; |
|||
|
|||
using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true))) |
|||
{ |
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
dtmUow.ThrowIfCommit = true; |
|||
|
|||
var exception = await Should.ThrowAsync<AbpException>(() => uow.CompleteAsync()); |
|||
exception.Message.ShouldBe("bye"); |
|||
uow.IsCompleted.ShouldBeFalse(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.Count.ShouldBe(1); |
|||
messageInfoModel = bag.TransMessages.First().Value; |
|||
} |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var dbContextProvider = ServiceProvider.GetRequiredService<IDbContextProvider<DtmTestDbContext>>(); |
|||
var dbContext = await dbContextProvider.GetDbContextAsync(); |
|||
|
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(true); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Support_Multi_Database() |
|||
{ |
|||
IDtmMessageInfoModel messageInfoModel; |
|||
IDtmMessageInfoModel messageInfoModel2; |
|||
|
|||
using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true))) |
|||
{ |
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
await DistributedEventBus.PublishAsync(new Test2Eto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
uow.IsCompleted.ShouldBeTrue(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.Count.ShouldBe(2); |
|||
messageInfoModel = bag.TransMessages |
|||
.First(x => x.Value.DbConnectionLookupInfo.DbContextType == typeof(DtmTestDbContext)).Value; |
|||
messageInfoModel2 = bag.TransMessages |
|||
.First(x => x.Value.DbConnectionLookupInfo.DbContextType == typeof(DtmTestDbContext2)).Value; |
|||
} |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var dbContextProvider = ServiceProvider.GetRequiredService<IDbContextProvider<DtmTestDbContext>>(); |
|||
var dbContext = await dbContextProvider.GetDbContextAsync(); |
|||
|
|||
var dbContextProvider2 = ServiceProvider.GetRequiredService<IDbContextProvider<DtmTestDbContext2>>(); |
|||
var dbContext2 = await dbContextProvider2.GetDbContextAsync(); |
|||
|
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(false); |
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext2, messageInfoModel2.Gid)).ShouldBe(false); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore; |
|||
|
|||
[ConnectionStringName(DtmDbProperties.ConnectionString2Name)] |
|||
public class DtmTestDbContext2 : AbpDbContext<DtmTestDbContext2>, IDtmDbContext |
|||
{ |
|||
/* Add DbSet for each Aggregate Root here. Example: |
|||
* public DbSet<Question> Questions { get; set; } |
|||
*/ |
|||
|
|||
public DtmTestDbContext2(DbContextOptions<DtmTestDbContext2> options) |
|||
: base(options) |
|||
{ |
|||
|
|||
} |
|||
|
|||
protected override void OnModelCreating(ModelBuilder builder) |
|||
{ |
|||
base.OnModelCreating(builder); |
|||
|
|||
builder.ConfigureDtmTest(); |
|||
} |
|||
} |
|||
@ -0,0 +1,154 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.Abp.EventBus.Boxes.Dtm.Models; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB; |
|||
|
|||
[Collection(MongoTestCollection.Name)] |
|||
public class DtmOutboxTests : DtmMongoDbTestBase |
|||
{ |
|||
protected IUnitOfWorkManager UnitOfWorkManager { get; } |
|||
protected IDistributedEventBus DistributedEventBus { get; } |
|||
protected IDtmMsgBarrierManager<IAbpMongoDbContext> DtmMsgBarrierManager { get; } |
|||
|
|||
public DtmOutboxTests() |
|||
{ |
|||
UnitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
|||
DistributedEventBus = ServiceProvider.GetRequiredService<IDistributedEventBus>(); |
|||
DtmMsgBarrierManager = ServiceProvider.GetRequiredService<IDtmMsgBarrierManager<IAbpMongoDbContext>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Set_DefaultMessage_With_Non_Trans_Uow() |
|||
{ |
|||
using var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(false)); |
|||
|
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
uow.IsCompleted.ShouldBeTrue(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.ShouldBeEmpty(); |
|||
bag.DefaultMessage.ShouldNotBeNull(); |
|||
bag.DefaultMessage.EventInfos.Count.ShouldBe(1); |
|||
bag.DefaultMessage.EventInfos[0].EventName.ShouldBe("test"); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Not_Insert_Barrier_As_Rollback_If_Trans_Succeeded() |
|||
{ |
|||
IDtmMessageInfoModel messageInfoModel; |
|||
|
|||
using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true))) |
|||
{ |
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
uow.IsCompleted.ShouldBeTrue(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.Count.ShouldBe(1); |
|||
messageInfoModel = bag.TransMessages.First().Value; |
|||
} |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var dbContextProvider = ServiceProvider.GetRequiredService<IMongoDbContextProvider<DtmTestMongoDbContext>>(); |
|||
var dbContext = await dbContextProvider.GetDbContextAsync(); |
|||
|
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(false); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Insert_Barrier_As_Rollback_If_Trans_Rolled_Back() |
|||
{ |
|||
IDtmMessageInfoModel messageInfoModel; |
|||
|
|||
using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true))) |
|||
{ |
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
dtmUow.ThrowIfCommit = true; |
|||
|
|||
var exception = await Should.ThrowAsync<AbpException>(() => uow.CompleteAsync()); |
|||
exception.Message.ShouldBe("bye"); |
|||
uow.IsCompleted.ShouldBeFalse(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.Count.ShouldBe(1); |
|||
messageInfoModel = bag.TransMessages.First().Value; |
|||
} |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var dbContextProvider = ServiceProvider.GetRequiredService<IMongoDbContextProvider<DtmTestMongoDbContext>>(); |
|||
var dbContext = await dbContextProvider.GetDbContextAsync(); |
|||
|
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(true); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public virtual async Task Should_Support_Multi_Database() |
|||
{ |
|||
IDtmMessageInfoModel messageInfoModel; |
|||
IDtmMessageInfoModel messageInfoModel2; |
|||
|
|||
using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true))) |
|||
{ |
|||
await DistributedEventBus.PublishAsync(new TestEto()); |
|||
await DistributedEventBus.PublishAsync(new Test2Eto()); |
|||
|
|||
var dtmUow = uow as TestDtmUnitOfWork; |
|||
dtmUow.ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
uow.IsCompleted.ShouldBeTrue(); |
|||
|
|||
var bag = dtmUow.GetDtmOutboxEventBag(); |
|||
bag.HasAnyEvent().ShouldBeTrue(); |
|||
|
|||
bag.TransMessages.Count.ShouldBe(2); |
|||
messageInfoModel = bag.TransMessages |
|||
.First(x => x.Value.DbConnectionLookupInfo.DbContextType == typeof(DtmTestMongoDbContext)).Value; |
|||
messageInfoModel2 = bag.TransMessages |
|||
.First(x => x.Value.DbConnectionLookupInfo.DbContextType == typeof(DtmTestMongoDbContext2)).Value; |
|||
} |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var dbContextProvider = ServiceProvider.GetRequiredService<IMongoDbContextProvider<DtmTestMongoDbContext>>(); |
|||
var dbContext = await dbContextProvider.GetDbContextAsync(); |
|||
|
|||
var dbContextProvider2 = ServiceProvider.GetRequiredService<IMongoDbContextProvider<DtmTestMongoDbContext2>>(); |
|||
var dbContext2 = await dbContextProvider2.GetDbContextAsync(); |
|||
|
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(false); |
|||
(await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext2, messageInfoModel2.Gid)).ShouldBe(false); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.MongoDB; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB; |
|||
|
|||
[ConnectionStringName("Dtm2")] |
|||
public class DtmTestMongoDbContext2 : AbpMongoDbContext |
|||
{ |
|||
/* Add mongo collections here. Example: |
|||
* public IMongoCollection<Question> Questions => Collection<Question>(); |
|||
*/ |
|||
|
|||
protected override void CreateModel(IMongoModelBuilder modelBuilder) |
|||
{ |
|||
base.CreateModel(modelBuilder); |
|||
|
|||
modelBuilder.ConfigureDtmTest(); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System.Collections.Generic; |
|||
using EasyAbp.Abp.EventBus.Boxes.Dtm.Models; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm; |
|||
|
|||
public class FakeDtmMessageInfoModel : IDtmMessageInfoModel |
|||
{ |
|||
public bool EventsPublishingActionAdded { get; } |
|||
|
|||
public string Gid { get; set; } |
|||
|
|||
public object DtmMessage { get; set; } |
|||
|
|||
public DbConnectionLookupInfoModel DbConnectionLookupInfo { get; } |
|||
|
|||
public List<OutgoingEventInfo> EventInfos { get; } = new(); |
|||
|
|||
public FakeDtmMessageInfoModel(string gid, object dtmMessage, |
|||
[NotNull] DbConnectionLookupInfoModel dbConnectionLookupInfo) |
|||
{ |
|||
Gid = gid; |
|||
DtmMessage = dtmMessage; |
|||
DbConnectionLookupInfo = dbConnectionLookupInfo; |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using System; |
|||
using Volo.Abp.EventBus; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm; |
|||
|
|||
[EventName("test2")] |
|||
[Serializable] |
|||
public class Test2Eto |
|||
{ |
|||
public string Content { get; set; } |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm; |
|||
|
|||
[ExposeServices(typeof(TestDtmUnitOfWork), typeof(DtmUnitOfWork), typeof(IUnitOfWork))] |
|||
public class TestDtmUnitOfWork : DtmUnitOfWork |
|||
{ |
|||
public bool ThrowIfCommit { get; set; } |
|||
|
|||
public TestDtmUnitOfWork(IServiceProvider serviceProvider, IDtmMessageManager dtmMessageManager, |
|||
IUnitOfWorkEventPublisher unitOfWorkEventPublisher, IOptions<AbpUnitOfWorkDefaultOptions> options) : base( |
|||
serviceProvider, dtmMessageManager, unitOfWorkEventPublisher, options) |
|||
{ |
|||
} |
|||
|
|||
protected override async Task CommitTransactionsAsync() |
|||
{ |
|||
if (!EventBag.HasAnyEvent()) |
|||
{ |
|||
foreach (var transaction in GetAllActiveTransactionApis()) |
|||
{ |
|||
await transaction.CommitAsync(); |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
OnCompleted(async () => await DtmMessageManager.SubmitAsync(EventBag)); |
|||
|
|||
await DtmMessageManager.InsertBarriersAndPrepareAsync(EventBag); |
|||
|
|||
if (ThrowIfCommit) |
|||
{ |
|||
throw new AbpException("bye"); |
|||
} |
|||
|
|||
foreach (var transaction in GetAllActiveTransactionApis()) |
|||
{ |
|||
await transaction.CommitAsync(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using System; |
|||
using Volo.Abp.EventBus; |
|||
|
|||
namespace EasyAbp.Abp.EventBus.Boxes.Dtm; |
|||
|
|||
[EventName("test")] |
|||
[Serializable] |
|||
public class TestEto |
|||
{ |
|||
public string Content { get; set; } |
|||
} |
|||
Loading…
Reference in new issue