AsyncTest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Unit tests for async methods of Transaction class
  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.Transactions;
  11. using NUnit.Framework;
  12. using System.Threading;
  13. namespace MonoTests.System.Transactions {
  14. [TestFixture]
  15. public class AsyncTest {
  16. [SetUp]
  17. public void Setup ()
  18. {
  19. delayedException = null;
  20. called = false;
  21. mr.Reset ();
  22. state = 0;
  23. Transaction.Current = null;
  24. }
  25. [TearDown]
  26. public void TearDown ()
  27. {
  28. Transaction.Current = null;
  29. }
  30. [Test]
  31. [ExpectedException ( typeof ( InvalidOperationException ) )]
  32. public void AsyncFail1 ()
  33. {
  34. IntResourceManager irm = new IntResourceManager ( 1 );
  35. CommittableTransaction ct = new CommittableTransaction ();
  36. /* Set ambient Tx */
  37. Transaction.Current = ct;
  38. /* Enlist */
  39. irm.Value = 2;
  40. IAsyncResult ar = ct.BeginCommit ( null, null );
  41. IAsyncResult ar2 = ct.BeginCommit ( null, null );
  42. }
  43. [Test]
  44. [ExpectedException (typeof (TransactionAbortedException))]
  45. public void AsyncFail2 ()
  46. {
  47. IntResourceManager irm = new IntResourceManager ( 1 );
  48. CommittableTransaction ct = new CommittableTransaction ();
  49. /* Set ambient Tx */
  50. Transaction.Current = ct;
  51. /* Enlist */
  52. irm.Value = 2;
  53. irm.FailPrepare = true;
  54. IAsyncResult ar = ct.BeginCommit ( null, null );
  55. ct.EndCommit ( ar );
  56. }
  57. AsyncCallback callback = null;
  58. static int state = 0;
  59. /* Callback called ? */
  60. static bool called = false;
  61. static ManualResetEvent mr = new ManualResetEvent ( false );
  62. static Exception delayedException;
  63. static void CommitCallback (IAsyncResult ar)
  64. {
  65. called = true;
  66. CommittableTransaction ct = ar as CommittableTransaction;
  67. try {
  68. state = ( int ) ar.AsyncState;
  69. ct.EndCommit ( ar );
  70. } catch ( Exception e ) {
  71. delayedException = e;
  72. } finally {
  73. mr.Set ();
  74. }
  75. }
  76. [Test]
  77. public void AsyncFail3 ()
  78. {
  79. delayedException = null;
  80. IntResourceManager irm = new IntResourceManager ( 1 );
  81. CommittableTransaction ct = new CommittableTransaction ();
  82. /* Set ambient Tx */
  83. Transaction.Current = ct;
  84. /* Enlist */
  85. irm.Value = 2;
  86. irm.FailPrepare = true;
  87. callback = new AsyncCallback (CommitCallback);
  88. IAsyncResult ar = ct.BeginCommit ( callback, 5 );
  89. mr.WaitOne (new TimeSpan (0, 0, 60), true);
  90. Assert.IsTrue ( called, "callback not called" );
  91. Assert.AreEqual ( 5, state, "state not preserved" );
  92. if ( delayedException.GetType () != typeof ( TransactionAbortedException ) )
  93. Assert.Fail ( "Expected TransactionAbortedException, got {0}", delayedException.GetType () );
  94. }
  95. [Test]
  96. public void Async1 ()
  97. {
  98. IntResourceManager irm = new IntResourceManager ( 1 );
  99. CommittableTransaction ct = new CommittableTransaction ();
  100. /* Set ambient Tx */
  101. Transaction.Current = ct;
  102. /* Enlist */
  103. irm.Value = 2;
  104. callback = new AsyncCallback (CommitCallback);
  105. IAsyncResult ar = ct.BeginCommit ( callback, 5);
  106. mr.WaitOne (new TimeSpan (0, 2, 0), true);
  107. Assert.IsTrue (called, "callback not called" );
  108. Assert.AreEqual ( 5, state, "State not received back");
  109. if ( delayedException != null )
  110. throw new Exception ("", delayedException );
  111. }
  112. [Test]
  113. public void Async2 ()
  114. {
  115. IntResourceManager irm = new IntResourceManager ( 1 );
  116. CommittableTransaction ct = new CommittableTransaction ();
  117. using ( TransactionScope scope = new TransactionScope (ct) ) {
  118. irm.Value = 2;
  119. //scope.Complete ();
  120. IAsyncResult ar = ct.BeginCommit ( null, null);
  121. try {
  122. ct.EndCommit ( ar );
  123. }
  124. catch ( TransactionAbortedException) {
  125. irm.Check ( 0, 0, 1, 0, "irm" );
  126. return;
  127. }
  128. }
  129. Assert.Fail ( "EndCommit should've thrown an exception" );
  130. }
  131. [Test]
  132. public void Async3 ()
  133. {
  134. IntResourceManager irm = new IntResourceManager ( 1 );
  135. CommittableTransaction ct = new CommittableTransaction ();
  136. /* Set ambient Tx */
  137. Transaction.Current = ct;
  138. /* Enlist */
  139. irm.Value = 2;
  140. IAsyncResult ar = ct.BeginCommit ( null, null );
  141. ct.EndCommit ( ar );
  142. irm.Check ( 1, 1, 0, 0, "irm" );
  143. }
  144. [Test]
  145. public void Async4 ()
  146. {
  147. IntResourceManager irm = new IntResourceManager ( 1 );
  148. CommittableTransaction ct = new CommittableTransaction ();
  149. /* Set ambient Tx */
  150. Transaction.Current = ct;
  151. /* Enlist */
  152. irm.Value = 2;
  153. IAsyncResult ar = ct.BeginCommit ( null, null );
  154. ar.AsyncWaitHandle.WaitOne ();
  155. Assert.IsTrue ( ar.IsCompleted );
  156. irm.Check ( 1, 1, 0, 0, "irm" );
  157. }
  158. [Test]
  159. public void Async5 ()
  160. {
  161. IntResourceManager irm = new IntResourceManager ( 1 );
  162. CommittableTransaction ct = new CommittableTransaction ();
  163. /* Set ambient Tx */
  164. Transaction.Current = ct;
  165. /* Enlist */
  166. irm.Value = 2;
  167. irm.FailPrepare = true;
  168. IAsyncResult ar = ct.BeginCommit ( null, null );
  169. ar.AsyncWaitHandle.WaitOne ();
  170. Assert.IsTrue ( ar.IsCompleted );
  171. try {
  172. CommittableTransaction ctx = ar as CommittableTransaction;
  173. ctx.EndCommit ( ar );
  174. } catch ( TransactionAbortedException ) {
  175. irm.Check ( 1, 0, 0, 0, "irm" );
  176. return;
  177. }
  178. Assert.Fail ("EndCommit should've failed");
  179. }
  180. }
  181. }