Test the Retry Executor
Hard About 35 min Free, no account needed
You're given a correct retry executor. Write tests that prove its attempt budget, exception policy, and call sequence.
The brief
A retry loop is easy to read and surprisingly easy to get wrong. Is maxAttempts the total number
of calls, or the number of retries after the first call? Which failures are safe to retry? Does the
loop stop as soon as an attempt succeeds? The executor here is already written and correct. Your
job is to write the tests that prove it.
Here is the code under test:
public sealed class TransientFailureException : Exception
{
public TransientFailureException(string message = "Transient operation failure.")
: base(message) { }
}
public class RetryExecutor
{
public int Execute(Func<int> operation, int maxAttempts) { ... }
}maxAttempts is always positive and includes the first call. Execute retries only
TransientFailureException, returns immediately after the first successful call, and rethrows the
last transient failure when the budget is exhausted. Any other exception propagates immediately.
Write a Tests class. Each public, parameterless method is one test: it passes if it runs
without throwing and fails when an assertion throws. A captured counter makes the operation's call
sequence observable:
int calls = 0;
int result = new RetryExecutor().Execute(() =>
{
calls++;
return 42;
}, 3);A tiny assertion library is in scope (no using needed):
Assert.True(condition); Assert.False(condition);
Assert.Equal(expected, actual); Assert.NotEqual(a, b);
Assert.Null(x); Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);Your suite is run twice over:
- Against the correct
RetryExecutorabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken executors you never see. A mutant is caught when at least one of your tests fails against it.
You pass when your suite is green on the correct code and catches at least 5 of the 6 mutants. The mutants skip retries, make one too many or too few attempts, retry an unexpected exception, swallow the final transient failure, or keep calling after success. A happy-path result assertion catches almost none of them. Control what the delegate does on each call, and assert both the result or exception and the exact call count. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `RetryExecutor` class. Each public, parameterless method is one test; it
// passes if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy
// versions. A returned value alone says nothing about retries, attempt counts, or exception policy.
public class Tests
{
public void A_successful_operation_returns_its_value()
{
Assert.Equal(42, new RetryExecutor().Execute(() => 42, 3));
}
// TODO: use captured call counters to test retries, exhaustion, unexpected failures, and when
// the executor stops calling the operation.
}How it is graded
You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.
Related guides
What it practices
- Error path testing
- Prove exceptions, retries and rejected inputs behave as specified.
- Test doubles
- Use fakes, stubs and recording collaborators to isolate and observe a unit.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- Test the CSV Record Parser Hard Free You're given a correct CSV record parser. Write tests that pin quoting, empty fields, whitespace, and malformed input.
- Test the RPN Calculator Hard Free You're given a correct reverse-Polish evaluator. Write the tests that pin operand order and the error cases.
- Test the Checkout Service Medium Free You're given a correct checkout service with three collaborators. Write recording fakes that prove what it does to each one, and in what order.
- Test the Bowling Scorer Hard Free You're given a correct ten-pin bowling scorer. Write the tests that exercise spares, strikes, and the bonus rolls.