-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
156 additions
and
133 deletions.
There are no files selected for viewing
This file contains 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,63 @@ | ||
# Testing | ||
|
||
This document explains how to test Polly’s resilience pipelines. You should not test how the resilience pipelines operate internally, but rather test your own settings or custom delegates. | ||
|
||
To make the testing process simpler, Polly offers the [`Polly.Testing`](https://www.nuget.org/packages/Polly.Testing/) package. This package has a range of APIs designed to help you test the setup and combination of resilience pipelines in your user code. | ||
|
||
## Usage | ||
|
||
Begin by adding the `Polly.Testing` package to your test project: | ||
|
||
```sh | ||
dotnet add package Polly.Testing | ||
``` | ||
|
||
Use the `GetPipelineDescriptor` extension method to get the [`ResiliencePipelineDescriptor`](xref:Polly.Testing.ResiliencePipelineDescriptor) which provides details on the pipeline's composition: | ||
|
||
<!-- snippet: get-pipeline-descriptor --> | ||
```cs | ||
// Build your resilience pipeline. | ||
ResiliencePipeline pipeline = new ResiliencePipelineBuilder() | ||
.AddRetry(new RetryStrategyOptions | ||
{ | ||
MaxRetryAttempts = 4 | ||
}) | ||
.AddTimeout(TimeSpan.FromSeconds(1)) | ||
.Build(); | ||
|
||
// Retrieve the descriptor. | ||
ResiliencePipelineDescriptor descriptor = pipeline.GetPipelineDescriptor(); | ||
|
||
// Check the pipeline's composition with the descriptor. | ||
Assert.Equal(2, descriptor.Strategies.Count); | ||
|
||
// Verify the retry settings. | ||
var retryOptions = Assert.IsType<RetryStrategyOptions>(descriptor.Strategies[0].Options); | ||
Assert.Equal(4, retryOptions.MaxRetryAttempts); | ||
|
||
// Confirm the timeout settings. | ||
var timeoutOptions = Assert.IsType<TimeoutStrategyOptions>(descriptor.Strategies[1].Options); | ||
Assert.Equal(TimeSpan.FromSeconds(1), timeoutOptions.Timeout); | ||
``` | ||
<!-- endSnippet --> | ||
|
||
The `GetPipelineDescriptor` extension method is also available for the generic `ResiliencePipeline<T>`: | ||
|
||
<!-- snippet: get-pipeline-descriptor-generic --> | ||
```cs | ||
// Construct your resilience pipeline. | ||
ResiliencePipeline<string> pipeline = new ResiliencePipelineBuilder<string>() | ||
.AddRetry(new RetryStrategyOptions<string> | ||
{ | ||
MaxRetryAttempts = 4 | ||
}) | ||
.AddTimeout(TimeSpan.FromSeconds(1)) | ||
.Build(); | ||
|
||
// Obtain the descriptor. | ||
ResiliencePipelineDescriptor descriptor = pipeline.GetPipelineDescriptor(); | ||
|
||
// Check the pipeline's composition with the descriptor. | ||
// ... | ||
``` | ||
<!-- endSnippet --> |
This file contains 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 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 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 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 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,61 @@ | ||
using Polly.Retry; | ||
using Polly.Testing; | ||
using Polly.Timeout; | ||
using Xunit; | ||
|
||
namespace Snippets.Docs; | ||
|
||
internal static class Testing | ||
{ | ||
public static void GetPipelineDescriptor() | ||
{ | ||
#region get-pipeline-descriptor | ||
|
||
// Build your resilience pipeline. | ||
ResiliencePipeline pipeline = new ResiliencePipelineBuilder() | ||
.AddRetry(new RetryStrategyOptions | ||
{ | ||
MaxRetryAttempts = 4 | ||
}) | ||
.AddTimeout(TimeSpan.FromSeconds(1)) | ||
.Build(); | ||
|
||
// Retrieve the descriptor. | ||
ResiliencePipelineDescriptor descriptor = pipeline.GetPipelineDescriptor(); | ||
|
||
// Check the pipeline's composition with the descriptor. | ||
Assert.Equal(2, descriptor.Strategies.Count); | ||
|
||
// Verify the retry settings. | ||
var retryOptions = Assert.IsType<RetryStrategyOptions>(descriptor.Strategies[0].Options); | ||
Assert.Equal(4, retryOptions.MaxRetryAttempts); | ||
|
||
// Confirm the timeout settings. | ||
var timeoutOptions = Assert.IsType<TimeoutStrategyOptions>(descriptor.Strategies[1].Options); | ||
Assert.Equal(TimeSpan.FromSeconds(1), timeoutOptions.Timeout); | ||
|
||
#endregion | ||
} | ||
|
||
public static void GetPipelineDescriptorGeneric() | ||
{ | ||
#region get-pipeline-descriptor-generic | ||
|
||
// Construct your resilience pipeline. | ||
ResiliencePipeline<string> pipeline = new ResiliencePipelineBuilder<string>() | ||
.AddRetry(new RetryStrategyOptions<string> | ||
{ | ||
MaxRetryAttempts = 4 | ||
}) | ||
.AddTimeout(TimeSpan.FromSeconds(1)) | ||
.Build(); | ||
|
||
// Obtain the descriptor. | ||
ResiliencePipelineDescriptor descriptor = pipeline.GetPipelineDescriptor(); | ||
|
||
// Check the pipeline's composition with the descriptor. | ||
// ... | ||
|
||
#endregion | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 was deleted.
Oops, something went wrong.