Skip to main content
The outcome reactive chaos strategy is designed to inject or substitute fake results into system operations. This allows testing how an application behaves when it receives different types of responses, like successful results, errors, or exceptions.

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:
Chaos strategies should be placed last in the resilience pipeline. This ensures that the fake outcome is injected at the last minute, allowing your other resilience strategies (retry, circuit breaker, etc.) to handle the injected outcome.

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

The OutcomeGenerator<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

Anti-patterns

DON’T: Inject only faults using outcome strategy

Don’t use outcome strategies to inject only exceptions. Use the fault chaos strategy instead.

DO: Use fault strategy for exceptions

Use the fault strategy to inject exceptions. This provides better telemetry and control.

DON’T: Mix faults and outcomes in advanced scenarios

While you can inject exceptions using the outcome strategy, this has undesired implications:
  • Telemetry events mix results and exceptions, making metrics less clear
  • You lose separate control over fault vs. outcome injection rates
  • Callbacks become more complex as they need to handle both cases

DO: Separate fault and outcome strategies

Separate fault and outcome strategies for better control, clearer telemetry, and more maintainable code.

Best practices

When using outcome injection in production environments, always:
  • Start with a very low injection rate (e.g., 0.01 or 1%)
  • Target only specific test users or tenants
  • Use realistic outcomes that could actually occur in production
  • Monitor the impact on user experience and system behavior
  • Have a quick way to disable chaos injection if needed
Combine outcome injection with retry and circuit breaker strategies to ensure your resilience mechanisms handle different types of responses correctly.