Skip to content

Commit 37ce796

Browse files
authored
Merge pull request #18 from catcherwong/tcctest
tests: add ut for tcc
2 parents 65266c3 + f6d0fbe commit 37ce796

2 files changed

Lines changed: 99 additions & 5 deletions

File tree

src/Dtmcli.Tests/TccTests.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Collections.Generic;
2+
using System.Net.Http;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Logging.Abstractions;
6+
using Moq;
7+
using Xunit;
8+
9+
namespace Dtmcli.Tests
10+
{
11+
public class TccTests
12+
{
13+
[Fact]
14+
public async void Execute_Should_Submit()
15+
{
16+
var dtmClient = new Mock<IDtmClient>();
17+
MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
18+
MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
19+
MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK);
20+
21+
var gid = "tcc_gid";
22+
var globalTrans = new TccGlobalTransaction(dtmClient.Object, NullLoggerFactory.Instance);
23+
var res = await globalTrans.Excecute(gid, async (tcc) =>
24+
{
25+
var res1 = await tcc.CallBranch(new { }, "http://localhost:9999/TransOutTry", "http://localhost:9999/TransOutConfirm", "http://localhost:9999/TransOutCancel", default);
26+
var res2 = await tcc.CallBranch(new { }, "http://localhost:9999/TransInTry", "http://localhost:9999/TransInConfirm", "http://localhost:9999/TransInCancel", default);
27+
});
28+
29+
Assert.Equal(gid, res);
30+
}
31+
32+
[Fact]
33+
public async void Execute_Should_Abort_When_CallBranch_With_Old_Ver_Exception()
34+
{
35+
var dtmClient = new Mock<IDtmClient>();
36+
MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
37+
MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
38+
MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
39+
MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.OK, "FAILURE");
40+
41+
var gid = "tcc_gid";
42+
var globalTrans = new TccGlobalTransaction(dtmClient.Object, NullLoggerFactory.Instance);
43+
var res = await globalTrans.Excecute(gid, async (tcc) =>
44+
{
45+
var res1 = await tcc.CallBranch(new { }, "http://localhost:9999/TransOutTry", "http://localhost:9999/TransOutConfirm", "http://localhost:9999/TransOutCancel", default);
46+
var res2 = await tcc.CallBranch(new { }, "http://localhost:9999/TransInTry", "http://localhost:9999/TransInConfirm", "http://localhost:9999/TransInCancel", default);
47+
});
48+
49+
Assert.Empty(res);
50+
dtmClient.Verify(x => x.TransCallDtm(It.IsAny<DtmImp.TransBase>(), It.IsAny<object>(), Constant.Request.OPERATION_ABORT, It.IsAny<CancellationToken>()), Times.Once);
51+
}
52+
53+
[Fact]
54+
public async void Execute_Should_Abort_When_CallBranch_With_New_Ver_Exception()
55+
{
56+
var dtmClient = new Mock<IDtmClient>();
57+
MockTransCallDtm(dtmClient, Constant.Request.OPERATION_PREPARE, true);
58+
MockTransCallDtm(dtmClient, Constant.Request.OPERATION_ABORT, true);
59+
MockTransRegisterBranch(dtmClient, Constant.Request.OPERATION_REGISTERBRANCH, true);
60+
MockTransRequestBranch(dtmClient, System.Net.HttpStatusCode.BadRequest);
61+
62+
var gid = "tcc_gid";
63+
var globalTrans = new TccGlobalTransaction(dtmClient.Object, NullLoggerFactory.Instance);
64+
var res = await globalTrans.Excecute(gid, async (tcc) =>
65+
{
66+
var res1 = await tcc.CallBranch(new { }, "http://localhost:9999/TransOutTry", "http://localhost:9999/TransOutConfirm", "http://localhost:9999/TransOutCancel", default);
67+
var res2 = await tcc.CallBranch(new { }, "http://localhost:9999/TransInTry", "http://localhost:9999/TransInConfirm", "http://localhost:9999/TransInCancel", default);
68+
});
69+
70+
Assert.Empty(res);
71+
dtmClient.Verify(x => x.TransCallDtm(It.IsAny<DtmImp.TransBase>(), It.IsAny<object>(), Constant.Request.OPERATION_ABORT, It.IsAny<CancellationToken>()), Times.Once);
72+
}
73+
74+
private void MockTransCallDtm(Mock<IDtmClient> mock, string op, bool result)
75+
{
76+
mock
77+
.Setup(x => x.TransCallDtm(It.IsAny<DtmImp.TransBase>(), It.IsAny<object>(), op, It.IsAny<CancellationToken>()))
78+
.Returns(Task.FromResult(result));
79+
}
80+
81+
private void MockTransRegisterBranch(Mock<IDtmClient> mock, string op, bool result)
82+
{
83+
mock
84+
.Setup(x => x.TransRegisterBranch(It.IsAny<DtmImp.TransBase>(), It.IsAny<Dictionary<string, string>>(), op, It.IsAny<CancellationToken>()))
85+
.Returns(Task.FromResult(result));
86+
}
87+
88+
private void MockTransRequestBranch(Mock<IDtmClient> mock, System.Net.HttpStatusCode statusCode, string content = "content")
89+
{
90+
var httpRspMsg = new HttpResponseMessage(statusCode);
91+
httpRspMsg.Content = new StringContent(content);
92+
93+
mock
94+
.Setup(x => x.TransRequestBranch(It.IsAny<DtmImp.TransBase>(), It.IsAny<HttpMethod>(), It.IsAny<object>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
95+
.Returns(Task.FromResult(httpRspMsg));
96+
}
97+
}
98+
}

src/Dtmcli/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ namespace Dtmcli
55
{
66
public static class ServiceCollectionExtensions
77
{
8-
internal static IServiceCollection ServiceCollection;
9-
10-
118
public static IServiceCollection AddDtmcli(this IServiceCollection services, Action<DtmOptions> setupAction)
129
{
1310
if(setupAction == null)
@@ -16,7 +13,7 @@ public static IServiceCollection AddDtmcli(this IServiceCollection services, Act
1613
}
1714
var options = new DtmOptions();
1815
setupAction(options);
19-
ServiceCollection = services;
16+
2017
services.AddHttpClient("dtmClient", client =>
2118
{
2219
client.BaseAddress = new Uri(options.DtmUrl);
@@ -26,7 +23,6 @@ public static IServiceCollection AddDtmcli(this IServiceCollection services, Act
2623

2724
services.AddSingleton<TccGlobalTransaction>();
2825

29-
// 子事务屏障
3026
services.AddSingleton<IBranchBarrierFactory, DefaultBranchBarrierFactory>();
3127
return services;
3228
}

0 commit comments

Comments
 (0)