public static async Task SimpleTest() { const string TraceFileName = "Transactions.TransactionHandlerTest.SimpleTest.log.csv"; try { Trace.AutoFlush = true; Trace.Listeners.Add(new TextWriterTraceListener(new StreamWriter(TraceFileName, false))); using (var dataSource = new MockDataSource("ds", "tx")) using (var context = new TransactionContext(TransactionContextAffinity.RequiresNew)) { var command = new MockDataCommand(); await dataSource.ExecuteNonQuery(command).ConfigureAwait(false); context.VoteCommit(); } } finally { Trace.Close(); } CheckTraceLog(TraceFileName); }
public static void ValidStateTransitionTest_Entered_ToBeCommitted(TransactionContextAffinity affinity) { using (var context = new TransactionContext(affinity)) { context.VoteCommit(); Assert.AreEqual(TransactionContextState.ToBeCommitted, context.State); } }
public static void ValidStateTransitionTest_ToBeCommitted_ToBeCommitted(TransactionContextAffinity affinity) { using (var context = new TransactionContext(affinity)) { context.VoteCommit(); Assert.AreEqual(TransactionContextState.ToBeCommitted, context.State); context.StateChanged += (sender, e) => { if (e.NewState == TransactionContextState.ToBeCommitted) Assert.Fail("Changed event should not be raised again"); }; context.VoteCommit(); Assert.AreEqual(TransactionContextState.ToBeCommitted, context.State); } }
private static async Task ExecuteNode(TransactionContextTestNode node) { if (node.Parent == null) Assert.That(TransactionContext.CurrentTransactionContext, Is.Null); var tcs = new TaskCompletionSource<TransactionContextState>(); using (var tx = new TransactionContext(node.Affinity)) { Assert.That(TransactionContext.CurrentTransactionContext, Is.EqualTo(tx)); Assert.That(tx.IsController, Is.EqualTo(node.IsController)); if (node.IsController) { tx.StateChanged += (s, e) => { if (e.NewState == TransactionContextState.Exited) tcs.SetResult(e.OldState); }; } await node.ExecuteOperation().ConfigureAwait(false); if (node.Children != null) { foreach (var child in node.Children) await ExecuteNode(child).ConfigureAwait(false); } if (node.VoteAction == VoteAction.VoteCommit) tx.VoteCommit(); else if (node.VoteAction == VoteAction.VoteRollback) tx.VoteRollback(); } if (node.Parent == null) Assert.That(TransactionContext.CurrentTransactionContext, Is.Null); if (node.IsController) { var actualCommitState = await tcs.Task.ConfigureAwait(false); Assert.That(actualCommitState, Is.EqualTo(node.GetExpectedCommitState())); } }
public static void InvalidStateTransitionTest_Exited_ToBeCommitted(TransactionContextAffinity affinity) { var ex = Assert.Throws<InvalidOperationException>( () => { using (var context = new TransactionContext(affinity)) { context.Exit(); Assert.AreEqual(TransactionContextState.Exited, context.State); context.VoteCommit(); } }); }