Repository navigation
fix(transactions): await recovery cleanup tasks #11188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ReubenBond
merged 3 commits into
dotnet:main
from
ReubenBond:rb-fix-transaction-recovery-lifetime
Sep 8, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
test/Transactions/Orleans.Transactions.Tests/TransactionRecoveryTestsRunnerLifetimeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| using System.Diagnostics; | ||
| using Orleans.Transactions.Diagnostics; | ||
| using Orleans.Transactions.TestKit; | ||
| using TestExtensions; | ||
| using Xunit; | ||
|
|
||
| namespace Orleans.Transactions.Tests; | ||
|
|
||
| [TestSuite("BVT")] | ||
| [TestProvider("None")] | ||
| [TestArea("Transactions")] | ||
| [TestCategory("BVT"), TestCategory("Transactions")] | ||
| public class TransactionRecoveryTestsRunnerLifetimeTests | ||
| { | ||
| private static readonly TimeSpan SynchronizationTimeout = TimeSpan.FromSeconds(10); | ||
|
|
||
| [Fact] | ||
| public async Task ProducerCancellationScope_CompletedProducerDisposesCancellationSource() | ||
| { | ||
| var stopProducing = new CancellationTokenSource(); | ||
| var scope = new TransactionRecoveryTestsRunner.ProducerCancellationScope( | ||
| stopProducing, | ||
| Task.CompletedTask); | ||
|
|
||
| await scope.DisposeAsync(); | ||
|
|
||
| Assert.Throws<ObjectDisposedException>(() => stopProducing.Token); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ProducerCancellationScope_FaultedProducerDoesNotThrowDuringDisposal() | ||
| { | ||
| var stopProducing = new CancellationTokenSource(); | ||
| var scope = new TransactionRecoveryTestsRunner.ProducerCancellationScope( | ||
| stopProducing, | ||
| Task.FromException(new InvalidOperationException("boom"))); | ||
|
|
||
| await scope.DisposeAsync(); | ||
|
|
||
| Assert.Throws<ObjectDisposedException>(() => stopProducing.Token); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ProducerCancellationScope_ActiveProducerIsAwaitedBeforeCancellationSourceDisposal() | ||
| { | ||
| var stopProducing = new CancellationTokenSource(); | ||
| var cancellationObserved = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| using var registration = stopProducing.Token.Register(() => cancellationObserved.TrySetResult()); | ||
| var producer = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var scope = new TransactionRecoveryTestsRunner.ProducerCancellationScope( | ||
| stopProducing, | ||
| producer.Task); | ||
|
|
||
| var disposal = scope.DisposeAsync().AsTask(); | ||
| await cancellationObserved.Task.WaitAsync(SynchronizationTimeout, TestContext.Current.CancellationToken); | ||
|
|
||
| Assert.False(disposal.IsCompleted); | ||
| Assert.True(stopProducing.Token.IsCancellationRequested); | ||
|
|
||
| producer.TrySetResult(); | ||
| await disposal.WaitAsync(SynchronizationTimeout, TestContext.Current.CancellationToken); | ||
|
|
||
| Assert.Throws<ObjectDisposedException>(() => stopProducing.Token); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ObserveAndReleaseGateAsync_ReachedGateCompletesBeforeDisposal() | ||
| { | ||
| var observer = new TransactionRecoveryEventObserver(_ => true); | ||
| var gate = observer.GateNextTransition(_ => true); | ||
| var transition = CreateTransition(); | ||
| var observation = TransactionRecoveryTestsRunner.ObserveAndReleaseGateAsync( | ||
| gate, | ||
| GetDeadline(), | ||
| TestContext.Current.CancellationToken); | ||
| var blockingTask = RunLongRunning(() => | ||
| { | ||
| Assert.True(gate.TryReach(transition)); | ||
| gate.Block(); | ||
| }); | ||
|
|
||
| try | ||
| { | ||
| Assert.Same(transition, await observation.WaitAsync( | ||
| SynchronizationTimeout, | ||
| TestContext.Current.CancellationToken)); | ||
| await blockingTask.WaitAsync(SynchronizationTimeout, TestContext.Current.CancellationToken); | ||
| } | ||
| finally | ||
| { | ||
| gate.Dispose(); | ||
| observer.Dispose(); | ||
| await blockingTask.WaitAsync(SynchronizationTimeout, TestContext.Current.CancellationToken); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ObserveAndReleaseGateAsync_CancellationCompletesBeforeDisposal() | ||
| { | ||
| var observer = new TransactionRecoveryEventObserver(_ => true); | ||
| var gate = observer.GateNextTransition(_ => true); | ||
| using var cancellation = CancellationTokenSource.CreateLinkedTokenSource( | ||
| TestContext.Current.CancellationToken); | ||
| var observation = TransactionRecoveryTestsRunner.ObserveAndReleaseGateAsync( | ||
| gate, | ||
| GetDeadline(), | ||
| cancellation.Token); | ||
| var blockingTask = RunLongRunning(gate.Block); | ||
|
|
||
| try | ||
| { | ||
| await cancellation.CancelAsync(); | ||
| await Assert.ThrowsAnyAsync<OperationCanceledException>(() => observation); | ||
| await blockingTask.WaitAsync(SynchronizationTimeout, TestContext.Current.CancellationToken); | ||
| } | ||
| finally | ||
| { | ||
| gate.Dispose(); | ||
| observer.Dispose(); | ||
| await blockingTask.WaitAsync(SynchronizationTimeout, TestContext.Current.CancellationToken); | ||
| } | ||
| } | ||
|
|
||
| private static TransactionRecoveryEventObserver.RecoveryTransition CreateTransition() => | ||
| new( | ||
| Sequence: 1, | ||
| ObservedAtUtc: DateTime.UtcNow, | ||
| Elapsed: TimeSpan.Zero, | ||
| TransactionRecoveryEventObserver.RecoveryTransitionKind.TransactionConfirmCompleted, | ||
| TransactionIds: [], | ||
| TransactionDiagnosticEvents.TransactionProtocolRole.RemoteParticipant, | ||
| TransactionDiagnosticEvents.TransactionPhase.Confirm, | ||
| ResourceName: "test", | ||
| GrainId: null, | ||
| SiloAddress: null, | ||
| ActivationId: default, | ||
| Status: null, | ||
| CommitCount: null, | ||
| Succeeded: true); | ||
|
ReubenBond marked this conversation as resolved.
Resolved
|
||
|
|
||
| private static long GetDeadline() | ||
| => Stopwatch.GetTimestamp() + (long)(SynchronizationTimeout.TotalSeconds * Stopwatch.Frequency); | ||
|
|
||
| private static Task RunLongRunning(Action action) | ||
| => Task.Factory.StartNew( | ||
| action, | ||
| CancellationToken.None, | ||
| TaskCreationOptions.LongRunning, | ||
| TaskScheduler.Default); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.