Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba8f9f7

Browse files
authored
gPRC unit test&sample: TransactionScope dispose exception (#90)
* test(Dtmgrpc.IntegrationTests): add MySql QueryPrepared Demo- add MySql QueryPrepared Demo in IntegrationTests- Add new test case for MsgGrpc.DoAndSubmit to verify database transactions* test(Dtmgrpc.IntegrationTests): add exception handling and status check in MsgGrpcTestcover Grpc Msg.DoAndSubmit
1 parent2dd50e7 commitba8f9f7

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

‎tests/BusiGrpcService/Services/BusiApiService.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
usingSystem.Text.Json;
66
usingDapper;
77
usingSystem.Data.Common;
8+
usingDtmCommon;
89
usingDtmSERedisBarrier;
910

1011
namespaceBusiGrpcService.Services
@@ -140,6 +141,18 @@ public override async Task<BusiReply> QueryPrepared(BusiReq request, ServerCallC
140141
throwDtmgrpc.DtmGImp.Utils.DtmError2GrpcError(ex);
141142
}
142143

144+
// real mysql query prepared demo, just copy it!
145+
publicoverrideasyncTask<Empty>QueryPreparedMySqlReal(BusiReqrequest,ServerCallContextcontext)
146+
{
147+
BranchBarrierbarrier=_barrierFactory.CreateBranchBarrier(context);
148+
stringresult=awaitbarrier.QueryPrepared(this.GetBarrierConn());
149+
150+
Exceptionex=Dtmgrpc.DtmGImp.Utils.String2DtmError(result);
151+
if(ex!=null)
152+
throwDtmgrpc.DtmGImp.Utils.DtmError2GrpcError(ex);
153+
returnnewEmpty();
154+
}
155+
143156
publicoverrideasyncTask<Empty>TransInRedis(BusiReqrequest,ServerCallContextcontext)
144157
{
145158
_logger.LogInformation("TransInRedis req={req}",JsonSerializer.Serialize(request));

‎tests/Dtmgrpc.IntegrationTests/Dtmgrpc.IntegrationTests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
22+
<PackageReferenceInclude="MySqlConnector"Version="$(MySqlConnectorPackageVersion)" />
2223
</ItemGroup>
2324

2425
<ItemGroup>

‎tests/Dtmgrpc.IntegrationTests/MsgGrpcTest.cs‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
usingMicrosoft.Extensions.DependencyInjection;
22
usingSystem;
3+
usingSystem.Data.Common;
34
usingSystem.Threading.Tasks;
5+
usingSystem.Transactions;
6+
usingDapper;
7+
usingGrpc.Core;
8+
usingMySqlConnector;
49
usingXunit;
510

611
namespaceDtmgrpc.IntegrationTests
@@ -27,5 +32,100 @@ public async Task Submit_Should_Succeed()
2732
varstatus=awaitITTestHelper.GetTranStatus(gid);
2833
Assert.Equal("succeed",status);
2934
}
35+
36+
[Fact]
37+
publicasyncTaskDoAndSubmit_Should_DbTrans_Exception()
38+
{
39+
varprovider=ITTestHelper.AddDtmGrpc();
40+
vartransFactory=provider.GetRequiredService<IDtmTransFactory>();
41+
42+
vargid="msgTestGid"+Guid.NewGuid().ToString();
43+
varmsg=transFactory.NewMsgGrpc(gid);
44+
varreq=ITTestHelper.GenBusiReq(false,false);
45+
varbusiGrpc=ITTestHelper.BuisgRPCUrl;
46+
47+
msg.Add(busiGrpc+"/busi.Busi/TransIn",req);
48+
// do TransOut local, then TransIn with DTM.
49+
awaitAssert.ThrowsAsync<System.InvalidOperationException>(async()=>
50+
{
51+
// System.InvalidOperationException: A TransactionScope must be disposed on the same thread that it was created.
52+
//
53+
// System.InvalidOperationException
54+
// A TransactionScope must be disposed on the same thread that it was created.
55+
// at Dtmgrpc.MsgGrpc.DoAndSubmit(String queryPrepared, Func`2 busiCall, CancellationToken cancellationToken) in /home/yunjin/Data/projects/github/dtm-labs/client-csharp/src/Dtmgrpc/Msg/MsgGrpc.cs:line 110
56+
57+
awaitmsg.DoAndSubmit(busiGrpc+"/busi.Busi/QueryPreparedMySqlReal",async branchBarrier=>
58+
{
59+
MySqlConnectionconn=getBarrierMySqlConnection();
60+
awaitbranchBarrier.Call(conn,()=>
61+
{
62+
Tasktask=this.LocalAdjustBalance(conn,TransOutUID,-req.Amount,"SUCCESS");
63+
returntask;
64+
},
65+
TransactionScopeOption.Required,
66+
IsolationLevel.ReadCommitted
67+
// , default TransactionScopeAsyncFlowOption.Suppress
68+
);
69+
});
70+
});
71+
72+
awaitTask.Delay(4000);
73+
varstatus=awaitITTestHelper.GetTranStatus(gid);
74+
// The exception did not affect the local transaction commit
75+
Assert.Equal("succeed",status);
76+
}
77+
78+
[Fact]
79+
publicasyncTaskDoAndSubmit_Should_Succeed()
80+
{
81+
varprovider=ITTestHelper.AddDtmGrpc();
82+
vartransFactory=provider.GetRequiredService<IDtmTransFactory>();
83+
84+
vargid="msgTestGid"+Guid.NewGuid().ToString();
85+
varmsg=transFactory.NewMsgGrpc(gid);
86+
varreq=ITTestHelper.GenBusiReq(false,false);
87+
varbusiGrpc=ITTestHelper.BuisgRPCUrl;
88+
89+
msg.Add(busiGrpc+"/busi.Busi/TransIn",req);
90+
// do TransOut local, then TransIn with DTM.
91+
92+
awaitmsg.DoAndSubmit(busiGrpc+"/busi.Busi/QueryPreparedMySqlReal",async branchBarrier=>
93+
{
94+
MySqlConnectionconn=getBarrierMySqlConnection();
95+
awaitbranchBarrier.Call(conn,()=>
96+
{
97+
Tasktask=this.LocalAdjustBalance(conn,TransOutUID,-req.Amount,"SUCCESS");
98+
returntask;
99+
},
100+
TransactionScopeOption.Required,
101+
IsolationLevel.ReadCommitted,
102+
TransactionScopeAsyncFlowOption.Enabled);
103+
});
104+
105+
awaitTask.Delay(2000);
106+
varstatus=awaitITTestHelper.GetTranStatus(gid);
107+
Assert.Equal("succeed",status);
108+
}
109+
110+
privatestaticreadonlyintTransOutUID=1;
111+
112+
privatestaticreadonlyintTransInUID=2;
113+
114+
privateMySqlConnectiongetBarrierMySqlConnection()=>new("Server=localhost;port=3306;User ID=root;Password=123456;Database=dtm_barrier");
115+
116+
privateasyncTaskLocalAdjustBalance(DbConnectionconn,intuid,longamount,stringresult)
117+
{
118+
// _logger.LogInformation("AdjustBalanceLocal uid={uid}, amount={amount}, result={result}", uid, amount, result);
119+
120+
if(result.Equals("FAILURE"))
121+
{
122+
thrownewRpcException(newStatus(StatusCode.Aborted,"FAILURE"));
123+
}
124+
125+
awaitconn.ExecuteAsync(
126+
sql:"update dtm_busi.user_account set balance = balance + @balance where user_id = @user_id",
127+
param:new{balance=amount,user_id=uid}
128+
);
129+
}
30130
}
31131
}

‎tests/protos/busi.proto‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ service Busi {
4545
rpcTransOutRevertRedis(BusiReq)returns (google.protobuf.Empty) {}
4646

4747
rpcQueryPrepared(BusiReq)returns (BusiReply) {}
48-
rpcQueryPreparedB(BusiReq)returns (google.protobuf.Empty) {}
48+
rpcQueryPreparedMySqlReal(BusiReq)returns (google.protobuf.Empty) {}
4949
rpcQueryPreparedRedis(BusiReq)returns (google.protobuf.Empty) {}
5050
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp