2 // Unit tests for async methods of Transaction class
5 // Ankit Jain <JAnkit@novell.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
11 using System.Transactions;
12 using NUnit.Framework;
13 using System.Threading;
15 namespace MonoTests.System.Transactions {
18 public class AsyncTest {
23 delayedException = null;
27 Transaction.Current = null;
31 public void TearDown ()
33 Transaction.Current = null;
37 [ExpectedException ( typeof ( InvalidOperationException ) )]
38 public void AsyncFail1 ()
40 IntResourceManager irm = new IntResourceManager ( 1 );
42 CommittableTransaction ct = new CommittableTransaction ();
44 Transaction.Current = ct;
49 IAsyncResult ar = ct.BeginCommit ( null, null );
50 IAsyncResult ar2 = ct.BeginCommit ( null, null );
55 [ExpectedException (typeof (TransactionAbortedException))]
56 public void AsyncFail2 ()
58 IntResourceManager irm = new IntResourceManager ( 1 );
60 CommittableTransaction ct = new CommittableTransaction ();
62 Transaction.Current = ct;
66 irm.FailPrepare = true;
68 IAsyncResult ar = ct.BeginCommit ( null, null );
73 AsyncCallback callback = null;
75 /* Callback called ? */
76 static bool called = false;
77 static ManualResetEvent mr = new ManualResetEvent ( false );
78 static Exception delayedException;
80 static void CommitCallback (IAsyncResult ar)
83 CommittableTransaction ct = ar as CommittableTransaction;
85 state = ( int ) ar.AsyncState;
87 } catch ( Exception e ) {
95 public void AsyncFail3 ()
97 delayedException = null;
98 IntResourceManager irm = new IntResourceManager ( 1 );
100 CommittableTransaction ct = new CommittableTransaction ();
102 Transaction.Current = ct;
106 irm.FailPrepare = true;
108 callback = new AsyncCallback (CommitCallback);
109 IAsyncResult ar = ct.BeginCommit ( callback, 5 );
110 mr.WaitOne (new TimeSpan (0, 0, 60), true);
112 Assert.IsTrue ( called, "callback not called" );
113 Assert.AreEqual ( 5, state, "state not preserved" );
115 if ( delayedException.GetType () != typeof ( TransactionAbortedException ) )
116 Assert.Fail ( "Expected TransactionAbortedException, got {0}", delayedException.GetType () );
120 public void Async1 ()
122 IntResourceManager irm = new IntResourceManager ( 1 );
124 CommittableTransaction ct = new CommittableTransaction ();
126 Transaction.Current = ct;
130 callback = new AsyncCallback (CommitCallback);
131 IAsyncResult ar = ct.BeginCommit ( callback, 5);
132 mr.WaitOne (new TimeSpan (0, 2, 0), true);
134 Assert.IsTrue (called, "callback not called" );
135 Assert.AreEqual ( 5, state, "State not received back");
137 if ( delayedException != null )
138 throw new Exception ("", delayedException );
142 public void Async2 ()
144 IntResourceManager irm = new IntResourceManager ( 1 );
146 CommittableTransaction ct = new CommittableTransaction ();
148 using ( TransactionScope scope = new TransactionScope (ct) ) {
153 IAsyncResult ar = ct.BeginCommit ( null, null);
157 catch ( TransactionAbortedException) {
158 irm.Check ( 0, 0, 1, 0, "irm" );
162 Assert.Fail ( "EndCommit should've thrown an exception" );
166 public void Async3 ()
168 IntResourceManager irm = new IntResourceManager ( 1 );
170 CommittableTransaction ct = new CommittableTransaction ();
172 Transaction.Current = ct;
177 IAsyncResult ar = ct.BeginCommit ( null, null );
180 irm.Check ( 1, 1, 0, 0, "irm" );
184 public void Async4 ()
186 IntResourceManager irm = new IntResourceManager ( 1 );
188 CommittableTransaction ct = new CommittableTransaction ();
190 Transaction.Current = ct;
195 IAsyncResult ar = ct.BeginCommit ( null, null );
196 ar.AsyncWaitHandle.WaitOne ();
197 Assert.IsTrue ( ar.IsCompleted );
199 irm.Check ( 1, 1, 0, 0, "irm" );
203 public void Async5 ()
205 IntResourceManager irm = new IntResourceManager ( 1 );
207 CommittableTransaction ct = new CommittableTransaction ();
209 Transaction.Current = ct;
213 irm.FailPrepare = true;
215 IAsyncResult ar = ct.BeginCommit ( null, null );
216 ar.AsyncWaitHandle.WaitOne ();
217 Assert.IsTrue ( ar.IsCompleted );
219 CommittableTransaction ctx = ar as CommittableTransaction;
220 ctx.EndCommit ( ar );
221 } catch ( TransactionAbortedException ) {
222 irm.Check ( 1, 0, 0, 0, "irm" );
226 Assert.Fail ("EndCommit should've failed");