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 bool FailPrepare
= false;
37 public bool FailWithException
= false;
38 public bool IgnorePrepare
= false;
40 public bool Volatile
= true;
41 public bool IgnoreSPC
= false;
42 public bool FailSPC
= false;
43 public bool FailCommit
= false;
44 public bool UseSingle
= false;
49 get { return actual; }
53 get { return transaction == null ? actual : tmpValue; }
56 if (Transaction
.Current
== null) {
57 /* Not in a transaction */
61 /* FIXME: Do what in this case? */
62 if (transaction
!= null)
63 Console
.WriteLine ("WARNING: Setting value more than once");
65 if (transaction
!= Transaction
.Current
) {
66 transaction
= Transaction
.Current
;
69 SinglePhaseNotification enlistment
= new SinglePhaseNotification ( this );
71 transaction
.EnlistVolatile ( enlistment
, EnlistmentOptions
.None
);
73 transaction
.EnlistDurable ( guid
, enlistment
, EnlistmentOptions
.None
);
75 EnlistmentNotification enlistment
= new EnlistmentNotification ( this );
77 transaction
.EnlistVolatile ( enlistment
, EnlistmentOptions
.None
);
79 transaction
.EnlistDurable ( guid
, enlistment
, EnlistmentOptions
.None
);
92 public void Rollback ()
97 public void CheckSPC ( string msg
)
99 Check ( 1, 0, 0, 0, 0, msg
);
102 public void Check2PC ( string msg
)
104 Check ( 0, 1, 1, 0, 0, msg
);
107 public void Check ( int s
, int p
, int c
, int r
, int d
, string msg
)
109 Assert
.AreEqual ( s
, NumSingle
, msg
+ ": NumSingle" );
110 Assert
.AreEqual ( p
, NumPrepare
, msg
+ ": NumPrepare" );
111 Assert
.AreEqual ( c
, NumCommit
, msg
+ ": NumCommit" );
112 Assert
.AreEqual ( r
, NumRollback
, msg
+ ": NumRollback" );
113 Assert
.AreEqual ( d
, NumInDoubt
, msg
+ ": NumInDoubt" );
116 /* Used for volatile RMs */
117 public void Check ( int p
, int c
, int r
, int d
, string msg
)
119 Check ( 0, p
, c
, r
, d
, msg
);
123 public class EnlistmentNotification
: IEnlistmentNotification
{
124 protected IntResourceManager resource
;
126 public EnlistmentNotification ( IntResourceManager resource
)
128 this.resource
= resource
;
131 public void Prepare ( PreparingEnlistment preparingEnlistment
)
133 resource
.NumPrepare
++;
134 if ( resource
.IgnorePrepare
)
137 if ( resource
.FailPrepare
) {
138 if (resource
.FailWithException
)
139 preparingEnlistment
.ForceRollback ( new NotSupportedException () );
141 preparingEnlistment
.ForceRollback ();
143 preparingEnlistment
.Prepared ();
147 public void Commit ( Enlistment enlistment
)
149 resource
.NumCommit
++;
150 if ( resource
.FailCommit
)
157 public void Rollback ( Enlistment enlistment
)
159 resource
.NumRollback
++;
160 resource
.Rollback ();
163 public void InDoubt ( Enlistment enlistment
)
165 resource
.NumInDoubt
++;
166 throw new Exception ( "IntResourceManager.InDoubt is not implemented." );
171 public class SinglePhaseNotification
: EnlistmentNotification
, ISinglePhaseNotification
173 public SinglePhaseNotification ( IntResourceManager resource
)
178 public void SinglePhaseCommit ( SinglePhaseEnlistment enlistment
)
180 resource
.NumSingle
++;
181 if ( resource
.IgnoreSPC
)
184 if ( resource
.FailSPC
) {
185 if ( resource
.FailWithException
)
186 enlistment
.Aborted ( new NotSupportedException () );
188 enlistment
.Aborted ();
192 enlistment
.Committed ();