Test the Stock Reservations
Hard About 50 min Free, no account needed
You're given a correct stock ledger whose holds expire. Write the sequences that prove no unit is sold twice or stranded on an expired hold.
The brief
When a shopper starts checkout, the store holds their units for a few minutes so nobody else can buy them. If the shopper pays, the hold becomes a sale. If they give up, the hold is released or simply expires and the units go back on the shelf. Two classic bugs follow: the same unit gets sold twice, or units stay stuck on a hold that should have expired. The ledger here is already written and correct. Your job is to write the tests that prove neither can happen.
Here is the public API:
public class StockReservations
{
public StockReservations(int onHand, long holdDuration) { ... }
public int OnHand { get; } // units in the warehouse
public int Available(long now) { ... } // units free to reserve at `now`
public bool Reserve(string orderId, int quantity, long now) { ... }
public bool Release(string orderId, long now) { ... }
public bool Confirm(string orderId, long now) { ... }
}Time is a plain number you pass in (think seconds). Calls come in order with now never going
backwards. The rules:
Reserveholdsquantityunits for the order untilnow + holdDurationand returns true. It returns false and changes nothing when the order already has an active hold, or when fewer thanquantityunits are available. Reserving exactly the available quantity succeeds.- A hold is active while
nowis earlier than its expiry. At exactly the expiry time it is over, and its units count as available again with no call needed to free them. Once an order's hold has expired, that order may reserve again. Releasegives an active hold's units back and returns true. It returns false when the order has no active hold.Confirmturns an active hold into a sale: the hold ends and its units leaveOnHandfor good. It returns false, and sells nothing, when the order has no active hold, including one that has expired.Available(now)is alwaysOnHandminus the quantities of the holds still active atnow.- A negative
onHand, aholdDurationof zero or less, or aquantityof zero or less throwsArgumentOutOfRangeException.
var stock = new StockReservations(onHand: 10, holdDuration: 15);
Assert.True(stock.Reserve("order-1", 3, now: 0));
Assert.Equal(7, stock.Available(0));Write a Tests class. Every public, parameterless method is a test; a test passes if it does not
throw. Use Assert.Equal, True, False, and Throws<TException> as needed (no using needed
for Assert). Your suite must pass on the correct StockReservations and catch every hidden
mutant, a planted-bug version you never see.
Starter code
public class Tests
{
public void A_reservation_holds_units()
{
var stock = new StockReservations(onHand: 10, holdDuration: 15);
Assert.True(stock.Reserve("order-1", 2, now: 0));
Assert.Equal(8, stock.Available(0));
}
}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.
What it practices
- Stateful testing
- Drive call sequences and state-machine transitions, not just single calls.
- Boundary value testing
- Pin behavior exactly at, just below and just above every boundary.
Related katas
- Test Fenced Lease Ownership Hard Free Exercise expiry, renewal, ownership transfer, and stale-token rejection in a lease that must never reuse authority.
- Test the Sliding-Window Rate Limiter Hard Free You're given a correct sliding-window rate limiter. Write the call sequences that prove the window slides and the limit holds.
- Test Stable Priority Ordering Medium Free Write operation sequences that distinguish priority order, FIFO ties, peeking, and queue-instance isolation.
- Test the Cart Discount Rules Hard Free You're given a correct cart pricer that stacks a volume discount, a coupon, and free shipping. Build the carts where a wrong rule order or a wrong rounding step changes the total.