Skip to content
Katabench
Try free

Test the Expiring Cache

Medium About 30 min Free, no account needed

You're given a correct time-to-live cache that reads an injected TimeProvider. Write a clock you can move by hand and the tests that pin every expiry edge.

The brief

A session cache keeps each entry for a fixed time to live, then forgets it. Code like this is hard to test against the real clock: you cannot wait ten minutes in a unit test, and sleeping makes a suite slow and flaky. So this cache never asks the system for the time. It reads an injected TimeProvider, the clock abstraction built into .NET, which does the same job as a hand-rolled IClock interface. The cache is already written and correct. Your job is to write the tests that prove it, driving time with a clock you control.

Here is the public API:

C#
public class ExpiringCache
{
    public ExpiringCache(TimeProvider clock) { ... }

    // The number of entries that have not expired yet.
    public int Count { get; }

    // Stores value until clock.GetUtcNow() + ttl.
    public void Set(string key, string value, TimeSpan ttl) { ... }

    // True, with the stored value, while the entry is live.
    public bool TryGet(string key, out string value) { ... }
}

The rules, all measured on the clock the cache was given:

  • An entry set at time t with time to live ttl is live while the clock reads earlier than t + ttl. At exactly t + ttl it is gone: TryGet returns false.
  • The time to live is measured from the moment of the Set call.
  • Expiry is absolute. Reading an entry never extends its life.
  • Setting a key that already exists replaces its value and restarts its time to live.
  • Count includes only live entries, whether or not anyone has read the expired ones yet.
  • ttl must be positive. Zero or negative throws ArgumentOutOfRangeException.

Write a Tests class. Every public, parameterless method is a test; a test passes if it does not throw. Helper classes can sit next to it in the same file. Use Assert.Equal, True, False, and Throws<TException> as needed (no using needed for Assert, though TimeProvider, TimeSpan, and DateTimeOffset live in System). Never sleep. Your suite must pass on the correct ExpiringCache and catch every hidden mutant, a planted-bug version you never see.

Starter code

C#
using System;

public class Tests
{
    public void A_fresh_entry_can_be_read()
    {
        var cache = new ExpiringCache(TimeProvider.System);
        cache.Set("session:42", "alice", TimeSpan.FromMinutes(5));

        Assert.True(cache.TryGet("session:42", out var value));
        Assert.Equal("alice", value);
    }
}

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 →

Related guides

What it practices

Controlling time in tests
Inject a clock or TimeProvider so expiry and windows are tested deterministically.
Boundary value testing
Pin behavior exactly at, just below and just above every boundary.

Related katas