Redis · Chapter 24 of 26

Testing Redis code

By the end of this chapter you can test code that uses Redis against a real Redis, one fresh server a test file, test time without waiting for it, test races by making them happen, test what code does rather than how, and say when a mock client is enough and when it is not. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Tested
  • 4 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

GullyXI now has nineteen chapters of Redis code, and every one of them has a way to be subtly wrong: a limit off by one, a key that never expires, a lock that anyone can release, a cache that stampedes. Each of those bugs passes a quick try in the terminal. Venkat found the last one himself: after a change to sessions, sessions stopped expiring, and phones that should have been logged out after half an hour stayed logged in for days. The change had a test, and the test passed.

Rhea wants every piece that guards money or match night to have tests that would have caught it. Farhan asks one thing of a test: that it fails when the code is wrong. A test that passes on broken code is worse than none, because it tells you all is well.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    limiter.test.js: tests that catch two rate-limiter bugs

    Write vitest tests in limiter.test.js for allowLogin(redis, fanId, now) from limiter.js: at most 5 logins a fan in each minute (the minute is now / 60000 rounded down), counted in login:<fanId>:<minute>, which expires 60 seconds after a login. Use a real Redis from startRedis in gullyxi/testing, as the starter does.

    The Check runs your tests on two correct limiters, the chapter's and one written as a Lua script (all your tests must pass on both), then on two broken ones, each of which must make at least one test fail: one that allows a sixth login in a minute, and one whose count never expires.

  2. Task 2

    lock.test.js: a test for the lock

    Write vitest tests in lock.test.js for acquire(redis, name, ttlMs) and release(redis, name, token) from lock.js: acquire takes lock:<name> for ttlMs and returns a random token, or null when someone else holds it; release frees it only for the holder's token and returns true, or false.

    The Check runs your tests on two correct locks, the chapter's and one that releases with WATCH (all must pass), then on three broken ones, each of which must make a test fail: one whose release is a plain DEL whoever holds the lock, one whose SET has no NX so every worker gets it, and one whose SET has no PX so it never expires.

  3. Task 3

    stampede.test.js: a test for the stampede

    Write vitest tests in stampede.test.js for getMatchCentre(redis, db, matchId) from match-centre.js: page:match:<matchId> from Redis, or from db.getMatchCentre saved with EX 30, and misses at the same moment share one database call for that match.

    The Check runs your tests on two correct versions (all must pass), then on three broken ones, each of which must make a test fail: one that sends every miss to the database, one that shares a single call between different matches, and one that caches the page for ever.

  4. Task 4 · interview question

    Interview: session.test.js, a mock or a real Redis

    Write vitest tests in session.test.js, against a real Redis, for session.js: createSession(redis, fanId, now) stores JSON { fan, created } under session:<id> with EX 1800 and returns the id; getSession(redis, id) returns the object or null; updateSession(redis, id, changes) merges the changes in, keeps the TTL and returns true, or returns false and creates nothing when the session does not exist. The starter's mock test passes on the bug below; you may keep it or delete it.

    The Check runs your tests on two correct versions (all must pass), then on two broken ones, each of which must make a test fail: one whose update drops the TTL (a SET without KEEPTTL), and one that makes a session out of nothing for a missing id.

    Farhan will ask: a mock or a real Redis for these tests, and what can a mock never tell you?

What you will learn

  • A real Redis per test file: startRedis from gullyxi/testing, FLUSHALL before each test.
  • Time: pass now, read TTLs, never sleep; fake timers do not move Redis's clock.
  • Races: make them happen with Promise.all.
  • Behaviour, not implementation: tests that pass on any correct version.
  • A mock or a real Redis, and what a mock can never tell you.

More in Tested

Build "Testing Redis code" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.