IntResourceManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // ResourceManager representing an integer, used by other test cases
  3. //
  4. // Author:
  5. // Ankit Jain <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Transactions;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Transactions
  15. {
  16. public class IntResourceManager
  17. {
  18. public IntResourceManager (int value)
  19. {
  20. actual = value;
  21. guid = Guid.NewGuid ();
  22. }
  23. private int actual;
  24. private int tmpValue;
  25. private Transaction transaction = null;
  26. public int NumPrepare = 0;
  27. public int NumRollback = 0;
  28. public int NumCommit = 0;
  29. public int NumInDoubt = 0;
  30. public int NumSingle = 0;
  31. public bool FailPrepare = false;
  32. public bool FailWithException = false;
  33. public bool IgnorePrepare = false;
  34. public bool Volatile = true;
  35. public bool IgnoreSPC = false;
  36. public bool FailSPC = false;
  37. public bool FailCommit = false;
  38. public bool UseSingle = false;
  39. Guid guid;
  40. public int Actual {
  41. get { return actual; }
  42. }
  43. public int Value {
  44. get { return transaction == null ? actual : tmpValue; }
  45. set
  46. {
  47. if (Transaction.Current == null) {
  48. /* Not in a transaction */
  49. actual = value;
  50. return;
  51. }
  52. /* FIXME: Do what in this case? */
  53. if (transaction != null)
  54. Console.WriteLine ("WARNING: Setting value more than once");
  55. if (transaction != Transaction.Current) {
  56. transaction = Transaction.Current;
  57. if (UseSingle) {
  58. SinglePhaseNotification enlistment = new SinglePhaseNotification ( this );
  59. if ( Volatile )
  60. transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
  61. else
  62. transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
  63. } else {
  64. EnlistmentNotification enlistment = new EnlistmentNotification ( this );
  65. if ( Volatile )
  66. transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
  67. else
  68. transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
  69. }
  70. }
  71. tmpValue = value;
  72. }
  73. }
  74. public void Commit ()
  75. {
  76. actual = tmpValue;
  77. transaction = null;
  78. }
  79. public void Rollback ()
  80. {
  81. transaction = null;
  82. }
  83. public void CheckSPC ( string msg )
  84. {
  85. Check ( 1, 0, 0, 0, 0, msg );
  86. }
  87. public void Check2PC ( string msg)
  88. {
  89. Check ( 0, 1, 1, 0, 0, msg );
  90. }
  91. public void Check ( int s, int p, int c, int r, int d, string msg )
  92. {
  93. Assert.AreEqual ( s, NumSingle, msg + ": NumSingle" );
  94. Assert.AreEqual ( p, NumPrepare, msg + ": NumPrepare" );
  95. Assert.AreEqual ( c, NumCommit, msg + ": NumCommit" );
  96. Assert.AreEqual ( r, NumRollback, msg + ": NumRollback" );
  97. Assert.AreEqual ( d, NumInDoubt, msg + ": NumInDoubt" );
  98. }
  99. /* Used for volatile RMs */
  100. public void Check ( int p, int c, int r, int d, string msg )
  101. {
  102. Check ( 0, p, c, r, d, msg );
  103. }
  104. }
  105. public class EnlistmentNotification : IEnlistmentNotification {
  106. protected IntResourceManager resource;
  107. public EnlistmentNotification ( IntResourceManager resource )
  108. {
  109. this.resource = resource;
  110. }
  111. public void Prepare ( PreparingEnlistment preparingEnlistment )
  112. {
  113. resource.NumPrepare++;
  114. if ( resource.IgnorePrepare )
  115. return;
  116. if ( resource.FailPrepare ) {
  117. if (resource.FailWithException)
  118. preparingEnlistment.ForceRollback ( new NotSupportedException () );
  119. else
  120. preparingEnlistment.ForceRollback ();
  121. } else {
  122. preparingEnlistment.Prepared ();
  123. }
  124. }
  125. public void Commit ( Enlistment enlistment )
  126. {
  127. resource.NumCommit++;
  128. if ( resource.FailCommit )
  129. return;
  130. resource.Commit ();
  131. enlistment.Done ();
  132. }
  133. public void Rollback ( Enlistment enlistment )
  134. {
  135. resource.NumRollback++;
  136. resource.Rollback ();
  137. }
  138. public void InDoubt ( Enlistment enlistment )
  139. {
  140. resource.NumInDoubt++;
  141. throw new Exception ( "IntResourceManager.InDoubt is not implemented." );
  142. }
  143. }
  144. public class SinglePhaseNotification : EnlistmentNotification, ISinglePhaseNotification
  145. {
  146. public SinglePhaseNotification ( IntResourceManager resource )
  147. : base ( resource )
  148. {
  149. }
  150. public void SinglePhaseCommit ( SinglePhaseEnlistment enlistment )
  151. {
  152. resource.NumSingle++;
  153. if ( resource.IgnoreSPC )
  154. return;
  155. if ( resource.FailSPC ) {
  156. if ( resource.FailWithException )
  157. enlistment.Aborted ( new NotSupportedException () );
  158. else
  159. enlistment.Aborted ();
  160. }
  161. else {
  162. resource.Commit ();
  163. enlistment.Committed ();
  164. }
  165. }
  166. }
  167. }