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