Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,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");
}
}
}

View File

@@ -0,0 +1,15 @@
2009-01-07 Zoltan Varga <vargaz@gmail.com>
* AsyncTest.cs: Disable these tests, they fail randomly (bug #463999).
2007-08-14 Ankit Jain <jankit@novell.com>
* EnlistTest.cs: Add tests for bug #82423.
2006-03-03 Ankit Jain <jankit@novell.com>
* AsyncTest.cs:
* TransactionScopeTest.cs:
* EnlistTest.cs: Initial set of test cases.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,245 @@
//
// ResourceManager representing an integer, used by other test cases
//
// Author:
// Ankit Jain <JAnkit@novell.com>
//
// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using NUnit.Framework;
namespace MonoTests.System.Transactions
{
public class IntResourceManager
{
public IntResourceManager (int value)
{
actual = value;
guid = Guid.NewGuid ();
}
private int actual;
private int tmpValue;
private Transaction transaction = null;
public int NumPrepare = 0;
public int NumRollback = 0;
public int NumCommit = 0;
public int NumInDoubt = 0;
public int NumSingle = 0;
public int NumInitialize = 0;
public int NumPromote = 0;
public int NumEnlistFailed = 0;
public ResourceManagerType Type = ResourceManagerType.Volatile;
public bool FailPrepare = false;
public bool FailWithException = false;
public bool IgnorePrepare = false;
public bool IgnoreSPC = false;
public bool FailSPC = false;
public bool FailCommit = false;
public bool FailRollback = false;
public bool UseSingle = false;
public Exception ThrowThisException = null;
Guid guid;
public int Actual {
get { return actual; }
}
public int Value {
get { return transaction == null ? actual : tmpValue; }
set
{
if (Transaction.Current == null) {
/* Not in a transaction */
actual = value;
return;
}
/* FIXME: Do what in this case? */
if (transaction != null)
Console.WriteLine ("WARNING: Setting value more than once");
if (transaction != Transaction.Current) {
transaction = Transaction.Current;
if ( Type == ResourceManagerType.Promotable ) {
transaction.EnlistPromotableSinglePhase(new PromotableSinglePhaseNotification ( this ));
} else if (UseSingle) {
SinglePhaseNotification enlistment = new SinglePhaseNotification ( this );
if ( Type == ResourceManagerType.Volatile )
transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
else
transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
} else {
EnlistmentNotification enlistment = new EnlistmentNotification ( this );
if ( Type == ResourceManagerType.Volatile )
transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
else
transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
}
}
tmpValue = value;
}
}
public void Commit ()
{
actual = tmpValue;
transaction = null;
}
public void Rollback ()
{
transaction = null;
}
public void CheckSPC ( string msg )
{
Check ( 1, 0, 0, 0, 0, 0, 0, msg );
}
public void Check2PC ( string msg)
{
Check ( 0, 1, 1, 0, 0, 0, 0, msg );
}
public void Check ( int s, int p, int c, int r, int d, int i, int pr, string msg )
{
Assert.AreEqual ( s, NumSingle, msg + ": NumSingle" );
Assert.AreEqual ( p, NumPrepare, msg + ": NumPrepare" );
Assert.AreEqual ( c, NumCommit, msg + ": NumCommit" );
Assert.AreEqual ( r, NumRollback, msg + ": NumRollback" );
Assert.AreEqual ( d, NumInDoubt, msg + ": NumInDoubt" );
Assert.AreEqual ( i, NumInitialize, msg + ": NumInitialize" );
Assert.AreEqual ( pr, NumPromote, msg + ": NumPromote" );
}
/* Used for volatile RMs */
public void Check ( int p, int c, int r, int d, string msg )
{
Check ( 0, p, c, r, d, 0, 0, msg );
}
}
public class EnlistmentNotification : IEnlistmentNotification {
protected IntResourceManager resource;
public EnlistmentNotification ( IntResourceManager resource )
{
this.resource = resource;
}
public void Prepare ( PreparingEnlistment preparingEnlistment )
{
resource.NumPrepare++;
if ( resource.IgnorePrepare )
return;
if ( resource.FailPrepare ) {
if (resource.FailWithException)
preparingEnlistment.ForceRollback ( resource.ThrowThisException ?? new NotSupportedException () );
else
preparingEnlistment.ForceRollback ();
} else {
preparingEnlistment.Prepared ();
}
}
public void Commit ( Enlistment enlistment )
{
resource.NumCommit++;
if (resource.FailCommit)
{
if (resource.FailWithException)
throw (resource.ThrowThisException ?? new NotSupportedException());
else
return;
}
resource.Commit ();
enlistment.Done ();
}
public void Rollback ( Enlistment enlistment )
{
resource.NumRollback++;
if (resource.FailRollback)
{
if (resource.FailWithException)
throw (resource.ThrowThisException ?? new NotSupportedException());
else
return;
}
resource.Rollback ();
}
public void InDoubt ( Enlistment enlistment )
{
resource.NumInDoubt++;
throw new Exception ( "IntResourceManager.InDoubt is not implemented." );
}
}
public class SinglePhaseNotification : EnlistmentNotification, ISinglePhaseNotification
{
public SinglePhaseNotification ( IntResourceManager resource )
: base ( resource )
{
}
public void SinglePhaseCommit ( SinglePhaseEnlistment enlistment )
{
resource.NumSingle++;
if ( resource.IgnoreSPC )
return;
if ( resource.FailSPC ) {
if ( resource.FailWithException )
enlistment.Aborted ( resource.ThrowThisException ?? new NotSupportedException () );
else
enlistment.Aborted ();
}
else {
resource.Commit ();
enlistment.Committed ();
}
}
}
public class PromotableSinglePhaseNotification : SinglePhaseNotification, IPromotableSinglePhaseNotification
{
public PromotableSinglePhaseNotification ( IntResourceManager resource )
: base( resource )
{
}
public void Initialize ()
{
resource.NumInitialize++;
}
public void Rollback ( SinglePhaseEnlistment enlistment )
{
resource.NumRollback++;
resource.Rollback ();
}
public byte [] Promote ()
{
resource.NumPromote++;
return new byte[0];
}
}
public enum ResourceManagerType { Volatile, Durable, Promotable };
}

File diff suppressed because it is too large Load Diff