@@ -19,7 +19,7 @@ public async void Execute_Should_Submit()
1919 TestHelper . MockTransCallDtm ( dtmClient , Constant . Request . OPERATION_PREPARE , false ) ;
2020 TestHelper . MockTransRegisterBranch ( dtmClient , Constant . Request . OPERATION_REGISTERBRANCH , false ) ;
2121 TestHelper . MockTransRequestBranch ( dtmClient , System . Net . HttpStatusCode . OK ) ;
22-
22+
2323 var globalTrans = new TccGlobalTransaction ( dtmClient . Object , NullLoggerFactory . Instance ) ;
2424 var res = await globalTrans . Excecute ( async ( tcc ) =>
2525 {
@@ -101,31 +101,80 @@ public async void Set_TransOptions_Should_Succeed()
101101
102102 var gid = "tcc_gid" ;
103103 var globalTrans = new TccGlobalTransaction ( dtmClient . Object , NullLoggerFactory . Instance ) ;
104- var res = await globalTrans . Excecute ( gid , tcc =>
104+ var res = await globalTrans . Excecute ( gid , tcc =>
105105 {
106106 tcc . EnableWaitResult ( ) ;
107107 tcc . SetRetryInterval ( 10 ) ;
108108 tcc . SetTimeoutToFail ( 100 ) ;
109- tcc . SetBranchHeaders ( new Dictionary < string , string >
109+ tcc . SetBranchHeaders ( new Dictionary < string , string >
110110 {
111111 { "bh1" , "123" } ,
112112 { "bh2" , "456" } ,
113113 } ) ;
114- } , async ( tcc ) =>
114+ } , async ( tcc ) =>
115+ {
116+ var res1 = await tcc . CallBranch ( new { } , "http://localhost:9999/TransOutTry" , "http://localhost:9999/TransOutConfirm" , "http://localhost:9999/TransOutCancel" , default ) ;
117+ var res2 = await tcc . CallBranch ( new { } , "http://localhost:9999/TransInTry" , "http://localhost:9999/TransInConfirm" , "http://localhost:9999/TransInCancel" , default ) ;
118+
119+ var transBase = tcc . GetTransBase ( ) ;
120+
121+ Assert . True ( transBase . WaitResult ) ;
122+ Assert . Equal ( 10 , transBase . RetryInterval ) ;
123+ Assert . Equal ( 100 , transBase . TimeoutToFail ) ;
124+ Assert . Contains ( "bh1" , transBase . BranchHeaders . Keys ) ;
125+ Assert . Contains ( "bh2" , transBase . BranchHeaders . Keys ) ;
126+ } ) ;
127+
128+ Assert . Equal ( gid , res ) ;
129+ }
130+
131+ [ Fact ]
132+ public async void Execute_Should_Abort_With_RollbackReason_When_Occure_Exception ( )
133+ {
134+ var dtmClient = new Mock < IDtmClient > ( ) ;
135+ TestHelper . MockTransCallDtm ( dtmClient , Constant . Request . OPERATION_PREPARE , false ) ;
136+ TestHelper . MockTransCallDtm ( dtmClient , Constant . Request . OPERATION_ABORT , false ) ;
137+ TestHelper . MockTransRegisterBranch ( dtmClient , Constant . Request . OPERATION_REGISTERBRANCH , true , "123123123" ) ;
138+ TestHelper . MockTransRequestBranch ( dtmClient , System . Net . HttpStatusCode . BadRequest ) ;
139+
140+ var gid = "tcc_gid" ;
141+ var globalTrans = new TccGlobalTransaction ( dtmClient . Object , NullLoggerFactory . Instance ) ;
142+ var res = await globalTrans . Excecute ( gid , async ( tcc ) =>
115143 {
116144 var res1 = await tcc . CallBranch ( new { } , "http://localhost:9999/TransOutTry" , "http://localhost:9999/TransOutConfirm" , "http://localhost:9999/TransOutCancel" , default ) ;
117145 var res2 = await tcc . CallBranch ( new { } , "http://localhost:9999/TransInTry" , "http://localhost:9999/TransInConfirm" , "http://localhost:9999/TransInCancel" , default ) ;
146+ } ) ;
118147
119- var transBase = tcc . GetTransBase ( ) ;
148+ Assert . Empty ( res ) ;
149+ dtmClient . Verify ( x => x . TransCallDtm ( It . Is < TransBase > ( x => x . RollbackReason . Equals ( "123123123" ) ) , It . IsAny < object > ( ) , Constant . Request . OPERATION_ABORT , It . IsAny < CancellationToken > ( ) ) , Times . Once ) ;
150+ }
151+
152+ [ Fact ]
153+ public async void Execute_Should_Abort_With_Big_RollbackReason_When_Occure_Exception ( )
154+ {
155+ var builder = new System . Text . StringBuilder ( 2048 ) ;
156+ for ( int i = 0 ; i < 1100 ; i ++ )
157+ {
158+ builder . Append ( "r" ) ;
159+ }
160+ var ex = builder . ToString ( ) ;
161+
162+ var dtmClient = new Mock < IDtmClient > ( ) ;
163+ TestHelper . MockTransCallDtm ( dtmClient , Constant . Request . OPERATION_PREPARE , false ) ;
164+ TestHelper . MockTransCallDtm ( dtmClient , Constant . Request . OPERATION_ABORT , false ) ;
165+ TestHelper . MockTransRegisterBranch ( dtmClient , Constant . Request . OPERATION_REGISTERBRANCH , true , ex ) ;
166+ TestHelper . MockTransRequestBranch ( dtmClient , System . Net . HttpStatusCode . BadRequest ) ;
120167
121- Assert . True ( transBase . WaitResult ) ;
122- Assert . Equal ( 10 , transBase . RetryInterval ) ;
123- Assert . Equal ( 100 , transBase . TimeoutToFail ) ;
124- Assert . Contains ( "bh1" , transBase . BranchHeaders . Keys ) ;
125- Assert . Contains ( "bh2" , transBase . BranchHeaders . Keys ) ;
168+ var gid = "tcc_gid" ;
169+ var globalTrans = new TccGlobalTransaction ( dtmClient . Object , NullLoggerFactory . Instance ) ;
170+ var res = await globalTrans . Excecute ( gid , async ( tcc ) =>
171+ {
172+ var res1 = await tcc . CallBranch ( new { } , "http://localhost:9999/TransOutTry" , "http://localhost:9999/TransOutConfirm" , "http://localhost:9999/TransOutCancel" , default ) ;
173+ var res2 = await tcc . CallBranch ( new { } , "http://localhost:9999/TransInTry" , "http://localhost:9999/TransInConfirm" , "http://localhost:9999/TransInCancel" , default ) ;
126174 } ) ;
127175
128- Assert . Equal ( gid , res ) ;
176+ Assert . Empty ( res ) ;
177+ dtmClient . Verify ( x => x . TransCallDtm ( It . Is < TransBase > ( x => x . RollbackReason . Equals ( ex . Substring ( 0 , 1023 ) ) ) , It . IsAny < object > ( ) , Constant . Request . OPERATION_ABORT , It . IsAny < CancellationToken > ( ) ) , Times . Once ) ;
129178 }
130179 }
131180}
0 commit comments