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:
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
twith time to livettlis live while the clock reads earlier thant + ttl. At exactlyt + ttlit is gone:TryGetreturns false. - The time to live is measured from the moment of the
Setcall. - 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.
Countincludes only live entries, whether or not anyone has read the expired ones yet.ttlmust be positive. Zero or negative throwsArgumentOutOfRangeException.
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
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.
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
- Test the Caesar Cipher Medium Free You're given a correct Caesar cipher. Write the tests that pin wraparound, case, non-letters, and shift normalization.
- Test the Password Policy Medium Free You're given a correct password validator with four rules. Write tests that isolate each rule so a dropped one is caught.
- Test Stable Priority Ordering Medium Free Write operation sequences that distinguish priority order, FIFO ties, peeking, and queue-instance isolation.
- Test the Triangle Classifier Medium Free You're given a correct triangle classifier. Write the tests that cover every shape and the invalid cases.