2 // ResourceManager representing an integer, used by other test cases
5 // Ankit Jain <JAnkit@novell.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
11 using System.Collections.Generic;
13 using System.Transactions;
14 using NUnit.Framework;
16 namespace MonoTests.System.Transactions
18 public class IntResourceManager
20 public IntResourceManager (int value)
23 guid = Guid.NewGuid ();
28 private Transaction transaction = null;
30 public int NumPrepare = 0;
31 public int NumRollback = 0;
32 public int NumCommit = 0;
33 public int NumInDoubt = 0;
34 public int NumSingle = 0;
36 public int NumInitialize = 0;
37 public int NumPromote = 0;
38 public int NumEnlistFailed = 0;
40 public ResourceManagerType Type = ResourceManagerType.Volatile;
41 public bool FailPrepare = false;
42 public bool FailWithException = false;
43 public bool IgnorePrepare = false;
44 public bool IgnoreSPC = false;
45 public bool FailSPC = false;
46 public bool FailCommit = false;
47 public bool UseSingle = false;
52 get { return actual; }
56 get { return transaction == null ? actual : tmpValue; }
59 if (Transaction.Current == null) {
60 /* Not in a transaction */
64 /* FIXME: Do what in this case? */
65 if (transaction != null)
66 Console.WriteLine ("WARNING: Setting value more than once");
68 if (transaction != Transaction.Current) {
69 transaction = Transaction.Current;
71 if ( Type == ResourceManagerType.Promotable ) {
72 transaction.EnlistPromotableSinglePhase(new PromotableSinglePhaseNotification ( this ));
73 } else if (UseSingle) {
74 SinglePhaseNotification enlistment = new SinglePhaseNotification ( this );
75 if ( Type == ResourceManagerType.Volatile )
76 transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
78 transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
80 EnlistmentNotification enlistment = new EnlistmentNotification ( this );
81 if ( Type == ResourceManagerType.Volatile )
82 transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
84 transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
97 public void Rollback ()
102 public void CheckSPC ( string msg )
104 Check ( 1, 0, 0, 0, 0, 0, 0, msg );
107 public void Check2PC ( string msg)
109 Check ( 0, 1, 1, 0, 0, 0, 0, msg );
112 public void Check ( int s, int p, int c, int r, int d, int i, int pr, string msg )
114 Assert.AreEqual ( s, NumSingle, msg + ": NumSingle" );
115 Assert.AreEqual ( p, NumPrepare, msg + ": NumPrepare" );
116 Assert.AreEqual ( c, NumCommit, msg + ": NumCommit" );
117 Assert.AreEqual ( r, NumRollback, msg + ": NumRollback" );
118 Assert.AreEqual ( d, NumInDoubt, msg + ": NumInDoubt" );
119 Assert.AreEqual ( i, NumInitialize, msg + ": NumRollback" );
120 Assert.AreEqual ( pr, NumPromote, msg + ": NumInDoubt" );
123 /* Used for volatile RMs */
124 public void Check ( int p, int c, int r, int d, string msg )
126 Check ( 0, p, c, r, d, 0, 0, msg );
130 public class EnlistmentNotification : IEnlistmentNotification {
131 protected IntResourceManager resource;
133 public EnlistmentNotification ( IntResourceManager resource )
135 this.resource = resource;
138 public void Prepare ( PreparingEnlistment preparingEnlistment )
140 resource.NumPrepare++;
141 if ( resource.IgnorePrepare )
144 if ( resource.FailPrepare ) {
145 if (resource.FailWithException)
146 preparingEnlistment.ForceRollback ( new NotSupportedException () );
148 preparingEnlistment.ForceRollback ();
150 preparingEnlistment.Prepared ();
154 public void Commit ( Enlistment enlistment )
156 resource.NumCommit++;
157 if ( resource.FailCommit )
164 public void Rollback ( Enlistment enlistment )
166 resource.NumRollback++;
167 resource.Rollback ();
170 public void InDoubt ( Enlistment enlistment )
172 resource.NumInDoubt++;
173 throw new Exception ( "IntResourceManager.InDoubt is not implemented." );
177 public class SinglePhaseNotification : EnlistmentNotification, ISinglePhaseNotification
179 public SinglePhaseNotification ( IntResourceManager resource )
184 public void SinglePhaseCommit ( SinglePhaseEnlistment enlistment )
186 resource.NumSingle++;
187 if ( resource.IgnoreSPC )
190 if ( resource.FailSPC ) {
191 if ( resource.FailWithException )
192 enlistment.Aborted ( new NotSupportedException () );
194 enlistment.Aborted ();
198 enlistment.Committed ();
203 public class PromotableSinglePhaseNotification : SinglePhaseNotification, IPromotableSinglePhaseNotification
205 public PromotableSinglePhaseNotification ( IntResourceManager resource )
210 public void Initialize ()
212 resource.NumInitialize++;
215 public void Rollback ( SinglePhaseEnlistment enlistment )
217 resource.NumRollback++;
218 resource.Rollback ();
221 public byte [] Promote ()
223 resource.NumPromote++;
228 public enum ResourceManagerType { Volatile, Durable, Promotable };