Class LlmFailoverBuilder
Produces an ordered array of Expectations that simulate a provider
returning failures for the first N attempts, then succeeding on subsequent
attempts. This lets you point a retry-capable LLM client (LiteLLM, Envoy
AI Gateway, an SDK's own retry config) at MockServer and deterministically
assert that failover/retry logic works.
The mechanism relies on MockServer's expectation matching order and
Times exhaustion: failure expectations are registered first with
limited Times, so they are matched and consumed before the unlimited
success expectation is reached.
Example:
llmFailover()
.withPath("/v1/chat/completions")
.withProvider(Provider.OPENAI)
.withModel("gpt-4o")
.failWith(503) // attempt 1 fails with 503
.failWith(429) // attempt 2 fails with 429
.thenRespondWith(completion) // attempt 3+ succeeds
.build(); // returns Expectation[]
-
Method Summary
Modifier and TypeMethodDescriptionapplyTo(MockServerClient client) Build and register all expectations with the MockServerClient.build()Build all expectations without registering them.failWith(int statusCode) Add a single failure attempt returning the given HTTP status code with a default provider-plausible error body.failWith(int statusCode, int count) Addcountconsecutive failure attempts returning the given HTTP status code with a default provider-plausible error body.Add a single failure attempt returning the given HTTP status code with a custom error body.intReturns the total number of failure attempts configured.static LlmFailoverBuilderEntry point for building an LLM failover scenario.thenRespondWith(Completion completion) Set the completion to return after all failures are exhausted.Set the model name for the success response.Set the request path to match (e.g.withProvider(Provider provider) Set the LLM provider for the success response encoding.
-
Method Details
-
llmFailover
Entry point for building an LLM failover scenario.- Returns:
- a new LlmFailoverBuilder
-
withPath
Set the request path to match (e.g./v1/chat/completions).- Parameters:
path- the path- Returns:
- this builder
-
withProvider
Set the LLM provider for the success response encoding.- Parameters:
provider- the provider- Returns:
- this builder
-
withModel
Set the model name for the success response.- Parameters:
model- the model name- Returns:
- this builder
-
failWith
Add a single failure attempt returning the given HTTP status code with a default provider-plausible error body.- Parameters:
statusCode- the HTTP status code (e.g. 503, 429, 500)- Returns:
- this builder
-
failWith
Add a single failure attempt returning the given HTTP status code with a custom error body.- Parameters:
statusCode- the HTTP status code (e.g. 503, 429, 500)errorBody- the response body to return- Returns:
- this builder
-
failWith
Addcountconsecutive failure attempts returning the given HTTP status code with a default provider-plausible error body.- Parameters:
statusCode- the HTTP status codecount- number of consecutive failures- Returns:
- this builder
-
thenRespondWith
Set the completion to return after all failures are exhausted.- Parameters:
completion- the success completion- Returns:
- this builder
-
applyTo
Build and register all expectations with the MockServerClient.- Parameters:
client- the MockServerClient- Returns:
- the created expectations
-
build
Build all expectations without registering them.Returns an ordered array of failure expectations followed by a single success expectation with
Times.unlimited(). Consecutive failures with the same status code (and body) are coalesced into one expectation withTimes.exactly(count); otherwise each failure is its own expectation withTimes.exactly(1). Registration order ensures failures are matched first and consumed before the success expectation.- Returns:
- the array of expectations
-
getFailureCount
public int getFailureCount()Returns the total number of failure attempts configured.- Returns:
- the failure count
-