public TransactionContextTestNode AddChild(TransactionContextTestNode child) { child.Parent = this; _children.Add(child); return this; }
public TransactionContextTestNode AddChild(TransactionContextTestNode child) { child.Parent = this; _children.Add(child); return(this); }
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())); } }
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 async Task NestedContextTest(TransactionContextTestNode testNode) { const int IterationCount = 50; const string TraceFileName = "Transactions.TransactionHandlerTest.NestedContextTest.log.csv"; try { Trace.AutoFlush = true; Trace.UseGlobalLock = true; Trace.Listeners.Add(new TextWriterTraceListener(new StreamWriter(TraceFileName, false))); Trace.WriteLine(testNode.ToString(), "NLight.Tests.Unit.Transactions.TransactionHandlerTest"); var tasks = new List<Task>(); for (int i = 0; i < IterationCount; i++) tasks.Add(Task.Factory.StartNew(() => ExecuteNode(testNode), TaskCreationOptions.DenyChildAttach).Unwrap()); await Task.WhenAll(tasks).ConfigureAwait(false); } finally { Trace.Close(); } CheckTraceLog(TraceFileName); }