|
| 1 | +using Polly.Hedging; |
| 2 | +using Polly.Timeout; |
| 3 | + |
| 4 | +namespace Polly.Core.Tests.Issues; |
| 5 | + |
| 6 | +public partial class IssuesTests |
| 7 | +{ |
| 8 | + // https://github.com/App-vNext/Polly/issues/3086 |
| 9 | + // When a strategy substitutes the caller's CancellationToken with an internal one (timeout, hedging), |
| 10 | + // a caller-initiated cancellation must still surface an OperationCanceledException carrying the |
| 11 | + // caller's token, so that callers can distinguish caller cancellation from other failures. |
| 12 | + [Fact] |
| 13 | + public async Task Timeout_CallerCancellation_ExceptionCarriesCallerToken_3086() |
| 14 | + { |
| 15 | + var pipeline = new ResiliencePipelineBuilder() |
| 16 | + .AddTimeout(TimeSpan.FromMinutes(1)) |
| 17 | + .Build(); |
| 18 | + |
| 19 | + using var cts = new CancellationTokenSource(); |
| 20 | + |
| 21 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 22 | + pipeline.ExecuteAsync( |
| 23 | + async token => |
| 24 | + { |
| 25 | + cts.Cancel(); // simulate cancellation request from upstream caller |
| 26 | + token.ThrowIfCancellationRequested(); // simulate cancellation response from downstream code |
| 27 | + }, |
| 28 | + cts.Token).AsTask()); |
| 29 | + |
| 30 | + exception.CancellationToken.ShouldBe(cts.Token); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task TimeoutThenRetry_CallerCancellation_ExceptionCarriesCallerToken_3086() |
| 35 | + { |
| 36 | + var pipeline = new ResiliencePipelineBuilder() |
| 37 | + .AddTimeout(TimeSpan.FromMinutes(1)) |
| 38 | + .AddRetry(new() { MaxRetryAttempts = 3, Delay = TimeSpan.Zero }) |
| 39 | + .Build(); |
| 40 | + |
| 41 | + using var cts = new CancellationTokenSource(); |
| 42 | + |
| 43 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 44 | + pipeline.ExecuteAsync( |
| 45 | + async token => |
| 46 | + { |
| 47 | + cts.Cancel(); |
| 48 | + token.ThrowIfCancellationRequested(); |
| 49 | + }, |
| 50 | + cts.Token).AsTask()); |
| 51 | + |
| 52 | + exception.CancellationToken.ShouldBe(cts.Token); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task RetryThenTimeout_CallerCancellation_ExceptionCarriesCallerToken_3086() |
| 57 | + { |
| 58 | + var pipeline = new ResiliencePipelineBuilder() |
| 59 | + .AddRetry(new() { MaxRetryAttempts = 3, Delay = TimeSpan.Zero }) |
| 60 | + .AddTimeout(TimeSpan.FromMinutes(1)) |
| 61 | + .Build(); |
| 62 | + |
| 63 | + using var cts = new CancellationTokenSource(); |
| 64 | + |
| 65 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 66 | + pipeline.ExecuteAsync( |
| 67 | + async token => |
| 68 | + { |
| 69 | + cts.Cancel(); |
| 70 | + token.ThrowIfCancellationRequested(); |
| 71 | + }, |
| 72 | + cts.Token).AsTask()); |
| 73 | + |
| 74 | + exception.CancellationToken.ShouldBe(cts.Token); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task NestedTimeouts_CallerCancellation_ExceptionCarriesCallerToken_3086() |
| 79 | + { |
| 80 | + var innermost = new ResiliencePipelineBuilder() |
| 81 | + .AddTimeout(TimeSpan.FromMinutes(1)) |
| 82 | + .Build(); |
| 83 | + |
| 84 | + var inner = new ResiliencePipelineBuilder() |
| 85 | + .AddTimeout(TimeSpan.FromMinutes(2)) |
| 86 | + .AddPipeline(innermost) |
| 87 | + .Build(); |
| 88 | + |
| 89 | + var pipeline = new ResiliencePipelineBuilder() |
| 90 | + .AddTimeout(TimeSpan.FromMinutes(3)) |
| 91 | + .AddPipeline(inner) |
| 92 | + .Build(); |
| 93 | + |
| 94 | + using var cts = new CancellationTokenSource(); |
| 95 | + |
| 96 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 97 | + pipeline.ExecuteAsync( |
| 98 | + async token => |
| 99 | + { |
| 100 | + cts.Cancel(); |
| 101 | + token.ThrowIfCancellationRequested(); |
| 102 | + }, |
| 103 | + cts.Token).AsTask()); |
| 104 | + |
| 105 | + exception.CancellationToken.ShouldBe(cts.Token); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task Hedging_CallerCancellation_ExceptionCarriesCallerToken_3086() |
| 110 | + { |
| 111 | + var pipeline = new ResiliencePipelineBuilder<string>() |
| 112 | + .AddHedging(new HedgingStrategyOptions<string> |
| 113 | + { |
| 114 | + MaxHedgedAttempts = 1, |
| 115 | + Delay = TimeSpan.FromMinutes(1), // ensure only the primary attempt runs before cancellation |
| 116 | + ShouldHandle = _ => PredicateResult.False(), // accept the primary outcome as-is |
| 117 | + }) |
| 118 | + .Build(); |
| 119 | + |
| 120 | + using var cts = new CancellationTokenSource(); |
| 121 | + |
| 122 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 123 | + pipeline.ExecuteAsync( |
| 124 | + async token => |
| 125 | + { |
| 126 | + cts.Cancel(); |
| 127 | + token.ThrowIfCancellationRequested(); |
| 128 | + return "unreachable"; |
| 129 | + }, |
| 130 | + cts.Token).AsTask()); |
| 131 | + |
| 132 | + exception.CancellationToken.ShouldBe(cts.Token); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task WithoutTokenSubstitution_CallerCancellation_ExceptionCarriesCallerToken_3086() |
| 137 | + { |
| 138 | + // A pipeline that does not substitute the token has always surfaced the caller's token |
| 139 | + // and this case simply guards against regressions for the common path. |
| 140 | + var pipeline = new ResiliencePipelineBuilder() |
| 141 | + .AddRetry(new() { MaxRetryAttempts = 3, Delay = TimeSpan.Zero }) |
| 142 | + .Build(); |
| 143 | + |
| 144 | + using var cts = new CancellationTokenSource(); |
| 145 | + |
| 146 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 147 | + pipeline.ExecuteAsync( |
| 148 | + async token => |
| 149 | + { |
| 150 | + cts.Cancel(); |
| 151 | + token.ThrowIfCancellationRequested(); |
| 152 | + }, |
| 153 | + cts.Token).AsTask()); |
| 154 | + |
| 155 | + exception.CancellationToken.ShouldBe(cts.Token); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public async Task Timeout_RealTimeout_StillThrowsTimeoutRejectedException_3086() |
| 160 | + { |
| 161 | + // "only if": a real timeout (caller token not cancelled) must remain a TimeoutRejectedException, |
| 162 | + // and must not be rewritten into a caller-cancellation. |
| 163 | + var pipeline = new ResiliencePipelineBuilder { TimeProvider = TimeProvider } |
| 164 | + .AddTimeout(TimeSpan.FromSeconds(1)) |
| 165 | + .Build(); |
| 166 | + |
| 167 | + await Should.ThrowAsync<TimeoutRejectedException>(() => |
| 168 | + pipeline.ExecuteAsync( |
| 169 | + async token => |
| 170 | + { |
| 171 | + var delay = TimeProvider.Delay(TimeSpan.FromSeconds(10), token); |
| 172 | + TimeProvider.Advance(TimeSpan.FromSeconds(2)); |
| 173 | + await delay; |
| 174 | + }, |
| 175 | + CancellationToken.None).AsTask()); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public async Task Timeout_UnrelatedCancellation_ExceptionPreserved_3086() |
| 180 | + { |
| 181 | + // "only if": when the caller token is not cancelled, an unrelated OperationCanceledException |
| 182 | + // thrown by user code must propagate unchanged (its own token must be preserved). |
| 183 | + var pipeline = new ResiliencePipelineBuilder() |
| 184 | + .AddTimeout(TimeSpan.FromMinutes(1)) |
| 185 | + .Build(); |
| 186 | + |
| 187 | + using var callerCts = new CancellationTokenSource(); |
| 188 | + using var unrelatedCts = new CancellationTokenSource(); |
| 189 | + |
| 190 | + unrelatedCts.Cancel(); |
| 191 | + |
| 192 | + var exception = await Should.ThrowAsync<OperationCanceledException>(() => |
| 193 | + pipeline.ExecuteAsync( |
| 194 | + async token => |
| 195 | + { |
| 196 | + await Task.Yield(); |
| 197 | + throw new OperationCanceledException(unrelatedCts.Token); |
| 198 | + }, |
| 199 | + callerCts.Token).AsTask()); |
| 200 | + |
| 201 | + exception.CancellationToken.ShouldBe(unrelatedCts.Token); |
| 202 | + } |
| 203 | +} |
0 commit comments