From b0a9ec76ed6d2fee0a2315b8fe449630aab61dcb Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Wed, 1 Jun 2022 22:36:58 +0800 Subject: [PATCH 1/5] UTs for outbox --- ...usBoxesDtmEntityFrameworkCoreTestModule.cs | 48 +++++- .../EntityFrameworkCore/DtmOutboxTests.cs | 153 +++++++++++++++++ .../EntityFrameworkCore/DtmTestDbContext2.cs | 26 +++ .../AbpEventBusBoxesDtmMongoDbTestModule.cs | 23 ++- .../MongoDB/DtmOutboxTests.cs | 154 ++++++++++++++++++ .../MongoDB/DtmTestMongoDbContext.cs | 2 +- .../MongoDB/DtmTestMongoDbContext2.cs | 19 +++ .../AbpEventBusBoxesDtmTestBaseModule.cs | 6 + .../DtmDbProperties.cs | 1 + .../FakeDistributedEventBus.cs | 6 +- .../FakeDtmMessageInfoModel.cs | 27 +++ .../FakeDtmMessageManager.cs | 50 +++++- .../Test2Eto.cs | 11 ++ .../TestDtmUnitOfWork.cs | 47 ++++++ .../TestEto.cs | 11 ++ 15 files changed, 568 insertions(+), 16 deletions(-) create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmTestDbContext2.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext2.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageInfoModel.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/Test2Eto.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestDtmUnitOfWork.cs create mode 100644 test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestEto.cs diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/AbpEventBusBoxesDtmEntityFrameworkCoreTestModule.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/AbpEventBusBoxesDtmEntityFrameworkCoreTestModule.cs index fa853ab..770642e 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/AbpEventBusBoxesDtmEntityFrameworkCoreTestModule.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/AbpEventBusBoxesDtmEntityFrameworkCoreTestModule.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Sqlite; using Volo.Abp.EventBus.Distributed; @@ -23,22 +24,45 @@ public class AbpEventBusBoxesDtmEntityFrameworkCoreTestModule : AbpModule Configure(options => { - options.Configure(abpDbContextConfigurationContext => + options.Configure(abpDbContextConfigurationContext => { abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection); }); }); + + var sqliteConnection2 = CreateDatabaseAndGetConnection2(); + + Configure(options => + { + options.Configure(abpDbContextConfigurationContext => + { + abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection2); + }); + }); context.Services.AddAbpDbContext(options => { options.AddDefaultRepositories(); }); + context.Services.AddAbpDbContext(options => + { + options.AddDefaultRepositories(); + }); + Configure(options => { - options.Outboxes.Configure(config => + options.Outboxes.Configure("FirstBox", config => { config.UseDbContextWithDtmOutbox(); + config.Selector = type => type == typeof(TestEto); + }); + + options.Outboxes.Configure("SecondBox", config => + { + config.UseDbContextWithDtmOutbox(); + config.Selector = null; + // config.Selector = type => type == typeof(Test2Eto); }); // options.Inboxes.Configure(config => @@ -46,11 +70,17 @@ public class AbpEventBusBoxesDtmEntityFrameworkCoreTestModule : AbpModule // config.UseDbContextWithDtmInbox(); // }); }); + + Configure(options => + { + options.ConnectionStrings["Dtm"] = "Data Source=Context;Mode=Memory;Cache=Shared"; + options.ConnectionStrings["Dtm2"] = "Data Source=Context2;Mode=Memory;Cache=Shared"; + }); } private static SqliteConnection CreateDatabaseAndGetConnection() { - var connection = new SqliteConnection("Data Source=:memory:"); + var connection = new SqliteConnection("Data Source=Context;Mode=Memory;Cache=Shared"); connection.Open(); new DtmTestDbContext( @@ -59,4 +89,16 @@ public class AbpEventBusBoxesDtmEntityFrameworkCoreTestModule : AbpModule return connection; } + + private static SqliteConnection CreateDatabaseAndGetConnection2() + { + var connection = new SqliteConnection("Data Source=Context2;Mode=Memory;Cache=Shared"); + connection.Open(); + + new DtmTestDbContext2( + new DbContextOptionsBuilder().UseSqlite(connection).Options + ).GetService().CreateTables(); + + return connection; + } } diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs new file mode 100644 index 0000000..d5db7b7 --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs @@ -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 DtmMsgBarrierManager { get; } + + public DtmOutboxTests() + { + UnitOfWorkManager = ServiceProvider.GetRequiredService(); + DistributedEventBus = ServiceProvider.GetRequiredService(); + DtmMsgBarrierManager = ServiceProvider.GetRequiredService>(); + } + + [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_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>(); + 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(() => 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>(); + 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>(); + var dbContext = await dbContextProvider.GetDbContextAsync(); + + var dbContextProvider2 = ServiceProvider.GetRequiredService>(); + var dbContext2 = await dbContextProvider2.GetDbContextAsync(); + + (await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(false); + (await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext2, messageInfoModel2.Gid)).ShouldBe(false); + }); + } +} \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmTestDbContext2.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmTestDbContext2.cs new file mode 100644 index 0000000..8610aef --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmTestDbContext2.cs @@ -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, IDtmDbContext +{ + /* Add DbSet for each Aggregate Root here. Example: + * public DbSet Questions { get; set; } + */ + + public DtmTestDbContext2(DbContextOptions options) + : base(options) + { + + } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + + builder.ConfigureDtmTest(); + } +} diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/AbpEventBusBoxesDtmMongoDbTestModule.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/AbpEventBusBoxesDtmMongoDbTestModule.cs index d13e117..fea7631 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/AbpEventBusBoxesDtmMongoDbTestModule.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/AbpEventBusBoxesDtmMongoDbTestModule.cs @@ -16,12 +16,16 @@ public class AbpEventBusBoxesDtmMongoDbTestModule : AbpModule { var stringArray = MongoDbFixture.ConnectionString.Split('?'); var connectionString = stringArray[0].EnsureEndsWith('/') + - "Db_" + + "Db_" + + Guid.NewGuid().ToString("N") + "/?" + stringArray[1]; + var connectionString2 = stringArray[0].EnsureEndsWith('/') + + "Db_" + Guid.NewGuid().ToString("N") + "/?" + stringArray[1]; Configure(options => { - options.ConnectionStrings.Default = connectionString; + options.ConnectionStrings["Dtm"] = connectionString; + options.ConnectionStrings["Dtm2"] = connectionString2; }); context.Services.AddMongoDbContext(options => @@ -29,11 +33,24 @@ public class AbpEventBusBoxesDtmMongoDbTestModule : AbpModule options.AddDefaultRepositories(); }); + context.Services.AddMongoDbContext(options => + { + options.AddDefaultRepositories(); + }); + Configure(options => { - options.Outboxes.Configure(config => + options.Outboxes.Configure("FirstBox", config => { config.UseMongoDbContextWithDtmOutbox(); + config.Selector = type => type == typeof(TestEto); + }); + + options.Outboxes.Configure("SecondBox", config => + { + config.UseMongoDbContextWithDtmOutbox(); + config.Selector = null; + // config.Selector = type => type == typeof(Test2Eto); }); // options.Inboxes.Configure(config => diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs new file mode 100644 index 0000000..4a6c2de --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs @@ -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 DtmMsgBarrierManager { get; } + + public DtmOutboxTests() + { + UnitOfWorkManager = ServiceProvider.GetRequiredService(); + DistributedEventBus = ServiceProvider.GetRequiredService(); + DtmMsgBarrierManager = ServiceProvider.GetRequiredService>(); + } + + [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_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>(); + 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(() => 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>(); + 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>(); + var dbContext = await dbContextProvider.GetDbContextAsync(); + + var dbContextProvider2 = ServiceProvider.GetRequiredService>(); + var dbContext2 = await dbContextProvider2.GetDbContextAsync(); + + (await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext, messageInfoModel.Gid)).ShouldBe(false); + (await DtmMsgBarrierManager.TryInsertBarrierAsRollbackAsync(dbContext2, messageInfoModel2.Gid)).ShouldBe(false); + }); + } +} \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext.cs index 618662b..96efe0e 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext.cs @@ -3,7 +3,7 @@ using Volo.Abp.MongoDB; namespace EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB; -[ConnectionStringName("Default")] +[ConnectionStringName("Dtm")] public class DtmTestMongoDbContext : AbpMongoDbContext { /* Add mongo collections here. Example: diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext2.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext2.cs new file mode 100644 index 0000000..cd1e386 --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmTestMongoDbContext2.cs @@ -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 Questions => Collection(); + */ + + protected override void CreateModel(IMongoModelBuilder modelBuilder) + { + base.CreateModel(modelBuilder); + + modelBuilder.ConfigureDtmTest(); + } +} diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/AbpEventBusBoxesDtmTestBaseModule.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/AbpEventBusBoxesDtmTestBaseModule.cs index adc53a4..441e787 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/AbpEventBusBoxesDtmTestBaseModule.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/AbpEventBusBoxesDtmTestBaseModule.cs @@ -1,10 +1,12 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp; using Volo.Abp.Authorization; using Volo.Abp.Autofac; using Volo.Abp.Data; using Volo.Abp.Modularity; using Volo.Abp.Threading; +using Volo.Abp.Uow; namespace EasyAbp.Abp.EventBus.Boxes.Dtm; @@ -21,6 +23,10 @@ public class AbpEventBusBoxesDtmTestBaseModule : AbpModule context.Services.AddAlwaysAllowAuthorization(); context.Services.AddDtmBoxes(); + + context.Services.TryAddTransient(); + context.Services.Replace(ServiceDescriptor.Transient()); + context.Services.Replace(ServiceDescriptor.Transient()); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/DtmDbProperties.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/DtmDbProperties.cs index d80dc8c..88d3840 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/DtmDbProperties.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/DtmDbProperties.cs @@ -7,4 +7,5 @@ public static class DtmDbProperties public static string DbSchema { get; set; } = null; public const string ConnectionStringName = "Dtm"; + public const string ConnectionString2Name = "Dtm2"; } diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDistributedEventBus.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDistributedEventBus.cs index 1903368..0378250 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDistributedEventBus.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDistributedEventBus.cs @@ -65,12 +65,12 @@ public class FakeDistributedEventBus : DistributedEventBusBase, ITransientDepend public override async Task PublishFromOutboxAsync(OutgoingEventInfo outgoingEvent, OutboxConfig outboxConfig) { - throw new NotSupportedException(); + await Task.CompletedTask; } public override async Task PublishManyFromOutboxAsync(IEnumerable outgoingEvents, OutboxConfig outboxConfig) { - throw new NotSupportedException(); + await Task.CompletedTask; } public override async Task ProcessFromInboxAsync(IncomingEventInfo incomingEvent, InboxConfig inboxConfig) @@ -80,6 +80,6 @@ public class FakeDistributedEventBus : DistributedEventBusBase, ITransientDepend protected override byte[] Serialize(object eventData) { - throw new NotSupportedException(); + return null; } } \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageInfoModel.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageInfoModel.cs new file mode 100644 index 0000000..6ad5cb2 --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageInfoModel.cs @@ -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 EventInfos { get; } = new(); + + public FakeDtmMessageInfoModel(string gid, object dtmMessage, + [NotNull] DbConnectionLookupInfoModel dbConnectionLookupInfo) + { + Gid = gid; + DtmMessage = dtmMessage; + DbConnectionLookupInfo = dbConnectionLookupInfo; + } +} \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs index 2514a75..eab1faf 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs @@ -1,45 +1,57 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using EasyAbp.Abp.EventBus.Boxes.Dtm.Models; +using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; using Volo.Abp; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; namespace EasyAbp.Abp.EventBus.Boxes.Dtm; public class FakeDtmMessageManager : IDtmMessageManager, ITransientDependency { + protected ICurrentTenant CurrentTenant { get; } protected IServiceProvider ServiceProvider { get; } + protected IDtmMsgGidProvider GidProvider { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; } + protected IConnectionStringHasher ConnectionStringHasher { get; } protected IConnectionStringResolver ConnectionStringResolver { get; } public FakeDtmMessageManager( + ICurrentTenant currentTenant, IServiceProvider serviceProvider, + IDtmMsgGidProvider gidProvider, IUnitOfWorkManager unitOfWorkManager, + IConnectionStringHasher connectionStringHasher, IConnectionStringResolver connectionStringResolver) { + CurrentTenant = currentTenant; ServiceProvider = serviceProvider; + GidProvider = gidProvider; UnitOfWorkManager = unitOfWorkManager; + ConnectionStringHasher = connectionStringHasher; ConnectionStringResolver = connectionStringResolver; } public virtual async Task AddEventAsync(DtmOutboxEventBag eventBag, object dbContext, string connectionString, object transObj, OutgoingEventInfo eventInfo) { - await Task.CompletedTask; + var dbContextType = dbContext.GetType(); + var hashedConnectionString = await ConnectionStringHasher.HashAsync(connectionString); + + var model = GetOrCreateDtmMessageInfoModel(eventBag, transObj, dbContextType, hashedConnectionString); + + model.EventInfos.Add(eventInfo); } public async Task InsertBarriersAndPrepareAsync(DtmOutboxEventBag eventBag, CancellationToken cancellationToken = default) - { - await Task.CompletedTask; - } - - public virtual async Task SubmitAsync(DtmOutboxEventBag eventBag, CancellationToken cancellationToken = default) { // await AddEventsPublishingActionAsync(eventBag); @@ -52,6 +64,11 @@ public class FakeDtmMessageManager : IDtmMessageManager, ITransientDependency await InsertTransMessagesBarriersAsync(eventBag); } + + public virtual async Task SubmitAsync(DtmOutboxEventBag eventBag, CancellationToken cancellationToken = default) + { + await Task.CompletedTask; + } protected virtual async Task InsertTransMessagesBarriersAsync(DtmOutboxEventBag eventBag) { @@ -92,4 +109,25 @@ public class FakeDtmMessageManager : IDtmMessageManager, ITransientDependency return databaseApi; } + + protected virtual IDtmMessageInfoModel GetOrCreateDtmMessageInfoModel(DtmOutboxEventBag eventBag, + [CanBeNull] object transObj, Type dbContextType, string hashedConnectionString) + { + if (transObj is null) + { + return eventBag.DefaultMessage ?? (eventBag.DefaultMessage = + CreateDtmMessageInfoModel(dbContextType, hashedConnectionString)); + } + + return eventBag.TransMessages.GetOrAdd(transObj, + _ => CreateDtmMessageInfoModel(dbContextType, hashedConnectionString)); + } + + protected virtual IDtmMessageInfoModel CreateDtmMessageInfoModel(Type dbContextType, string hashedConnectionString) + { + var gid = GidProvider.Create(); + + return new FakeDtmMessageInfoModel(gid, null, + new DbConnectionLookupInfoModel(dbContextType, CurrentTenant.Id, hashedConnectionString)); + } } \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/Test2Eto.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/Test2Eto.cs new file mode 100644 index 0000000..c894997 --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/Test2Eto.cs @@ -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; } +} \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestDtmUnitOfWork.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestDtmUnitOfWork.cs new file mode 100644 index 0000000..791deae --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestDtmUnitOfWork.cs @@ -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 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(); + } + } +} \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestEto.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestEto.cs new file mode 100644 index 0000000..7d9915b --- /dev/null +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/TestEto.cs @@ -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; } +} \ No newline at end of file From eba6710314fba2b68aedbbc3a44e64a4b21cf832 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Thu, 2 Jun 2022 03:31:11 +0800 Subject: [PATCH 2/5] Fix test names --- .../EntityFrameworkCore/DtmOutboxTests.cs | 2 +- .../MongoDB/DtmOutboxTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs index d5db7b7..659c17d 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore.Tests/EntityFrameworkCore/DtmOutboxTests.cs @@ -47,7 +47,7 @@ public class DtmOutboxTests : DtmEntityFrameworkCoreTestBase } [Fact] - public virtual async Task Should_Not_Barrier_As_Rollback_If_Trans_Succeeded() + public virtual async Task Should_Not_Insert_Barrier_As_Rollback_If_Trans_Succeeded() { IDtmMessageInfoModel messageInfoModel; diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs index 4a6c2de..a5dc565 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB.Tests/MongoDB/DtmOutboxTests.cs @@ -48,7 +48,7 @@ public class DtmOutboxTests : DtmMongoDbTestBase } [Fact] - public virtual async Task Should_Not_Barrier_As_Rollback_If_Trans_Succeeded() + public virtual async Task Should_Not_Insert_Barrier_As_Rollback_If_Trans_Succeeded() { IDtmMessageInfoModel messageInfoModel; From e3f177bf9bb2d80153a9d456e9056af77a5f1217 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Thu, 2 Jun 2022 14:20:36 +0800 Subject: [PATCH 3/5] Improve DtmMongoDbBarrierCollectionInitializer --- .../Dtm/Barriers/DtmMongoDbBarrierCollectionInitializer.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/DtmMongoDbBarrierCollectionInitializer.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/DtmMongoDbBarrierCollectionInitializer.cs index f6cef0b..f49b3dc 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/DtmMongoDbBarrierCollectionInitializer.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/DtmMongoDbBarrierCollectionInitializer.cs @@ -46,11 +46,7 @@ public class DtmMongoDbBarrierCollectionInitializer : IDtmMongoDbBarrierCollecti foreach (var server in servers) { - UnitOfWorkManager.Current.OnCompleted(() => - { - CreatedServers[server] = true; - return Task.CompletedTask; - }); + CreatedServers[server] = true; } } } \ No newline at end of file From 721bd3df5c1e0fd0060e42595428210a0c94cea8 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Thu, 2 Jun 2022 14:27:31 +0800 Subject: [PATCH 4/5] Use the MongoDB session and cancellationTokens --- .../Barriers/AbpEfCoreDtmMsgBarrierManager.cs | 10 +++-- .../Boxes/Dtm/GrpcDtmMessageManager.cs | 7 +-- .../AbpMongoDbDtmMsgBarrierManager.cs | 43 ++++++++++++++----- .../Boxes/Dtm/DtmMsgBarrierManagerBase.cs | 10 +++-- .../Boxes/Dtm/IDtmMsgBarrierManager.cs | 12 ++++-- .../FakeDtmMessageManager.cs | 7 +-- 6 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpEfCoreDtmMsgBarrierManager.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpEfCoreDtmMsgBarrierManager.cs index c847449..4a99f11 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpEfCoreDtmMsgBarrierManager.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.EntityFrameworkCore/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpEfCoreDtmMsgBarrierManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Dapper; using DtmCommon; @@ -33,7 +34,8 @@ public class AbpEfCoreDtmMsgBarrierManager : DtmMsgBarrierManagerBase TryInsertBarrierAsRollbackAsync(IEfCoreDbContext dbContext, string gid) + public override async Task TryInsertBarrierAsRollbackAsync(IEfCoreDbContext dbContext, string gid, + CancellationToken cancellationToken = default) { try { @@ -116,7 +119,8 @@ public class AbpEfCoreDtmMsgBarrierManager : DtmMsgBarrierManagerBase TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, string gid) + public override async Task TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, string gid, + CancellationToken cancellationToken) { if (!IsValidDatabaseApi(databaseApi)) { diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs index 966bef3..3c18086 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs @@ -83,7 +83,7 @@ public class GrpcDtmMessageManager : IDtmMessageManager, ITransientDependency await PrepareTransMessagesAsync(eventBag); - await InsertTransMessagesBarriersAsync(eventBag); + await InsertTransMessagesBarriersAsync(eventBag, cancellationToken); } public virtual async Task SubmitAsync(DtmOutboxEventBag eventBag, CancellationToken cancellationToken = default) @@ -155,7 +155,8 @@ public class GrpcDtmMessageManager : IDtmMessageManager, ITransientDependency } } - protected virtual async Task InsertTransMessagesBarriersAsync(DtmOutboxEventBag eventBag) + protected virtual async Task InsertTransMessagesBarriersAsync(DtmOutboxEventBag eventBag, + CancellationToken cancellationToken = default) { foreach (var model in eventBag.TransMessages.Values) { @@ -167,7 +168,7 @@ public class GrpcDtmMessageManager : IDtmMessageManager, ITransientDependency foreach (var barrierManager in barrierManagers) { - if (await barrierManager.TryInvokeEnsureInsertBarrierAsync(databaseApi, model.Gid)) + if (await barrierManager.TryInvokeEnsureInsertBarrierAsync(databaseApi, model.Gid, cancellationToken)) { inserted = true; break; diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpMongoDbDtmMsgBarrierManager.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpMongoDbDtmMsgBarrierManager.cs index e16c1aa..ac1551e 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpMongoDbDtmMsgBarrierManager.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.MongoDB/EasyAbp/Abp/EventBus/Boxes/Dtm/Barriers/AbpMongoDbDtmMsgBarrierManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using DtmCommon; using EasyAbp.Abp.EventBus.Boxes.Dtm.Options; @@ -31,7 +32,8 @@ public class AbpMongoDbDtmMsgBarrierManager : DtmMsgBarrierManagerBase TryInsertBarrierAsRollbackAsync(IAbpMongoDbContext dbContext, string gid) + public override async Task TryInsertBarrierAsRollbackAsync(IAbpMongoDbContext dbContext, string gid, + CancellationToken cancellationToken = default) { try { - await InsertBarrierAsync(dbContext, gid, Constant.Barrier.MSG_BARRIER_REASON); + await InsertBarrierAsync(dbContext, gid, Constant.Barrier.MSG_BARRIER_REASON, cancellationToken); } catch (Exception e) { @@ -76,10 +79,13 @@ public class AbpMongoDbDtmMsgBarrierManager : DtmMsgBarrierManagerBase(filter, cancellationToken: cancellationToken) + : await mongoCollection.FindAsync(dbContext.SessionHandle, filter, + cancellationToken: cancellationToken); - var cursor = await mongoCollection.FindAsync(filter); - - var res = await cursor.ToListAsync(); + var res = await cursor.ToListAsync(cancellationToken: cancellationToken); if (res is { Count: > 0 } && res[0].Reason.Equals(Constant.Barrier.MSG_BARRIER_REASON)) { @@ -107,7 +113,7 @@ public class AbpMongoDbDtmMsgBarrierManager : DtmMsgBarrierManagerBase TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, string gid) + public override async Task TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, string gid, + CancellationToken cancellationToken) { if (!IsValidDatabaseApi(databaseApi)) { diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/DtmMsgBarrierManagerBase.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/DtmMsgBarrierManagerBase.cs index 4b230be..3622636 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/DtmMsgBarrierManagerBase.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/DtmMsgBarrierManagerBase.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Threading.Tasks; using Volo.Abp.Uow; @@ -7,14 +8,17 @@ namespace EasyAbp.Abp.EventBus.Boxes.Dtm; public abstract class DtmMsgBarrierManagerBase : IDtmMsgBarrierManager, IDtmMsgBarrierManager where TDbContextInterface : class { - public abstract Task TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, string gid); + public abstract Task TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, string gid, + CancellationToken cancellationToken = default); protected virtual bool IsValidDatabaseApi(IDatabaseApi databaseApi) where TDatabaseApi : IDatabaseApi { return databaseApi.GetType().IsAssignableTo(typeof(TDatabaseApi)); } - public abstract Task EnsureInsertBarrierAsync(TDbContextInterface dbContext, string gid); + public abstract Task EnsureInsertBarrierAsync(TDbContextInterface dbContext, string gid, + CancellationToken cancellationToken = default); - public abstract Task TryInsertBarrierAsRollbackAsync(TDbContextInterface dbContext, string gid); + public abstract Task TryInsertBarrierAsRollbackAsync(TDbContextInterface dbContext, string gid, + CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/IDtmMsgBarrierManager.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/IDtmMsgBarrierManager.cs index de09897..58b9233 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/IDtmMsgBarrierManager.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm/EasyAbp/Abp/EventBus/Boxes/Dtm/IDtmMsgBarrierManager.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; using JetBrains.Annotations; using Volo.Abp.Uow; @@ -6,9 +7,11 @@ namespace EasyAbp.Abp.EventBus.Boxes.Dtm; public interface IDtmMsgBarrierManager : IDtmMsgBarrierManager where TDbContextInterface : class { - Task EnsureInsertBarrierAsync(TDbContextInterface dbContext, [NotNull] string gid); + Task EnsureInsertBarrierAsync(TDbContextInterface dbContext, [NotNull] string gid, + CancellationToken cancellationToken = default); - Task TryInsertBarrierAsRollbackAsync(TDbContextInterface dbContext, [NotNull] string gid); + Task TryInsertBarrierAsRollbackAsync(TDbContextInterface dbContext, [NotNull] string gid, + CancellationToken cancellationToken = default); } public interface IDtmMsgBarrierManager @@ -16,5 +19,6 @@ public interface IDtmMsgBarrierManager /// /// Invokes InsertBarrierAsync method if the can be identified. /// - Task TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, [NotNull] string gid); + Task TryInvokeEnsureInsertBarrierAsync(IDatabaseApi databaseApi, [NotNull] string gid, + CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs index eab1faf..8949421 100644 --- a/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs +++ b/test/EasyAbp.Abp.EventBus.Boxes.Dtm.TestBase/FakeDtmMessageManager.cs @@ -62,7 +62,7 @@ public class FakeDtmMessageManager : IDtmMessageManager, ITransientDependency // await PrepareTransMessagesAsync(eventBag, cancellationToken); - await InsertTransMessagesBarriersAsync(eventBag); + await InsertTransMessagesBarriersAsync(eventBag, cancellationToken); } public virtual async Task SubmitAsync(DtmOutboxEventBag eventBag, CancellationToken cancellationToken = default) @@ -70,7 +70,8 @@ public class FakeDtmMessageManager : IDtmMessageManager, ITransientDependency await Task.CompletedTask; } - protected virtual async Task InsertTransMessagesBarriersAsync(DtmOutboxEventBag eventBag) + protected virtual async Task InsertTransMessagesBarriersAsync(DtmOutboxEventBag eventBag, + CancellationToken cancellationToken = default) { foreach (var model in eventBag.TransMessages.Values) { @@ -82,7 +83,7 @@ public class FakeDtmMessageManager : IDtmMessageManager, ITransientDependency foreach (var barrierManager in barrierManagers) { - if (await barrierManager.TryInvokeEnsureInsertBarrierAsync(databaseApi, model.Gid)) + if (await barrierManager.TryInvokeEnsureInsertBarrierAsync(databaseApi, model.Gid, cancellationToken)) { inserted = true; break; From 35866a0524e7581ac1102bf741c6d4d1fd304794 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Thu, 2 Jun 2022 19:50:05 +0800 Subject: [PATCH 5/5] Add DTM headers for the non-transactional UOW --- .../EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs | 11 ----------- .../Boxes/Dtm/Models/GrpcDtmMessageInfoModel.cs | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs index 865a79e..abfda14 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/GrpcDtmMessageManager.cs @@ -141,17 +141,6 @@ public class GrpcDtmMessageManager : IDtmMessageManager, ITransientDependency foreach (var model in eventBag.TransMessages.Values) { var message = (model.DtmMessage as MsgGrpc)!; - - var dbContextType = $"{model.DbConnectionLookupInfo.DbContextType.FullName}, {model.DbConnectionLookupInfo.DbContextType.Assembly.GetName().Name}"; - - message.SetBranchHeaders(new Dictionary - { - {DtmRequestHeaderNames.ActionApiToken, AbpDtmGrpcOptions.ActionApiToken}, - {DtmRequestHeaderNames.DbContextType, dbContextType}, - {DtmRequestHeaderNames.TenantId, model.DbConnectionLookupInfo.TenantId.ToString()}, - {DtmRequestHeaderNames.HashedConnectionString, model.DbConnectionLookupInfo.HashedConnectionString}, - }); - await message.Prepare(AbpDtmGrpcOptions.GetQueryPreparedAddress(), cancellationToken); } } diff --git a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/Models/GrpcDtmMessageInfoModel.cs b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/Models/GrpcDtmMessageInfoModel.cs index 57cbec0..ea1534b 100644 --- a/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/Models/GrpcDtmMessageInfoModel.cs +++ b/src/EasyAbp.Abp.EventBus.Boxes.Dtm.Grpc/EasyAbp/Abp/EventBus/Boxes/Dtm/Models/GrpcDtmMessageInfoModel.cs @@ -35,11 +35,24 @@ public class GrpcDtmMessageInfoModel : IDtmMessageInfoModel throw new AbpException("Duplicate events publishing action."); } - ((MsgGrpc)DtmMessage).Add(abpDtmEventBoxesOptions.GetPublishEventsAddress(), new DtmMsgPublishEventsRequest + var message = (DtmMessage as MsgGrpc)!; + + message.Add(abpDtmEventBoxesOptions.GetPublishEventsAddress(), new DtmMsgPublishEventsRequest { OutgoingEventInfoListToByteString = serializer.Serialize(EventInfos) }); + var dbContextType = + $"{DbConnectionLookupInfo.DbContextType.FullName}, {DbConnectionLookupInfo.DbContextType.Assembly.GetName().Name}"; + + message.SetBranchHeaders(new Dictionary + { + {DtmRequestHeaderNames.ActionApiToken, abpDtmEventBoxesOptions.ActionApiToken}, + {DtmRequestHeaderNames.DbContextType, dbContextType}, + {DtmRequestHeaderNames.TenantId, DbConnectionLookupInfo.TenantId.ToString()}, + {DtmRequestHeaderNames.HashedConnectionString, DbConnectionLookupInfo.HashedConnectionString}, + }); + EventsPublishingActionAdded = true; } } \ No newline at end of file