Skip to content

Commit

Permalink
[Docs] Fix hedging documentation about unhappy paths (#1730)
Browse files Browse the repository at this point in the history
* Fix hedging documentation about unhappy paths

* Try building solution before docfx

---------

Co-authored-by: Martin Costello <martin@martincostello.com>
  • Loading branch information
peter-csala and martincostello committed Oct 26, 2023
1 parent 9e036de commit 9b2b170
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- name: Generate documentation
run: |
dotnet tool restore
dotnet build --configuration Release
dotnet docfx docs/docfx.json
- name: Deploy
Expand Down
16 changes: 8 additions & 8 deletions docs/strategies/hedging.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ When the `Delay` property is set to a value greater than zero, the hedging strat
- The primary execution is initiated.
- If the initial execution either fails or takes longer than the `Delay` to complete, a new execution is initiated.
- If the first two executions fail or exceed the `Delay` (calculated from the last initiated execution), another execution is triggered.
- The final result is the result of fastest successful execution.
- If all executions fail, the final result will be the first failure encountered.
- **Happy path**: The final result is the result of fastest successful execution.
- **Unhappy path**: If all executions fail, the final result will be the primary execution's failure.

#### Latency: happy path sequence diagram

Expand Down Expand Up @@ -155,8 +155,8 @@ In fallback mode, the `Delay` value should be less than `TimeSpan.Zero`. This mo

- An execution is initiated, and the strategy waits for its completion.
- If the initial execution fails, new one is initiated.
- The final result will be the first successful execution.
- If all executions fail, the final result will be the first failure encountered.
- **Happy path**: The final result will be the first successful execution.
- **Unhappy path**: If all executions fail, the final result will be the primary execution's failure.

#### Fallback: happy path sequence diagram

Expand Down Expand Up @@ -224,8 +224,8 @@ The hedging strategy operates in parallel mode when the `Delay` property is set
> Use this mode only when absolutely necessary, as it consumes the most resources, particularly when the hedging strategy uses remote resources such as remote HTTP services.
- All executions are initiated simultaneously, adhering to the `MaxHedgedAttempts` limit.
- The final result will be the fastest successful execution.
- If all executions fail, the final result will be the first failure encountered.
- **Happy path**: The final result will be the fastest successful execution.
- **Unhappy path**: If all executions fail, the final result will be the primary execution's failure

#### Parallel: happy path sequence diagram

Expand Down Expand Up @@ -293,8 +293,8 @@ sequenceDiagram
HUC->>-H: Fails (R1)
deactivate H
H->>P: Propagates failure (R2)
P->>C: Propagates failure (R2)
H->>P: Propagates failure (R1)
P->>C: Propagates failure (R1)
```

### Dynamic mode
Expand Down
32 changes: 32 additions & 0 deletions test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,38 @@ public async Task ExecuteAsync_OnHedgingEventThrows_EnsureExceptionRethrown()
.WithMessage("my-exception");
}

[InlineData(-1)] // Fallback mode
[InlineData(0)] // Parallel mode
[InlineData(1)] // Latency mode
[Theory]
public async Task ExecuteAsync_AllAttemptsFailAndTheOriginalCallIsTheSlowest_EnsureOriginalCallsResultReturned(int delaySeconds)
{
// arrange
int callCounter = 0;
async ValueTask<string> Execute(CancellationToken token)
{
if (callCounter == 0)
{
await Task.Delay(200, token);
return "1st";
}

return (++callCounter).ToString(System.Globalization.CultureInfo.InvariantCulture);
}

_options.ShouldHandle = new PredicateBuilder<string>().HandleResult(s => s.Length < 5);
_options.MaxHedgedAttempts = 2;
_options.Delay = TimeSpan.FromSeconds(delaySeconds);

ConfigureHedging(async context => Outcome.FromResult(await Execute(context.CancellationToken)));

// act
var actual = await Create().ExecuteAsync(Execute, _cts.Token);

// assert
actual.Should().Be("1st");
}

[Fact]
public async Task ExecuteAsync_CancellationLinking_Ok()
{
Expand Down

0 comments on commit 9b2b170

Please sign in to comment.