Skip to content

Commit

Permalink
refactor: Task<bool> => Task, and rm some constant
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Feb 19, 2022
1 parent 26579b2 commit ffaf0f1
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 242 deletions.
6 changes: 3 additions & 3 deletions src/Dtmcli.Tests/BranchBarrierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ public async void Call_Should_Throw_Duplicated_Exception_When_QueryPrepared_At_F

var connQ = GetDbConnection();
connQ.Mocks.When(cmd => cmd.CommandText.Contains("insert", StringComparison.Ordinal)).ReturnsScalar(cmd => 1);
connQ.Mocks.When(cmd => cmd.CommandText.Contains("select", StringComparison.OrdinalIgnoreCase)).ReturnsScalar(cmd => Constant.Barrier.MSG_BARRIER_REASON);
connQ.Mocks.When(cmd => cmd.CommandText.Contains("select", StringComparison.OrdinalIgnoreCase)).ReturnsScalar(cmd => DtmCommon.Constant.Barrier.MSG_BARRIER_REASON);

// QueryPrepared at first
var qRes = await branchBarrier.QueryPrepared(connQ);
Assert.Equal(Constant.ErrFailure, qRes);
Assert.Equal(DtmCommon.Constant.ResultFailure, qRes);

var connC = GetDbConnection();
connC.Mocks.When(cmd => cmd.Parameters.AsList().Select(x => x.Value).Contains("msg")).ReturnsScalar(cmd => 0);
Expand All @@ -175,7 +175,7 @@ public async void Call_Should_Throw_Duplicated_Exception_When_QueryPrepared_At_F

// Call later
var ex = await Assert.ThrowsAsync<DtmDuplicatedException>(async () => await branchBarrier.Call(connC, mockBusiCall.Object));
Assert.Equal(Constant.ResultDuplicated, ex.Message);
Assert.Equal(DtmCommon.Constant.ResultDuplicated, ex.Message);
mockBusiCall.Verify(x => x.Invoke(It.IsAny<System.Data.Common.DbTransaction>()), Times.Never);
}
}
Expand Down
36 changes: 16 additions & 20 deletions src/Dtmcli.Tests/MsgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,17 @@ public async void Submit_Should_Succeed()
{ "bh2", "456" },
});

var prepareRes = await msg.Prepare(busi + "/query");
Assert.True(prepareRes);
await msg.Prepare(busi + "/query");
await msg.Submit();

var submitRes = await msg.Submit();
Assert.True(submitRes);
Assert.True(true);
}

[Fact]
public async void DoAndSubmitDB_Should_Throw_Exception_When_Transbase_InValid()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);

var gid = string.Empty;
var msg = new Msg(dtmClient.Object, _branchBarrierFactory, gid);
Expand All @@ -83,7 +82,7 @@ public async void DoAndSubmitDB_Should_Throw_Exception_When_Transbase_InValid()
public async void DoAndSubmitDB_Should_Not_Call_Barrier_When_Prepare_Fail()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);

var gid = "TestMsgNormal";
var msg = new Msg(dtmClient.Object, _branchBarrierFactory, gid);
Expand All @@ -95,17 +94,16 @@ public async void DoAndSubmitDB_Should_Not_Call_Barrier_When_Prepare_Fail()
var db = new MockDbConnection();
var mockBusiCall = new Mock<Func<DbTransaction, Task<bool>>>();

await msg.DoAndSubmitDB(busi + "/query", db, x => Task.FromResult(true));

await Assert.ThrowsAnyAsync<Exception>(async () => await msg.DoAndSubmitDB(busi + "/query", db, x => Task.FromResult(true)));
mockBusiCall.Verify(x => x.Invoke(It.IsAny<DbTransaction>()), Times.Never);
}

[Fact]
public async void DoAndSubmitDB_Should_Succeed()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, false);

var gid = "TestMsgNormal";
var msg = new Msg(dtmClient.Object, _branchBarrierFactory, gid);
Expand All @@ -130,8 +128,8 @@ public async void DoAndSubmitDB_Should_Succeed()
public async void DoAndSubmitDB_Should_Abort_When_BusiCall_ThrowExeption_With_ResultFailure()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);

var gid = "TestMsgNormal";
var msg = new Msg(dtmClient.Object, _branchBarrierFactory, gid);
Expand All @@ -145,20 +143,19 @@ public async void DoAndSubmitDB_Should_Abort_When_BusiCall_ThrowExeption_With_Re
db.Mocks.When(x => x.CommandText.Contains("select", StringComparison.OrdinalIgnoreCase)).ReturnsScalar(cmd => "rollback");

var mockBusiCall = new Mock<Func<DbTransaction, Task>>();
mockBusiCall.Setup(x => x.Invoke(It.IsAny<DbTransaction>())).Throws(new Exception(Constant.ResultFailure));

await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object);
mockBusiCall.Setup(x => x.Invoke(It.IsAny<DbTransaction>())).Throws(new DtmFailureException());

await Assert.ThrowsAsync<DtmFailureException>(async () => await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object));
dtmClient.Verify(x => x.TransCallDtm(It.IsAny<TransBase>(), It.IsAny<object>(), Constant.Request.OPERATION_ABORT, It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async void DoAndSubmitDB_Should_QueryPrepared_When_BusiCall_ThrowExeption_Without_ResultFailure()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, false);
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK);

var gid = "TestMsgNormal";
Expand All @@ -175,8 +172,7 @@ public async void DoAndSubmitDB_Should_QueryPrepared_When_BusiCall_ThrowExeption
var mockBusiCall = new Mock<Func<DbTransaction, Task<bool>>>();
mockBusiCall.Setup(x => x.Invoke(It.IsAny<DbTransaction>())).Throws(new Exception("ex"));

await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object);

await Assert.ThrowsAsync<Exception>(async () => await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object));
dtmClient.Verify(x => x.TransRequestBranch(It.IsAny<TransBase>(), It.IsAny<HttpMethod>(), It.IsAny<object>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
}

Expand Down
19 changes: 19 additions & 0 deletions src/Dtmcli.Tests/SagaTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DtmCommon;
using Moq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -48,6 +49,24 @@ public async void Submit_Should_Succeed()
await sage.Submit();
}

[Fact]
public async void Submit_Should_ThrowException()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, true);

var gid = "TestSagaNormal";
var saga = new Saga(dtmClient.Object, gid);

var busi = "http://localhost:8081/api/busi";
var req = new { Amount = 30 };

saga.Add(string.Concat(busi, "/TransOut"), string.Concat(busi, "/TransOutRevert"), req)
.Add(string.Concat(busi, "/TransIn"), string.Concat(busi, "/TransInRevert"), req);

await Assert.ThrowsAnyAsync<Exception>(async () => await saga.Submit());
}

public class SageMockHttpMessageHandler : DelegatingHandler
{
public SageMockHttpMessageHandler()
Expand Down
20 changes: 10 additions & 10 deletions src/Dtmcli.Tests/TccTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class TccTests
public async void Execute_Should_Submit()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK);

var gid = "tcc_gid";
Expand All @@ -34,9 +34,9 @@ public async void Execute_Should_Submit()
public async void Execute_Should_Abort_When_CallBranch_With_Old_Ver_Exception()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK, "FAILURE");

var gid = "tcc_gid";
Expand All @@ -55,9 +55,9 @@ public async void Execute_Should_Abort_When_CallBranch_With_Old_Ver_Exception()
public async void Execute_Should_Abort_When_CallBranch_With_New_Ver_Exception()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.BadRequest);

var gid = "tcc_gid";
Expand All @@ -76,8 +76,8 @@ public async void Execute_Should_Abort_When_CallBranch_With_New_Ver_Exception()
public async void Set_TransOptions_Should_Succeed()
{
var dtmClient = new Mock<IDtmClient>();
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK);

var gid = "tcc_gid";
Expand Down
38 changes: 28 additions & 10 deletions src/Dtmcli.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -10,21 +12,37 @@ namespace Dtmcli.Tests
{
public class TestHelper
{
public static void MockTransCallDtm(Mock<IDtmClient> mock, string op, bool result)
public static void MockTransCallDtm(Mock<IDtmClient> mock, string op, bool isEx)
{
mock
.Setup(x => x.TransCallDtm(It.IsAny<TransBase>(), It.IsAny<object>(), op, It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(result));
var setup = mock
.Setup(x => x.TransCallDtm(It.IsAny<TransBase>(), It.IsAny<object>(), op, It.IsAny<CancellationToken>()));

if (isEx)
{
setup.Throws(new Exception(""));
}
else
{
setup.Returns(Task.CompletedTask);
}
}

public static void MockTransRegisterBranch(Mock<IDtmClient> mock, string op, bool result)
public static void MockTransRegisterBranch(Mock<IDtmClient> mock, string op, bool isEx)
{
mock
.Setup(x => x.TransRegisterBranch(It.IsAny<TransBase>(), It.IsAny<Dictionary<string, string>>(), op, It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(result));
var setup = mock
.Setup(x => x.TransRegisterBranch(It.IsAny<TransBase>(), It.IsAny<Dictionary<string, string>>(), op, It.IsAny<CancellationToken>()));

if (isEx)
{
setup.Throws(new Exception(""));
}
else
{
setup.Returns(Task.CompletedTask);
}
}

public static void MockTransRequestBranch(Mock<IDtmClient> mock, System.Net.HttpStatusCode statusCode, string content = "content")
public static void MockTransRequestBranch(Mock<IDtmClient> mock, HttpStatusCode statusCode, string content = "content")
{
var httpRspMsg = new HttpResponseMessage(statusCode);
httpRspMsg.Content = new StringContent(content);
Expand Down
95 changes: 4 additions & 91 deletions src/Dtmcli/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,7 @@ internal class Constant
{
internal static readonly string DtmClientHttpName = "dtmClient";
internal static readonly string BranchClientHttpName = "branchClient";

/// <summary>
/// status for global/branch trans status.
/// </summary>
internal static readonly string StatusPrepared = "prepared";

/// <summary>
/// status for global trans status.
/// </summary>
internal static readonly string StatusSubmitted = "submitted";

/// <summary>
/// status for global/branch trans status.
/// </summary>
internal static readonly string StatusSucceed = "succeed";

/// <summary>
/// status for global/branch trans status.
/// </summary>
internal static readonly string StatusFailed = "failed";

/// <summary>
/// status for global trans status.
/// </summary>
internal static readonly string StatusAborting = "aborting";

/// <summary>
/// branch type for TCC
/// </summary>
internal static readonly string BranchTry = "try";

/// <summary>
/// branch type for TCC
/// </summary>
internal static readonly string BranchConfirm = "confirm";

/// <summary>
/// branch type for TCC
/// </summary>
internal static readonly string BranchCancel = "cancel";

/// <summary>
/// branch type for XA
/// </summary>
internal static readonly string BranchCommit = "commit";

/// <summary>
/// branch type for XA
/// </summary>
internal static readonly string BranchRollback = "rollback";

internal static readonly string ErrFailure = "FAILURE";

internal static readonly string ResultFailure = "FAILURE";
internal static readonly string ResultSuccess = "SUCCESS";
internal static readonly string ResultOngoing = "ONGOING";

/// <summary>
/// error of DUPLICATED for only msg
/// if QueryPrepared executed before call. then DoAndSubmit return this error
/// </summary>
internal static readonly string ResultDuplicated = "DUPLICATED";

internal static readonly int FailureStatusCode = 400;


internal class Request
{
internal static readonly string CONTENT_TYPE = "application/json";
Expand All @@ -91,6 +27,8 @@ internal class Request

internal static readonly string OP = "op";

internal static readonly string DTM = "dtm";

internal static readonly string CODE = "code";

internal static readonly string MESSAGE = "message";
Expand All @@ -115,32 +53,7 @@ internal class Request
/// </summary>
internal static readonly string BRANCH_COMPENSATE = "compensate";

internal static readonly string TYPE_TCC = "tcc";

internal static readonly string TYPE_SAGA = "saga";

internal static readonly string TYPE_MSG = "msg";

internal static readonly string URL_NewGid = "/api/dtmsvr/newGid";
}

internal class Barrier
{
internal static readonly string TABLE_NAME = "dtm_barrier.barrier";

internal static readonly string DBTYPE_MYSQL = "mysql";

internal static readonly string DBTYPE_POSTGRES = "postgres";

internal static readonly string DBTYPE_SQLSERVER = "sqlserver";

internal static readonly string PG_CONSTRAINT = "uniq_barrier";

internal static readonly string MSG_BARRIER_REASON = "rollback";

internal static readonly string MSG_BRANCHID = "00";

internal static readonly string MSG_BARRIER_ID = "01";
}
}
}
}
Loading

0 comments on commit ffaf0f1

Please sign in to comment.