Skip to content

Commit

Permalink
add AddDtmBarrier for minimum injection (#48) (#55)
Browse files Browse the repository at this point in the history
* add AddDtmBarrier for minimum injection (#48)

* test: add more tests
  • Loading branch information
catcherwong authored Aug 29, 2022
1 parent e1e4f86 commit 883c327
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Dtmcli/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ public static IServiceCollection AddDtmcli(this IServiceCollection services, ICo
return AddDtmcliCore(services, op);
}

public static IServiceCollection AddDtmBarrier(this IServiceCollection services, Action<DtmOptions> setupAction)
{
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}

services.AddLogging();
services.AddOptions();
services.Configure(setupAction);

DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services);

// barrier factory
services.TryAddSingleton<IBranchBarrierFactory, DefaultBranchBarrierFactory>();

return services;
}

public static IServiceCollection AddDtmBarrier(this IServiceCollection services, IConfiguration configuration, string sectionName = "dtm")
{
services.AddLogging();
services.Configure<DtmOptions>(configuration.GetSection(sectionName));

DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services);

// barrier factory
services.TryAddSingleton<IBranchBarrierFactory, DefaultBranchBarrierFactory>();

return services;
}

private static IServiceCollection AddDtmcliCore(IServiceCollection services, DtmOptions options)
{
AddHttpClient(services, options);
Expand Down
30 changes: 30 additions & 0 deletions src/Dtmgrpc/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ public static IServiceCollection AddDtmGrpc(this IServiceCollection services, IC
return AddDtmGrpcCore(services);
}

public static IServiceCollection AddDtmBarrier(this IServiceCollection services, Action<DtmOptions> setupAction)
{
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}

services.AddLogging();
services.AddOptions();
services.Configure(setupAction);

DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services);

services.TryAddSingleton<IBranchBarrierFactory, DefaultBranchBarrierFactory>();

return services;
}

public static IServiceCollection AddDtmBarrier(this IServiceCollection services, IConfiguration configuration, string sectionName = "dtm")
{
services.AddLogging();
services.Configure<DtmOptions>(configuration.GetSection(sectionName));

DtmCommon.ServiceCollectionExtensions.AddDtmCommon(services);

services.TryAddSingleton<IBranchBarrierFactory, DefaultBranchBarrierFactory>();

return services;
}

private static IServiceCollection AddDtmGrpcCore(IServiceCollection services)
{
// dtm driver
Expand Down
69 changes: 69 additions & 0 deletions tests/Dtmcli.Tests/ServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Dtmcli.Tests
Expand Down Expand Up @@ -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<System.ArgumentNullException>(() => 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<IOptions<DtmOptions>>();
var dtmOptions = dtmOptionsAccs.Value;
Assert.Equal("mysql", dtmOptions.DBType);
Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName);

var dtmClient = provider.GetService<IDtmClient>();
Assert.Null(dtmClient);

var barrierFactory = provider.GetRequiredService<IBranchBarrierFactory>();
Assert.NotNull(barrierFactory);

var specials = provider.GetServices<IDbSpecial>();
Assert.Equal(3, specials.ToList().Count);
}

[Fact]
public void AddDtmBarrier_With_IConfiguration_Should_Succeed()
{
var dict = new Dictionary<string, string>
{
{ "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<IOptions<DtmOptions>>();
var dtmOptions = dtmOptionsAccs.Value;
Assert.Equal("mysql", dtmOptions.DBType);
Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName);

var dtmClient = provider.GetService<IDtmClient>();
Assert.Null(dtmClient);

var barrierFactory = provider.GetRequiredService<IBranchBarrierFactory>();
Assert.NotNull(barrierFactory);

var specials = provider.GetServices<IDbSpecial>();
Assert.Equal(3, specials.ToList().Count);
}
}
}
67 changes: 67 additions & 0 deletions tests/Dtmgrpc.Tests/ServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Dtmgrpc.Tests
Expand Down Expand Up @@ -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<System.ArgumentNullException>(() => 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<IOptions<DtmOptions>>();
var dtmOptions = dtmOptionsAccs.Value;
Assert.Equal("mysql", dtmOptions.DBType);
Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName);

var dtmClient = provider.GetService<IDtmgRPCClient>();
Assert.Null(dtmClient);

var barrierFactory = provider.GetRequiredService<IBranchBarrierFactory>();
Assert.NotNull(barrierFactory);

var specials = provider.GetServices<IDbSpecial>();
Assert.Equal(3, specials.ToList().Count);
}

[Fact]
public void AddDtmBarrier_With_IConfiguration_Should_Succeed()
{
var dict = new Dictionary<string, string>
{
{ "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<IOptions<DtmOptions>>();
var dtmOptions = dtmOptionsAccs.Value;
Assert.Equal("mysql", dtmOptions.DBType);
Assert.Equal("dtm_barrier.barrier2", dtmOptions.BarrierTableName);

var dtmClient = provider.GetService<IDtmgRPCClient>();
Assert.Null(dtmClient);

var barrierFactory = provider.GetRequiredService<IBranchBarrierFactory>();
Assert.NotNull(barrierFactory);

var specials = provider.GetServices<IDbSpecial>();
Assert.Equal(3, specials.ToList().Count);
}
}
}

0 comments on commit 883c327

Please sign in to comment.