Configuration
- Options:
ChaosBehaviorStrategyOptions - Extensions:
AddChaosBehavior
Basic usage
Here are several ways to configure the behavior chaos strategy:Complete example
Here’s a complete example showing behavior injection with retry:Strategy options
Use cases
Behavior injection is useful for:- Simulating resource exhaustion: Restart services, flush caches, or simulate memory pressure before operations
- Testing connection handling: Close database connections or network sockets to test reconnection logic
- Manipulating state: Put the system in specific states to test edge cases
- Simulating complex scenarios: Combine multiple actions to simulate real-world failure scenarios
- Testing cleanup logic: Trigger cleanup operations or resource disposal to verify proper handling
- Integration testing: Manipulate external dependencies before operations execute
Example scenarios
Restart a Redis cache
Clear application cache
Simulate connection pool exhaustion
Telemetry
The behavior chaos strategy reports the following telemetry events:
Here are some sample events:
The
Chaos.OnBehavior telemetry event will be reported only if the behavior chaos strategy injects a custom behavior which does not throw an exception. If the behavior is either not injected or injected and throws an exception then there will be no telemetry emitted. Also, the Result will be always empty for the Chaos.OnBehavior telemetry event.How it works
Normal execution (no chaos)
Chaos execution (behavior injected)
Unlike fault or outcome injection, behavior injection does not prevent the user’s callback from being invoked. The custom behavior is executed before the callback is executed.
Anti-patterns
DON’T: Inject delays using behavior strategy
DO: Use latency strategy for delays
Best practices
Advanced example: Context-aware behavior
- Execute different behaviors based on the operation being performed
- Use
EnabledGeneratorto control which operations are affected - Properly handle cancellation tokens in async behavior generators