2009-10-19 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Transactions / Test / EnlistTest.cs
blob1342f2f7c072c155e63a4acfeafffe980305a5bc
1 //
2 // Tests for combination of volatile & durable resource manangers
3 //
4 // Author:
5 // Ankit Jain <JAnkit@novell.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
10 using System;
11 using System.Transactions;
12 using NUnit.Framework;
14 namespace MonoTests.System.Transactions {
16 [TestFixture]
17 public class EnlistTest {
19 #region Vol1_Dur0
21 /* Single volatile resource, SPC happens */
22 [Test]
23 public void Vol1_Dur0 ()
25 IntResourceManager irm = new IntResourceManager (1);
26 irm.UseSingle = true;
27 using (TransactionScope scope = new TransactionScope ()) {
28 irm.Value = 2;
30 scope.Complete ();
32 irm.CheckSPC ("irm");
35 [Test]
36 public void Vol1_Dur0_2PC ()
38 IntResourceManager irm = new IntResourceManager (1);
40 using (TransactionScope scope = new TransactionScope ()) {
41 irm.Value = 2;
43 scope.Complete ();
45 irm.Check2PC ("irm");
48 /* Single volatile resource, SPC happens */
49 [Test]
50 public void Vol1_Dur0_Fail1 ()
52 IntResourceManager irm = new IntResourceManager (1);
53 irm.UseSingle = true;
54 using (TransactionScope scope = new TransactionScope ()) {
55 irm.Value = 2;
57 /* Not completing this..
58 scope.Complete ();*/
61 irm.Check ( 0, 0, 0, 1, 0, "irm" );
64 [Test]
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 ()) {
73 irm.Value = 2;
75 scope.Complete ();
79 [Test]
80 [ExpectedException ( typeof ( TransactionAbortedException ) )]
81 public void Vol1_Dur0_Fail3 ()
83 IntResourceManager irm = new IntResourceManager (1);
84 irm.UseSingle = true;
85 irm.FailSPC = true;
87 using (TransactionScope scope = new TransactionScope ()) {
88 irm.Value = 2;
90 scope.Complete ();
94 #endregion
96 #region Vol2_Dur0
98 /* >1 volatile, 2PC */
99 [Test]
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 ()) {
108 irm.Value = 2;
109 irm2.Value = 6;
111 scope.Complete ();
113 irm.Check2PC ( "irm" );
114 irm2.Check2PC ( "irm2" );
117 #endregion
119 #region Vol0_Dur1
120 /* 1 durable */
121 [Test]
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 ()) {
129 irm.Value = 2;
131 scope.Complete ();
134 irm.CheckSPC ( "irm" );
137 /* We support only 1 durable with 2PC
138 * On .net, this becomes a distributed transaction
140 [Test]
141 [Category ("NotWorking")]
142 public void Vol0_Dur1_2PC ()
144 IntResourceManager irm = new IntResourceManager (1);
146 /* Durable resource enlisted with a IEnlistedNotification
147 * object
149 irm.Volatile = false;
151 using (TransactionScope scope = new TransactionScope ()) {
152 irm.Value = 2;
154 scope.Complete ();
158 [Test]
159 public void Vol0_Dur1_Fail ()
161 IntResourceManager irm = new IntResourceManager ( 1 );
163 /* Durable resource enlisted with a IEnlistedNotification
164 * object
166 irm.Volatile = false;
167 irm.FailSPC = true;
168 irm.UseSingle = true;
169 try {
170 using (TransactionScope scope = new TransactionScope ()) {
171 irm.Value = 2;
173 scope.Complete ();
176 catch (TransactionAbortedException) {
177 irm.Check ( 1, 0, 0, 0, 0, "irm" );
178 return;
181 Assert.Fail ();
183 #endregion
185 #region Vol2_Dur1
186 /* >1vol + 1 durable */
187 [Test]
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 ()) {
201 irm [0].Value = 2;
202 irm [1].Value = 6;
203 irm [2].Value = 10;
204 irm [3].Value = 14;
206 scope.Complete ();
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 + "]" );
216 /* >1vol + 1 durable
217 * Durable fails SPC
219 [Test]
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 */
236 try {
237 using (TransactionScope scope = new TransactionScope ()) {
238 irm [0].Value = 2;
239 irm [1].Value = 6;
240 irm [2].Value = 10;
241 irm [3].Value = 14;
243 scope.Complete ();
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 + "]" );
254 /* >1vol + 1 durable
255 * durable doesn't complete SPC
257 [Test]
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 */
275 try {
276 using (TransactionScope scope = new TransactionScope ( TransactionScopeOption.Required, new TimeSpan ( 0, 0, 5 ) )) {
277 irm [0].Value = 2;
278 irm [1].Value = 6;
279 irm [2].Value = 10;
280 irm [3].Value = 14;
282 scope.Complete ();
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 + "]" );
294 /* >1vol + 1 durable
295 * Volatile fails Prepare
297 [Test]
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 */
314 try {
315 using (TransactionScope scope = new TransactionScope ()) {
316 irm [0].Value = 2;
317 irm [1].Value = 6;
318 irm [2].Value = 10;
319 irm [3].Value = 14;
321 scope.Complete ();
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]" );
335 return;
338 Assert.Fail ( "Expected TransactionAbortedException" );
341 [Test]
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 */
357 try {
358 using ( TransactionScope scope = new TransactionScope () ) {
359 irm [0].Value = 2;
360 irm [1].Value = 6;
362 scope.Complete ();
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]" );
371 return;
374 Assert.Fail ( "Expected TransactionAbortedException" );
377 [Test]
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 () ) {
397 irm [0].Value = 2;
398 irm [1].Value = 6;
400 scope.Complete ();
403 try {
404 ct.Commit ();
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]" );
412 try {
413 ct.Commit ();
415 catch (InvalidOperationException x ) {
416 Assert.IsNull ( x.InnerException);
417 Transaction.Current = null;
418 return;
420 Assert.Fail ( "Should not be reached" );
423 Assert.Fail ( "Expected TransactionAbortedException" );
426 #endregion
428 #region Others
429 /* >1vol
430 * > 1 durable, On .net this becomes a distributed transaction
431 * We don't support this in mono yet.
433 [Test]
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 ()) {
448 irm [0].Value = 2;
449 irm [1].Value = 6;
451 scope.Complete ();
455 [Test]
456 public void TransactionDispose ()
458 CommittableTransaction ct = new CommittableTransaction ();
459 IntResourceManager irm = new IntResourceManager (1);
460 irm.Volatile = false;
462 ct.Dispose ();
463 irm.Check (0, 0, 0, 0, "Dispose transaction");
466 [Test]
467 public void TransactionDispose2 ()
469 CommittableTransaction ct = new CommittableTransaction ();
470 IntResourceManager irm = new IntResourceManager (1);
472 Transaction.Current = ct;
473 irm.Value = 5;
475 try {
476 ct.Dispose ();
477 } finally {
478 Transaction.Current = null;
481 irm.Check (0, 0, 1, 0, "Dispose transaction");
482 Assert.AreEqual (1, irm.Value);
485 [Test]
486 public void TransactionDispose3 ()
488 CommittableTransaction ct = new CommittableTransaction ();
489 IntResourceManager irm = new IntResourceManager (1);
491 try {
492 Transaction.Current = ct;
493 irm.Value = 5;
494 ct.Commit ();
495 ct.Dispose ();
496 } finally {
497 Transaction.Current = null;
500 irm.Check (1, 1, 0, 0, "Dispose transaction");
501 Assert.AreEqual (5, irm.Value);
504 #endregion