2 // Tests for combination of volatile & durable resource manangers
5 // Ankit Jain <JAnkit@novell.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
11 using System
.Transactions
;
12 using NUnit
.Framework
;
14 namespace MonoTests
.System
.Transactions
{
17 public class EnlistTest
{
21 /* Single volatile resource, SPC happens */
23 public void Vol1_Dur0 ()
25 IntResourceManager irm
= new IntResourceManager (1);
27 using (TransactionScope scope
= new TransactionScope ()) {
36 public void Vol1_Dur0_2PC ()
38 IntResourceManager irm
= new IntResourceManager (1);
40 using (TransactionScope scope
= new TransactionScope ()) {
48 /* Single volatile resource, SPC happens */
50 public void Vol1_Dur0_Fail1 ()
52 IntResourceManager irm
= new IntResourceManager (1);
54 using (TransactionScope scope
= new TransactionScope ()) {
57 /* Not completing this..
61 irm
.Check ( 0, 0, 0, 1, 0, "irm" );
65 [ExpectedException ( typeof ( TransactionAbortedException
) )]
66 public void Vol1_Dur0_Fail2 ()
68 IntResourceManager irm
= new IntResourceManager (1);
70 irm
.FailPrepare
= true;
72 using (TransactionScope scope
= new TransactionScope ()) {
80 [ExpectedException ( typeof ( TransactionAbortedException
) )]
81 public void Vol1_Dur0_Fail3 ()
83 IntResourceManager irm
= new IntResourceManager (1);
87 using (TransactionScope scope
= new TransactionScope ()) {
98 /* >1 volatile, 2PC */
100 public void Vol2_Dur0_SPC ()
102 IntResourceManager irm
= new IntResourceManager (1);
103 IntResourceManager irm2
= new IntResourceManager (3);
105 irm
.UseSingle
= true;
106 irm2
.UseSingle
= true;
107 using (TransactionScope scope
= new TransactionScope ()) {
113 irm
.Check2PC ( "irm" );
114 irm2
.Check2PC ( "irm2" );
122 public void Vol0_Dur1 ()
124 IntResourceManager irm
= new IntResourceManager (1);
125 irm
.Volatile
= false;
126 irm
.UseSingle
= true;
128 using (TransactionScope scope
= new TransactionScope ()) {
134 irm
.CheckSPC ( "irm" );
137 /* We support only 1 durable with 2PC
138 * On .net, this becomes a distributed transaction
141 [Category ("NotWorking")]
142 public void Vol0_Dur1_2PC ()
144 IntResourceManager irm
= new IntResourceManager (1);
146 /* Durable resource enlisted with a IEnlistedNotification
149 irm
.Volatile
= false;
151 using (TransactionScope scope
= new TransactionScope ()) {
159 public void Vol0_Dur1_Fail ()
161 IntResourceManager irm
= new IntResourceManager ( 1 );
163 /* Durable resource enlisted with a IEnlistedNotification
166 irm
.Volatile
= false;
168 irm
.UseSingle
= true;
170 using (TransactionScope scope
= new TransactionScope ()) {
176 catch (TransactionAbortedException
) {
177 irm
.Check ( 1, 0, 0, 0, 0, "irm" );
186 /* >1vol + 1 durable */
188 public void Vol2_Dur1 ()
190 IntResourceManager
[] irm
= new IntResourceManager
[4];
191 irm
[0] = new IntResourceManager ( 1 );
192 irm
[1] = new IntResourceManager ( 3 );
193 irm
[2] = new IntResourceManager ( 5 );
194 irm
[3] = new IntResourceManager ( 7 );
196 irm
[0].Volatile
= false;
197 for ( int i
= 0; i
< 4; i
++ )
198 irm
[i
].UseSingle
= true;
200 using (TransactionScope scope
= new TransactionScope ()) {
209 irm
[0].CheckSPC ( "irm [0]" );
211 /* Volatile RMs get 2PC */
212 for (int i
= 1; i
< 4; i
++)
213 irm
[i
].Check2PC ( "irm [" + i
+ "]" );
220 public void Vol2_Dur1_Fail1 ()
222 IntResourceManager
[] irm
= new IntResourceManager
[4];
223 irm
[0] = new IntResourceManager (1);
224 irm
[1] = new IntResourceManager (3);
225 irm
[2] = new IntResourceManager (5);
226 irm
[3] = new IntResourceManager (7);
228 irm
[0].Volatile
= false;
229 irm
[0].FailSPC
= true;
231 for ( int i
= 0; i
< 4; i
++ )
232 irm
[i
].UseSingle
= true;
234 /* Durable RM irm[0] does Abort on SPC, so
235 * all volatile RMs get Rollback */
237 using (TransactionScope scope
= new TransactionScope ()) {
246 catch (TransactionAbortedException
) {
247 irm
[0].CheckSPC ( "irm [0]" );
248 /* Volatile RMs get 2PC Prepare, and then get rolled back */
249 for (int i
= 1; i
< 4; i
++)
250 irm
[i
].Check ( 0, 1, 0, 1, 0, "irm [" + i
+ "]" );
255 * durable doesn't complete SPC
258 [Ignore ( "Correct this test, it should throw TimeOutException or something" )]
259 public void Vol2_Dur1_Fail2 ()
261 IntResourceManager
[] irm
= new IntResourceManager
[4];
262 irm
[0] = new IntResourceManager (1);
263 irm
[1] = new IntResourceManager (3);
264 irm
[2] = new IntResourceManager (5);
265 irm
[3] = new IntResourceManager (7);
267 irm
[0].Volatile
= false;
268 irm
[0].IgnoreSPC
= true;
270 for ( int i
= 0; i
< 4; i
++ )
271 irm
[i
].UseSingle
= true;
273 /* Durable RM irm[2] does on SPC, so
274 * all volatile RMs get Rollback */
276 using (TransactionScope scope
= new TransactionScope ( TransactionScopeOption
.Required
, new TimeSpan ( 0, 0, 5 ) )) {
285 catch (TransactionAbortedException
) {
286 irm
[0].CheckSPC ( "irm [0]" );
288 /* Volatile RMs get 2PC Prepare, and then get rolled back */
289 for (int i
= 1; i
< 4; i
++)
290 irm
[i
].Check ( 0, 1, 0, 1, 0, "irm [" + i
+ "]" );
295 * Volatile fails Prepare
298 public void Vol2_Dur1_Fail3 ()
300 IntResourceManager
[] irm
= new IntResourceManager
[4];
301 irm
[0] = new IntResourceManager ( 1 );
302 irm
[1] = new IntResourceManager ( 3 );
303 irm
[2] = new IntResourceManager ( 5 );
304 irm
[3] = new IntResourceManager ( 7 );
306 irm
[0].Volatile
= false;
307 irm
[2].FailPrepare
= true;
309 for ( int i
= 0; i
< 4; i
++ )
310 irm
[i
].UseSingle
= true;
312 /* Durable RM irm[2] does on SPC, so
313 * all volatile RMs get Rollback */
315 using (TransactionScope scope
= new TransactionScope ()) {
324 catch (TransactionAbortedException
) {
325 irm
[0].Check ( 0, 0, 0, 1, 0, "irm [0]");
327 /* irm [1] & [2] get prepare,
328 * [2] -> ForceRollback,
329 * [1] & [3] get rollback,
330 * [0](durable) gets rollback */
331 irm
[1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
332 irm
[2].Check ( 0, 1, 0, 0, 0, "irm [2]" );
333 irm
[3].Check ( 0, 0, 0, 1, 0, "irm [3]" );
338 Assert
.Fail ( "Expected TransactionAbortedException" );
342 public void Vol2_Dur1_Fail4 ()
344 IntResourceManager
[] irm
= new IntResourceManager
[2];
345 irm
[0] = new IntResourceManager ( 1 );
346 irm
[1] = new IntResourceManager ( 3 );
348 irm
[0].Volatile
= false;
349 irm
[0].FailSPC
= true;
350 irm
[0].FailWithException
= true;
352 for ( int i
= 0; i
< 2; i
++ )
353 irm
[i
].UseSingle
= true;
355 /* Durable RM irm[2] does on SPC, so
356 * all volatile RMs get Rollback */
358 using ( TransactionScope scope
= new TransactionScope () ) {
365 catch ( TransactionAbortedException e
) {
366 Assert
.IsNotNull ( e
.InnerException
, "Expected e.InnerException == NotSupportedException, but got None");
367 Assert
.AreEqual ( typeof ( NotSupportedException
), e
.InnerException
.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e
.GetType () );
369 irm
[0].Check ( 1, 0, 0, 0, 0, "irm [0]" );
370 irm
[1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
374 Assert
.Fail ( "Expected TransactionAbortedException" );
378 public void Vol2_Dur1_Fail5 ()
380 CommittableTransaction ct
= new CommittableTransaction ();
381 IntResourceManager
[] irm
= new IntResourceManager
[2];
382 irm
[0] = new IntResourceManager ( 1 );
383 irm
[1] = new IntResourceManager ( 3 );
385 Transaction
.Current
= ct
;
386 irm
[0].Volatile
= false;
387 irm
[0].FailSPC
= true;
388 irm
[0].FailWithException
= true;
390 for ( int i
= 0; i
< 2; i
++ )
391 irm
[i
].UseSingle
= true;
393 /* Durable RM irm[2] does on SPC, so
394 * all volatile RMs get Rollback */
396 using ( TransactionScope scope
= new TransactionScope () ) {
406 catch ( TransactionAbortedException e
) {
407 Assert
.IsNotNull ( e
.InnerException
, "Expected e.InnerException == NotSupportedException, but got None" );
408 Assert
.AreEqual ( typeof ( NotSupportedException
), e
.InnerException
.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e
.GetType () );
410 irm
[0].Check ( 1, 0, 0, 0, 0, "irm [0]" );
411 irm
[1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
415 catch (InvalidOperationException x
) {
416 Assert
.IsNull ( x
.InnerException
);
417 Transaction
.Current
= null;
420 Assert
.Fail ( "Should not be reached" );
423 Assert
.Fail ( "Expected TransactionAbortedException" );
430 * > 1 durable, On .net this becomes a distributed transaction
431 * We don't support this in mono yet.
434 [Category ("NotWorking")]
435 public void Vol0_Dur2 ()
437 IntResourceManager
[] irm
= new IntResourceManager
[2];
438 irm
[0] = new IntResourceManager ( 1 );
439 irm
[1] = new IntResourceManager ( 3 );
441 irm
[0].Volatile
= false;
442 irm
[1].Volatile
= false;
444 for ( int i
= 0; i
< 2; i
++ )
445 irm
[i
].UseSingle
= true;
447 using (TransactionScope scope
= new TransactionScope ()) {
456 public void TransactionDispose ()
458 CommittableTransaction ct
= new CommittableTransaction ();
459 IntResourceManager irm
= new IntResourceManager (1);
460 irm
.Volatile
= false;
463 irm
.Check (0, 0, 0, 0, "Dispose transaction");
467 public void TransactionDispose2 ()
469 CommittableTransaction ct
= new CommittableTransaction ();
470 IntResourceManager irm
= new IntResourceManager (1);
472 Transaction
.Current
= ct
;
478 Transaction
.Current
= null;
481 irm
.Check (0, 0, 1, 0, "Dispose transaction");
482 Assert
.AreEqual (1, irm
.Value
);
486 public void TransactionDispose3 ()
488 CommittableTransaction ct
= new CommittableTransaction ();
489 IntResourceManager irm
= new IntResourceManager (1);
492 Transaction
.Current
= ct
;
497 Transaction
.Current
= null;
500 irm
.Check (1, 1, 0, 0, "Dispose transaction");
501 Assert
.AreEqual (5, irm
.Value
);