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+ }
0 commit comments