| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- //
- // Unit tests for async methods of Transaction class
- //
- // Author:
- // Ankit Jain <[email protected]>
- //
- // 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]
- 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");
- }
- }
- }
|