merge -r 60814:60815
[mono.git] / mcs / class / System.Transactions / Test / AsyncTest.cs
1 //
2 // Unit tests for async methods of Transaction class
3 //
4 // Author:
5 //      Ankit Jain      <JAnkit@novell.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.Transactions;
12 using NUnit.Framework;
13 using System.Threading;
14
15 namespace  MonoTests.System.Transactions {
16
17         [TestFixture]
18         public class AsyncTest {
19
20                 [SetUp]
21                 public void Setup ()
22                 {
23                         delayedException = null;
24                         called = false;
25                         mr.Reset ();
26                         state = 0;
27                         Transaction.Current = null;
28                 }
29
30                 [TearDown]
31                 public void TearDown ()
32                 {
33                         Transaction.Current = null;
34                 }
35
36                 [Test]
37                 [ExpectedException ( typeof ( InvalidOperationException ) )]
38                 public void AsyncFail1 ()
39                 {
40                         IntResourceManager irm = new IntResourceManager ( 1 );
41
42                         CommittableTransaction ct = new CommittableTransaction ();
43                         /* Set ambient Tx */
44                         Transaction.Current = ct;
45
46                         /* Enlist */
47                         irm.Value = 2;
48
49                         IAsyncResult ar = ct.BeginCommit ( null, null );
50                         IAsyncResult ar2 = ct.BeginCommit ( null, null );
51                 }
52
53
54                 [Test]
55                 [ExpectedException (typeof (TransactionAbortedException))]
56                 public void AsyncFail2 ()
57                 {
58                         IntResourceManager irm = new IntResourceManager ( 1 );
59
60                         CommittableTransaction ct = new CommittableTransaction ();
61                         /* Set ambient Tx */
62                         Transaction.Current = ct;
63
64                         /* Enlist */
65                         irm.Value = 2;
66                         irm.FailPrepare = true;
67
68                         IAsyncResult ar = ct.BeginCommit ( null, null );
69
70                         ct.EndCommit ( ar );
71                 }
72
73                 AsyncCallback callback = null;
74                 static int state = 0;
75                 /* Callback called ? */
76                 static bool called = false;
77                 static ManualResetEvent mr = new ManualResetEvent ( false );
78                 static Exception delayedException;
79
80                 static void CommitCallback (IAsyncResult ar)
81                 {
82                         called = true;
83                         CommittableTransaction ct = ar as CommittableTransaction;
84                         try {
85                                 state = ( int ) ar.AsyncState;
86                                 ct.EndCommit ( ar );
87                         } catch ( Exception e ) {
88                                 delayedException = e;
89                         } finally {
90                                 mr.Set ();
91                         }
92                 }
93
94                 [Test]
95                 public void AsyncFail3 ()
96                 {
97                         delayedException = null;
98                         IntResourceManager irm = new IntResourceManager ( 1 );
99
100                         CommittableTransaction ct = new CommittableTransaction ();
101                         /* Set ambient Tx */
102                         Transaction.Current = ct;
103                         
104                         /* Enlist */
105                         irm.Value = 2;
106                         irm.FailPrepare = true;
107
108                         callback = new AsyncCallback (CommitCallback);
109                         IAsyncResult ar = ct.BeginCommit ( callback, 5 );
110                         mr.WaitOne (new TimeSpan (0, 0, 60), true);
111
112                         Assert.IsTrue ( called, "callback not called" );
113                         Assert.AreEqual ( 5, state, "state not preserved" );
114
115                         if ( delayedException.GetType () != typeof ( TransactionAbortedException ) )
116                                 Assert.Fail ( "Expected TransactionAbortedException, got {0}", delayedException.GetType () );
117                 }
118
119                 [Test]
120                 public void Async1 ()
121                 {
122                         IntResourceManager irm = new IntResourceManager ( 1 );
123
124                         CommittableTransaction ct = new CommittableTransaction ();
125                         /* Set ambient Tx */
126                         Transaction.Current = ct;
127                         /* Enlist */
128                         irm.Value = 2;
129
130                         callback = new AsyncCallback (CommitCallback);
131                         IAsyncResult ar = ct.BeginCommit ( callback, 5);
132                         mr.WaitOne (new TimeSpan (0, 2, 0), true);
133
134                         Assert.IsTrue (called, "callback not called" );
135                         Assert.AreEqual ( 5, state, "State not received back");
136
137                         if ( delayedException != null )
138                                 throw new Exception ("", delayedException );
139                 }
140
141                 [Test]
142                 public void Async2 ()
143                 {
144                         IntResourceManager irm = new IntResourceManager ( 1 );
145
146                         CommittableTransaction ct = new CommittableTransaction ();
147
148                         using ( TransactionScope scope = new TransactionScope (ct) ) {
149                                 irm.Value = 2;
150
151                                 //scope.Complete ();
152
153                                 IAsyncResult ar = ct.BeginCommit ( null, null);
154                                 try {
155                                         ct.EndCommit ( ar );
156                                 }
157                                 catch ( TransactionAbortedException) {
158                                         irm.Check ( 0, 0, 1, 0, "irm" );
159                                         return;
160                                 }
161                         }
162                         Assert.Fail ( "EndCommit should've thrown an exception" );
163                 }
164
165                 [Test]
166                 public void Async3 ()
167                 {
168                         IntResourceManager irm = new IntResourceManager ( 1 );
169
170                         CommittableTransaction ct = new CommittableTransaction ();
171                         /* Set ambient Tx */
172                         Transaction.Current = ct;
173
174                         /* Enlist */
175                         irm.Value = 2;
176
177                         IAsyncResult ar = ct.BeginCommit ( null, null );
178                         ct.EndCommit ( ar );
179
180                         irm.Check ( 1, 1, 0, 0, "irm" );
181                 }
182
183                 [Test]
184                 public void Async4 ()
185                 {
186                         IntResourceManager irm = new IntResourceManager ( 1 );
187
188                         CommittableTransaction ct = new CommittableTransaction ();
189                         /* Set ambient Tx */
190                         Transaction.Current = ct;
191
192                         /* Enlist */
193                         irm.Value = 2;
194
195                         IAsyncResult ar = ct.BeginCommit ( null, null );
196                         ar.AsyncWaitHandle.WaitOne ();
197                         Assert.IsTrue ( ar.IsCompleted );
198
199                         irm.Check ( 1, 1, 0, 0, "irm" );
200                 }
201
202                 [Test]
203                 public void Async5 ()
204                 {
205                         IntResourceManager irm = new IntResourceManager ( 1 );
206
207                         CommittableTransaction ct = new CommittableTransaction ();
208                         /* Set ambient Tx */
209                         Transaction.Current = ct;
210
211                         /* Enlist */
212                         irm.Value = 2;
213                         irm.FailPrepare = true;
214
215                         IAsyncResult ar = ct.BeginCommit ( null, null );
216                         ar.AsyncWaitHandle.WaitOne ();
217                         Assert.IsTrue ( ar.IsCompleted );
218                         try {
219                                 CommittableTransaction ctx = ar as CommittableTransaction;
220                                 ctx.EndCommit ( ar );
221                         } catch ( TransactionAbortedException ) {
222                                 irm.Check ( 1, 0, 0, 0, "irm" );
223                                 return;
224                         }
225
226                         Assert.Fail ("EndCommit should've failed");
227                 }
228         }
229 }
230