Configuration
- Options:
ChaosOutcomeStrategyOptions<T> - Extensions:
AddChaosOutcome
Basic usage
Here are several ways to configure the outcome chaos strategy:Complete example
Here’s a complete example showing outcome injection with retry:Strategy options
This strategy is a reactive chaos strategy, but it does not have a
ShouldHandle delegate.Generating outcomes
You have two main approaches to generating outcomes:Using OutcomeGenerator<T> class
TheOutcomeGenerator<T> is a convenience API that allows you to specify what outcomes (results or exceptions) are to be injected. Additionally, it also allows assigning weight to each registered outcome.
When multiple outcomes are registered with different weights, outcomes with higher weights are more likely to be injected. For example, an outcome with weight 50 will be injected half as often as one with weight 100.
Using delegates
Delegates give you the most flexibility at the expense of slightly more complicated syntax. Delegates also support asynchronous outcome generation, if you ever need that possibility.Telemetry
The outcome chaos strategy reports the following telemetry events:
Here are some sample events:
The
Chaos.OnOutcome telemetry event will be reported only if the outcome chaos strategy injects an outcome object. If the outcome is not injected or injected but the generator delegate throws an exception then there will be no telemetry emitted. Also, the Result will be always empty for the Chaos.OnOutcome telemetry event.How it works
Normal execution (no chaos)
Chaos execution (outcome injected)
When a fake outcome is injected, the user’s callback is not invoked. The injected outcome is returned directly.
Use cases
Outcome injection is useful for:- Testing error responses: Verify how your application handles different HTTP status codes or error results
- Simulating service responses: Test scenarios where external services return specific responses
- Testing retry logic: Ensure your retry strategies correctly handle transient failures
- Validating circuit breaker behavior: Test how your circuit breaker opens and closes based on outcomes
- Testing fallback mechanisms: Verify that fallback logic activates correctly when certain outcomes occur