TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Polly-Contrib/Simmy/llms.txt
Use this file to discover all available pages before exploring further.
InjectLatency policy adds an artificial delay before the wrapped call executes. This makes it the ideal tool for testing how your application responds to sluggish dependencies: does a timeout policy fire at the right threshold? Does a circuit breaker open after repeated slow responses? Does your UI remain responsive? Simmy handles all of this without needing a real slow service — you control the delay, the injection rate, and whether the policy is active at all.
Latency injection policies are also cancellable: if a CancellationToken is signalled during the injected delay (for example, by an outer timeout policy), Simmy cancels the sleep immediately rather than blocking until the full delay elapses.
Factory method signatures
Quick start
Decide on a latency and injection rate
Choose how long the artificial delay should be and on what proportion of calls it fires. A 5-second delay on 10% of calls is a realistic starting point.
Build the chaos policy
Configure the latency value, injection rate, and enabled flag through the fluent builder.An injection rate of
0.1 means 10% of calls will be delayed by 5 seconds before the real code runs. Setting .Enabled(isEnabled) ties the active state to the isEnabled variable — flip it to false to pause chaos without changing any policy configuration.Fluent option methods
.Latency(TimeSpan)
Provides a fixed delay to inject. Simmy sleeps for this duration before the wrapped call.
.InjectionRate(double)
A value between
0.0 and 1.0 controlling how often the delay is injected..Enabled() / .Enabled(bool)
Activates the monkey policy. Pass
false or a variable to pause injection at runtime.Dynamic latency with a delegate
Supply aFunc<Context, CancellationToken, TimeSpan> to vary the injected delay per call. This lets you simulate jitter or model degrading performance under load.
Async variant with PolicyWrap
The async variant acceptsInjectLatencyAsyncOptions (where EnabledWhen takes a Func<Context, CancellationToken, Task<bool>>) and returns an AsyncInjectLatencyPolicy. It integrates naturally into async PolicyWrap chains.
Place the
InjectLatency policy innermost in the PolicyWrap. This way the outer timeout policy sees the injected delay as a real slow response and trips at its configured threshold — which is exactly the behaviour you want to verify.Testing timeout thresholds
Latency injection is the most direct way to test that aPolly.Timeout policy fires at the right threshold and that downstream policies handle the resulting TimeoutRejectedException correctly.
Cancellation support
Latency injection policies respect theCancellationToken passed to Execute/ExecuteAsync. If the token is cancelled during the injected sleep — for example, because an outer TimeoutPolicy cancelled the execution — Simmy aborts the delay immediately and propagates the cancellation.
Typed variant
When wrapping calls that return a value, useMonkeyPolicy.InjectLatency<TResult>, which returns InjectLatencyPolicy<TResult> and works identically but preserves the return type.
Reference
See Policy Options for the full reference on shared fluent options, includingContext-driven delegate overloads for InjectionRate and EnabledWhen.