Skip to content

Commit ffaf0f1

Browse files
committed
refactor: Task<bool> => Task, and rm some constant
1 parent 26579b2 commit ffaf0f1

File tree

16 files changed

+187
-242
lines changed

16 files changed

+187
-242
lines changed

src/Dtmcli.Tests/BranchBarrierTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ public async void Call_Should_Throw_Duplicated_Exception_When_QueryPrepared_At_F
162162

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

167167
// QueryPrepared at first
168168
var qRes = await branchBarrier.QueryPrepared(connQ);
169-
Assert.Equal(Constant.ErrFailure, qRes);
169+
Assert.Equal(DtmCommon.Constant.ResultFailure, qRes);
170170

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

176176
// Call later
177177
var ex = await Assert.ThrowsAsync<DtmDuplicatedException>(async () => await branchBarrier.Call(connC, mockBusiCall.Object));
178-
Assert.Equal(Constant.ResultDuplicated, ex.Message);
178+
Assert.Equal(DtmCommon.Constant.ResultDuplicated, ex.Message);
179179
mockBusiCall.Verify(x => x.Invoke(It.IsAny<System.Data.Common.DbTransaction>()), Times.Never);
180180
}
181181
}

src/Dtmcli.Tests/MsgTests.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,17 @@ public async void Submit_Should_Succeed()
5454
{ "bh2", "456" },
5555
});
5656

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

60-
var submitRes = await msg.Submit();
61-
Assert.True(submitRes);
60+
Assert.True(true);
6261
}
6362

6463
[Fact]
6564
public async void DoAndSubmitDB_Should_Throw_Exception_When_Transbase_InValid()
6665
{
6766
var dtmClient = new Mock<IDtmClient>();
68-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
67+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
6968

7069
var gid = string.Empty;
7170
var msg = new Msg(dtmClient.Object, _branchBarrierFactory, gid);
@@ -83,7 +82,7 @@ public async void DoAndSubmitDB_Should_Throw_Exception_When_Transbase_InValid()
8382
public async void DoAndSubmitDB_Should_Not_Call_Barrier_When_Prepare_Fail()
8483
{
8584
var dtmClient = new Mock<IDtmClient>();
86-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
85+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
8786

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

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

103101
[Fact]
104102
public async void DoAndSubmitDB_Should_Succeed()
105103
{
106104
var dtmClient = new Mock<IDtmClient>();
107-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
108-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, true);
105+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
106+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, false);
109107

110108
var gid = "TestMsgNormal";
111109
var msg = new Msg(dtmClient.Object, _branchBarrierFactory, gid);
@@ -130,8 +128,8 @@ public async void DoAndSubmitDB_Should_Succeed()
130128
public async void DoAndSubmitDB_Should_Abort_When_BusiCall_ThrowExeption_With_ResultFailure()
131129
{
132130
var dtmClient = new Mock<IDtmClient>();
133-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
134-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
131+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
132+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
135133

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

147145
var mockBusiCall = new Mock<Func<DbTransaction, Task>>();
148-
mockBusiCall.Setup(x => x.Invoke(It.IsAny<DbTransaction>())).Throws(new Exception(Constant.ResultFailure));
149-
150-
await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object);
146+
mockBusiCall.Setup(x => x.Invoke(It.IsAny<DbTransaction>())).Throws(new DtmFailureException());
151147

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

155152
[Fact]
156153
public async void DoAndSubmitDB_Should_QueryPrepared_When_BusiCall_ThrowExeption_Without_ResultFailure()
157154
{
158155
var dtmClient = new Mock<IDtmClient>();
159-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
160-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
161-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, true);
156+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
157+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
158+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, false);
162159
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK);
163160

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

178-
await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object);
179-
175+
await Assert.ThrowsAsync<Exception>(async () => await msg.DoAndSubmitDB(busi + "/query", db, mockBusiCall.Object));
180176
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);
181177
}
182178

src/Dtmcli.Tests/SagaTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DtmCommon;
22
using Moq;
3+
using System;
34
using System.Collections.Generic;
45
using System.Net.Http;
56
using System.Threading;
@@ -48,6 +49,24 @@ public async void Submit_Should_Succeed()
4849
await sage.Submit();
4950
}
5051

52+
[Fact]
53+
public async void Submit_Should_ThrowException()
54+
{
55+
var dtmClient = new Mock<IDtmClient>();
56+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_SUBMIT, true);
57+
58+
var gid = "TestSagaNormal";
59+
var saga = new Saga(dtmClient.Object, gid);
60+
61+
var busi = "http://localhost:8081/api/busi";
62+
var req = new { Amount = 30 };
63+
64+
saga.Add(string.Concat(busi, "/TransOut"), string.Concat(busi, "/TransOutRevert"), req)
65+
.Add(string.Concat(busi, "/TransIn"), string.Concat(busi, "/TransInRevert"), req);
66+
67+
await Assert.ThrowsAnyAsync<Exception>(async () => await saga.Submit());
68+
}
69+
5170
public class SageMockHttpMessageHandler : DelegatingHandler
5271
{
5372
public SageMockHttpMessageHandler()

src/Dtmcli.Tests/TccTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class TccTests
1515
public async void Execute_Should_Submit()
1616
{
1717
var dtmClient = new Mock<IDtmClient>();
18-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
19-
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
18+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
19+
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
2020
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK);
2121

2222
var gid = "tcc_gid";
@@ -34,9 +34,9 @@ public async void Execute_Should_Submit()
3434
public async void Execute_Should_Abort_When_CallBranch_With_Old_Ver_Exception()
3535
{
3636
var dtmClient = new Mock<IDtmClient>();
37-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
38-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
39-
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
37+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
38+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
39+
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
4040
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK, "FAILURE");
4141

4242
var gid = "tcc_gid";
@@ -55,9 +55,9 @@ public async void Execute_Should_Abort_When_CallBranch_With_Old_Ver_Exception()
5555
public async void Execute_Should_Abort_When_CallBranch_With_New_Ver_Exception()
5656
{
5757
var dtmClient = new Mock<IDtmClient>();
58-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
59-
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
60-
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
58+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, false);
59+
TestHelper.MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, false);
60+
TestHelper.MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, false);
6161
TestHelper.MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.BadRequest);
6262

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

8383
var gid = "tcc_gid";

src/Dtmcli.Tests/TestHelper.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
24
using System.Net.Http;
35
using System.Threading;
46
using System.Threading.Tasks;
@@ -10,21 +12,37 @@ namespace Dtmcli.Tests
1012
{
1113
public class TestHelper
1214
{
13-
public static void MockTransCallDtm(Mock<IDtmClient> mock, string op, bool result)
15+
public static void MockTransCallDtm(Mock<IDtmClient> mock, string op, bool isEx)
1416
{
15-
mock
16-
.Setup(x => x.TransCallDtm(It.IsAny<TransBase>(), It.IsAny<object>(), op, It.IsAny<CancellationToken>()))
17-
.Returns(Task.FromResult(result));
17+
var setup = mock
18+
.Setup(x => x.TransCallDtm(It.IsAny<TransBase>(), It.IsAny<object>(), op, It.IsAny<CancellationToken>()));
19+
20+
if (isEx)
21+
{
22+
setup.Throws(new Exception(""));
23+
}
24+
else
25+
{
26+
setup.Returns(Task.CompletedTask);
27+
}
1828
}
1929

20-
public static void MockTransRegisterBranch(Mock<IDtmClient> mock, string op, bool result)
30+
public static void MockTransRegisterBranch(Mock<IDtmClient> mock, string op, bool isEx)
2131
{
22-
mock
23-
.Setup(x => x.TransRegisterBranch(It.IsAny<TransBase>(), It.IsAny<Dictionary<string, string>>(), op, It.IsAny<CancellationToken>()))
24-
.Returns(Task.FromResult(result));
32+
var setup = mock
33+
.Setup(x => x.TransRegisterBranch(It.IsAny<TransBase>(), It.IsAny<Dictionary<string, string>>(), op, It.IsAny<CancellationToken>()));
34+
35+
if (isEx)
36+
{
37+
setup.Throws(new Exception(""));
38+
}
39+
else
40+
{
41+
setup.Returns(Task.CompletedTask);
42+
}
2543
}
2644

27-
public static void MockTransRequestBranch(Mock<IDtmClient> mock, System.Net.HttpStatusCode statusCode, string content = "content")
45+
public static void MockTransRequestBranch(Mock<IDtmClient> mock, HttpStatusCode statusCode, string content = "content")
2846
{
2947
var httpRspMsg = new HttpResponseMessage(statusCode);
3048
httpRspMsg.Content = new StringContent(content);

src/Dtmcli/Constant.cs

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,7 @@ internal class Constant
44
{
55
internal static readonly string DtmClientHttpName = "dtmClient";
66
internal static readonly string BranchClientHttpName = "branchClient";
7-
8-
/// <summary>
9-
/// status for global/branch trans status.
10-
/// </summary>
11-
internal static readonly string StatusPrepared = "prepared";
12-
13-
/// <summary>
14-
/// status for global trans status.
15-
/// </summary>
16-
internal static readonly string StatusSubmitted = "submitted";
17-
18-
/// <summary>
19-
/// status for global/branch trans status.
20-
/// </summary>
21-
internal static readonly string StatusSucceed = "succeed";
22-
23-
/// <summary>
24-
/// status for global/branch trans status.
25-
/// </summary>
26-
internal static readonly string StatusFailed = "failed";
27-
28-
/// <summary>
29-
/// status for global trans status.
30-
/// </summary>
31-
internal static readonly string StatusAborting = "aborting";
32-
33-
/// <summary>
34-
/// branch type for TCC
35-
/// </summary>
36-
internal static readonly string BranchTry = "try";
37-
38-
/// <summary>
39-
/// branch type for TCC
40-
/// </summary>
41-
internal static readonly string BranchConfirm = "confirm";
42-
43-
/// <summary>
44-
/// branch type for TCC
45-
/// </summary>
46-
internal static readonly string BranchCancel = "cancel";
47-
48-
/// <summary>
49-
/// branch type for XA
50-
/// </summary>
51-
internal static readonly string BranchCommit = "commit";
52-
53-
/// <summary>
54-
/// branch type for XA
55-
/// </summary>
56-
internal static readonly string BranchRollback = "rollback";
57-
58-
internal static readonly string ErrFailure = "FAILURE";
59-
60-
internal static readonly string ResultFailure = "FAILURE";
61-
internal static readonly string ResultSuccess = "SUCCESS";
62-
internal static readonly string ResultOngoing = "ONGOING";
63-
64-
/// <summary>
65-
/// error of DUPLICATED for only msg
66-
/// if QueryPrepared executed before call. then DoAndSubmit return this error
67-
/// </summary>
68-
internal static readonly string ResultDuplicated = "DUPLICATED";
69-
70-
internal static readonly int FailureStatusCode = 400;
71-
7+
728
internal class Request
739
{
7410
internal static readonly string CONTENT_TYPE = "application/json";
@@ -91,6 +27,8 @@ internal class Request
9127

9228
internal static readonly string OP = "op";
9329

30+
internal static readonly string DTM = "dtm";
31+
9432
internal static readonly string CODE = "code";
9533

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

118-
internal static readonly string TYPE_TCC = "tcc";
119-
120-
internal static readonly string TYPE_SAGA = "saga";
121-
122-
internal static readonly string TYPE_MSG = "msg";
123-
12456
internal static readonly string URL_NewGid = "/api/dtmsvr/newGid";
125-
}
126-
127-
internal class Barrier
128-
{
129-
internal static readonly string TABLE_NAME = "dtm_barrier.barrier";
130-
131-
internal static readonly string DBTYPE_MYSQL = "mysql";
132-
133-
internal static readonly string DBTYPE_POSTGRES = "postgres";
134-
135-
internal static readonly string DBTYPE_SQLSERVER = "sqlserver";
136-
137-
internal static readonly string PG_CONSTRAINT = "uniq_barrier";
138-
139-
internal static readonly string MSG_BARRIER_REASON = "rollback";
140-
141-
internal static readonly string MSG_BRANCHID = "00";
142-
143-
internal static readonly string MSG_BARRIER_ID = "01";
144-
}
57+
}
14558
}
14659
}

0 commit comments

Comments
 (0)