|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using ManagedCode.Communication.CQRS; |
| 6 | +using Shouldly; |
| 7 | + |
| 8 | +namespace ManagedCode.Communication.Tests.CQRS; |
| 9 | + |
| 10 | +internal static class CqrsStreamCancellationJoinScenario |
| 11 | +{ |
| 12 | + private const string StartedState = "started"; |
| 13 | + private const string FinalState = "complete"; |
| 14 | + private const int OperationTimeoutSeconds = 10; |
| 15 | + private const int PrematureCompletionObservationMilliseconds = 250; |
| 16 | + |
| 17 | + internal static async Task AssertFaultingCallbackJoinAsync() |
| 18 | + { |
| 19 | + var probe = new CqrsStreamCancellationJoinProbe(); |
| 20 | + var enumerator = CqrsStream.Create<ProgressUpdate, FinalResult>(probe.HandleAsync).GetAsyncEnumerator(); |
| 21 | + await RunFaultingCallbackJoinAsync(probe, enumerator); |
| 22 | + } |
| 23 | + |
| 24 | + internal static async Task AssertOrdinaryCancellationJoinAsync() |
| 25 | + { |
| 26 | + using var cancellation = new CancellationTokenSource(); |
| 27 | + var handlerSettled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 28 | + var enumerator = CqrsStream.Create<ProgressUpdate, FinalResult>(async writer => |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + await writer.StartedAsync(new ProgressUpdate(StartedState)); |
| 33 | + await Task.Delay(Timeout.InfiniteTimeSpan, writer.CancellationToken).ConfigureAwait(true); |
| 34 | + return Result<FinalResult>.Succeed(new FinalResult(FinalState)); |
| 35 | + } |
| 36 | + finally |
| 37 | + { |
| 38 | + handlerSettled.TrySetResult(); |
| 39 | + } |
| 40 | + }).GetAsyncEnumerator(cancellation.Token); |
| 41 | + await RunOrdinaryCancellationJoinAsync(enumerator, handlerSettled.Task, cancellation); |
| 42 | + } |
| 43 | + |
| 44 | + private static async Task RunFaultingCallbackJoinAsync( |
| 45 | + CqrsStreamCancellationJoinProbe probe, IAsyncEnumerator<Chunk> enumerator) |
| 46 | + { |
| 47 | + Task<bool>? firstMove = null; |
| 48 | + Task? disposal = null; |
| 49 | + Exception? primaryFailure = null; |
| 50 | + var cleanupFailures = new List<Exception>(); |
| 51 | + var callbackFailureObserved = false; |
| 52 | + try |
| 53 | + { |
| 54 | + firstMove = enumerator.MoveNextAsync().AsTask(); |
| 55 | + await probe.HandlerEntered.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 56 | + (await firstMove.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds))).ShouldBeTrue(); |
| 57 | + enumerator.Current.Kind.ShouldBe(CqrsStreamChunkKind.Started); |
| 58 | + await probe.CallbackRegistered.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 59 | + disposal = DisposeEnumeratorAsync(enumerator); |
| 60 | + await probe.CallbackEntered.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 61 | + await probe.FinallyEntered.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 62 | + await Task.WhenAny(disposal, Task.Delay(TimeSpan.FromMilliseconds( |
| 63 | + PrematureCompletionObservationMilliseconds))); |
| 64 | + disposal.IsCompleted.ShouldBeFalse(); |
| 65 | + probe.ReleaseHandler(); |
| 66 | + await probe.HandlerSettled.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 67 | + await AssertOnlyCallbackFailureAsync(disposal, probe.CallbackFailure); |
| 68 | + callbackFailureObserved = true; |
| 69 | + await AssertHealthyFollowUpAsync(); |
| 70 | + } |
| 71 | + catch (Exception exception) |
| 72 | + { |
| 73 | + primaryFailure = exception; |
| 74 | + } |
| 75 | + finally |
| 76 | + { |
| 77 | + probe.ReleaseHandler(); |
| 78 | + disposal ??= DisposeEnumeratorAsync(enumerator); |
| 79 | + if (!callbackFailureObserved) |
| 80 | + { |
| 81 | + await CqrsStreamCancellationJoinCleanup.ObserveTaskAsync(disposal, cleanupFailures); |
| 82 | + } |
| 83 | + if (firstMove is not null) |
| 84 | + { |
| 85 | + await CqrsStreamCancellationJoinCleanup.ObserveTaskAsync(firstMove, cleanupFailures); |
| 86 | + } |
| 87 | + await probe.HandlerSettled; |
| 88 | + } |
| 89 | + CqrsStreamCancellationJoinCleanup.ThrowFailures(primaryFailure, cleanupFailures); |
| 90 | + } |
| 91 | + |
| 92 | + private static async Task RunOrdinaryCancellationJoinAsync( |
| 93 | + IAsyncEnumerator<Chunk> enumerator, Task handlerSettled, CancellationTokenSource cancellation) |
| 94 | + { |
| 95 | + Task<bool>? firstMove = null; |
| 96 | + Task? disposal = null; |
| 97 | + Exception? primaryFailure = null; |
| 98 | + var cleanupFailures = new List<Exception>(); |
| 99 | + try |
| 100 | + { |
| 101 | + firstMove = enumerator.MoveNextAsync().AsTask(); |
| 102 | + (await firstMove.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds))).ShouldBeTrue(); |
| 103 | + enumerator.Current.Kind.ShouldBe(CqrsStreamChunkKind.Started); |
| 104 | + disposal = DisposeEnumeratorAsync(enumerator); |
| 105 | + await handlerSettled.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 106 | + await disposal.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds)); |
| 107 | + } |
| 108 | + catch (Exception exception) |
| 109 | + { |
| 110 | + primaryFailure = exception; |
| 111 | + } |
| 112 | + finally |
| 113 | + { |
| 114 | + await CqrsStreamCancellationJoinCleanup.ObserveTaskAsync( |
| 115 | + cancellation.CancelAsync(), cleanupFailures); |
| 116 | + disposal ??= DisposeEnumeratorAsync(enumerator); |
| 117 | + await CqrsStreamCancellationJoinCleanup.ObserveTaskAsync(disposal, cleanupFailures); |
| 118 | + if (firstMove is not null) |
| 119 | + { |
| 120 | + await CqrsStreamCancellationJoinCleanup.ObserveTaskAsync(firstMove, cleanupFailures); |
| 121 | + } |
| 122 | + await handlerSettled; |
| 123 | + } |
| 124 | + CqrsStreamCancellationJoinCleanup.ThrowFailures(primaryFailure, cleanupFailures); |
| 125 | + } |
| 126 | + |
| 127 | + private static async Task AssertOnlyCallbackFailureAsync(Task disposal, Exception expected) |
| 128 | + { |
| 129 | + var failure = await Should.ThrowAsync<Exception>( |
| 130 | + () => disposal.WaitAsync(TimeSpan.FromSeconds(OperationTimeoutSeconds))); |
| 131 | + var leaves = CqrsStreamCancellationJoinCleanup.Leaves(failure!); |
| 132 | + leaves.Count.ShouldBe(1); |
| 133 | + leaves[0].ShouldBeSameAs(expected); |
| 134 | + } |
| 135 | + |
| 136 | + private static async Task AssertHealthyFollowUpAsync() |
| 137 | + { |
| 138 | + var chunks = new List<Chunk>(); |
| 139 | + await foreach (var chunk in CqrsStream.Create<ProgressUpdate, FinalResult>(async writer => |
| 140 | + { |
| 141 | + await writer.StartedAsync(new ProgressUpdate(StartedState)); |
| 142 | + return Result<FinalResult>.Succeed(new FinalResult(FinalState)); |
| 143 | + })) |
| 144 | + { |
| 145 | + chunks.Add(chunk); |
| 146 | + } |
| 147 | + chunks.ConvertAll(chunk => chunk.Kind).ShouldBe( |
| 148 | + [CqrsStreamChunkKind.Started, CqrsStreamChunkKind.Completed]); |
| 149 | + } |
| 150 | + |
| 151 | + private static async Task DisposeEnumeratorAsync(IAsyncEnumerator<Chunk> enumerator) |
| 152 | + { |
| 153 | + await enumerator.DisposeAsync().ConfigureAwait(false); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments