diff --git a/src/Dtmcli/ServiceCollectionExtensions.cs b/src/Dtmcli/ServiceCollectionExtensions.cs index 5c33f7e..5c365ee 100644 --- a/src/Dtmcli/ServiceCollectionExtensions.cs +++ b/src/Dtmcli/ServiceCollectionExtensions.cs @@ -33,6 +33,38 @@ public static IServiceCollection AddDtmcli(this IServiceCollection services, ICo return AddDtmcliCore(services, op); } + public static IServiceCollection AddDtmBarrier(this IServiceCollection services, Action setupAction) + { + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + services.AddLogging(); + services.AddOptions(); + services.Configure(setupAction); + + DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services); + + // barrier factory + services.TryAddSingleton(); + + return services; + } + + public static IServiceCollection AddDtmBarrier(this IServiceCollection services, IConfiguration configuration, string sectionName = "dtm") + { + services.AddLogging(); + services.Configure(configuration.GetSection(sectionName)); + + DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services); + + // barrier factory + services.TryAddSingleton(); + + return services; + } + private static IServiceCollection AddDtmcliCore(IServiceCollection services, DtmOptions options) { AddHttpClient(services, options); diff --git a/src/Dtmgrpc/ServiceCollectionExtensions.cs b/src/Dtmgrpc/ServiceCollectionExtensions.cs index 9b6f476..9265af6 100644 --- a/src/Dtmgrpc/ServiceCollectionExtensions.cs +++ b/src/Dtmgrpc/ServiceCollectionExtensions.cs @@ -28,6 +28,36 @@ public static IServiceCollection AddDtmGrpc(this IServiceCollection services, IC return AddDtmGrpcCore(services); } + public static IServiceCollection AddDtmBarrier(this IServiceCollection services, Action setupAction) + { + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + services.AddLogging(); + services.AddOptions(); + services.Configure(setupAction); + + DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services); + + services.TryAddSingleton(); + + return services; + } + + public static IServiceCollection AddDtmBarrier(this IServiceCollection services, IConfiguration configuration, string sectionName = "dtm") + { + services.AddLogging(); + services.Configure(configuration.GetSection(sectionName)); + + DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services); + + services.TryAddSingleton(); + + return services; + } + private static IServiceCollection AddDtmGrpcCore(IServiceCollection services) { // dtm driver diff --git a/tests/Dtmcli.Tests/ServiceCollectionExtensionsTests.cs b/tests/Dtmcli.Tests/ServiceCollectionExtensionsTests.cs index fef09c8..3dd1c36 100644 --- a/tests/Dtmcli.Tests/ServiceCollectionExtensionsTests.cs +++ b/tests/Dtmcli.Tests/ServiceCollectionExtensionsTests.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System.Collections.Generic; +using System.Linq; using Xunit; namespace Dtmcli.Tests @@ -92,5 +93,73 @@ public void AddDtmcli_With_IConfiguration_And_Empty_Option_Should_Succeed() var dtmOptions = dtmOptionsAccs.Value; Assert.NotEqual(dtmUrl, dtmOptions.DtmUrl); } + + + [Fact] + public void AddDtmBarrier_Without_Action_Should_Throw_Exception() + { + var services = new ServiceCollection(); + + Assert.Throws(() => services.AddDtmBarrier(null)); + } + + + [Fact] + public void AddDtmBarrier_With_Action_Should_Succeed() + { + var services = new ServiceCollection(); + services.AddDtmBarrier(x => + { + x.DBType = "mysql"; + x.BarrierTableName = "dtm_barrier.barrier2"; + }); + + var provider = services.BuildServiceProvider(); + + var dtmOptionsAccs = provider.GetService>(); + var dtmOptions = dtmOptionsAccs.Value; + Assert.Equal("mysql", dtmOptions.DBType); + Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName); + + var dtmClient = provider.GetService(); + Assert.Null(dtmClient); + + var barrierFactory = provider.GetRequiredService(); + Assert.NotNull(barrierFactory); + + var specials = provider.GetServices(); + Assert.Equal(3, specials.ToList().Count); + } + + [Fact] + public void AddDtmBarrier_With_IConfiguration_Should_Succeed() + { + var dict = new Dictionary + { + { "dtm:DBType", "mysql" }, + { "dtm:BarrierTableName", "dtm_barrier.barrier2" }, + }; + + var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); + + var services = new ServiceCollection(); + services.AddDtmBarrier(config, "dtm"); + + var provider = services.BuildServiceProvider(); + + var dtmOptionsAccs = provider.GetService>(); + var dtmOptions = dtmOptionsAccs.Value; + Assert.Equal("mysql", dtmOptions.DBType); + Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName); + + var dtmClient = provider.GetService(); + Assert.Null(dtmClient); + + var barrierFactory = provider.GetRequiredService(); + Assert.NotNull(barrierFactory); + + var specials = provider.GetServices(); + Assert.Equal(3, specials.ToList().Count); + } } } \ No newline at end of file diff --git a/tests/Dtmgrpc.Tests/ServiceCollectionExtensionsTests.cs b/tests/Dtmgrpc.Tests/ServiceCollectionExtensionsTests.cs index bf7bc40..2bf4404 100644 --- a/tests/Dtmgrpc.Tests/ServiceCollectionExtensionsTests.cs +++ b/tests/Dtmgrpc.Tests/ServiceCollectionExtensionsTests.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System.Collections.Generic; +using System.Linq; using Xunit; namespace Dtmgrpc.Tests @@ -91,5 +92,71 @@ public void AddDtmGrpc_With_IConfiguration_And_Empty_Option_Should_Succeed() var dtmOptions = dtmOptionsAccs.Value; Assert.NotEqual(dtmUrl, dtmOptions.DtmUrl); } + + [Fact] + public void AddDtmBarrier_Without_Action_Should_Throw_Exception() + { + var services = new ServiceCollection(); + + Assert.Throws(() => services.AddDtmBarrier(null)); + } + + [Fact] + public void AddDtmBarrier_With_Action_Should_Succeed() + { + var services = new ServiceCollection(); + services.AddDtmBarrier(x => + { + x.DBType = "mysql"; + x.BarrierTableName = "dtm_barrier.barrier2"; + }); + + var provider = services.BuildServiceProvider(); + + var dtmOptionsAccs = provider.GetService>(); + var dtmOptions = dtmOptionsAccs.Value; + Assert.Equal("mysql", dtmOptions.DBType); + Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName); + + var dtmClient = provider.GetService(); + Assert.Null(dtmClient); + + var barrierFactory = provider.GetRequiredService(); + Assert.NotNull(barrierFactory); + + var specials = provider.GetServices(); + Assert.Equal(3, specials.ToList().Count); + } + + [Fact] + public void AddDtmBarrier_With_IConfiguration_Should_Succeed() + { + var dict = new Dictionary + { + { "dtm:DBType", "mysql" }, + { "dtm:BarrierTableName", "dtm_barrier.barrier2" }, + }; + + var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); + + var services = new ServiceCollection(); + services.AddDtmBarrier(config, "dtm"); + + var provider = services.BuildServiceProvider(); + + var dtmOptionsAccs = provider.GetService>(); + var dtmOptions = dtmOptionsAccs.Value; + Assert.Equal("mysql", dtmOptions.DBType); + Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName); + + var dtmClient = provider.GetService(); + Assert.Null(dtmClient); + + var barrierFactory = provider.GetRequiredService(); + Assert.NotNull(barrierFactory); + + var specials = provider.GetServices(); + Assert.Equal(3, specials.ToList().Count); + } } }