Skip to content
Katabench
Try free

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:

C#
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:

  • Reserve holds quantity units for the order until now + holdDuration and returns true. It returns false and changes nothing when the order already has an active hold, or when fewer than quantity units are available. Reserving exactly the available quantity succeeds.
  • A hold is active while now is 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.
  • Release gives an active hold's units back and returns true. It returns false when the order has no active hold.
  • Confirm turns an active hold into a sale: the hold ends and its units leave OnHand for good. It returns false, and sells nothing, when the order has no active hold, including one that has expired.
  • Available(now) is always OnHand minus the quantities of the holds still active at now.
  • A negative onHand, a holdDuration of zero or less, or a quantity of zero or less throws ArgumentOutOfRangeException.
C#
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

C#
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.

C# unit testing exercises →

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