Concurrency and races · Chapter 9 of 15
The job that outlasted its interval
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Time
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.
Why you need this
A two-second run never reaches a one-minute interval, so the runs never overlapped. The bug was there from the first day, waiting for a slow bank. Races that need something to be slow are found in production on the worst evening, unless a test makes it slow on purpose.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: overlap the payout runs on purpose
Write reproduce() in repro.js. Make a pretend clock with createClock() from fake-clock.js, and a runOnce that stands in for a run on a slow bank day: it adds one to a count of runs in progress, keeps the most there ever were at once, and then waits for a promise that never settles. Start the job with startPayouts(runOnce, { every: 60_000, clock }) from payout-job.js, move the clock on 3 minutes (await clock.tick(180_000)), stop the job with the function startPayouts returned, and resolve to the most runs there were at once.
The check runs your reproduce() twice: beside the original payout-job.js it must resolve to 3 (three runs paying at the same time), and beside a fixed payout-job.js it must resolve to 1. No real waiting: the pretend clock moves at once, and reproduce() must finish within 2 seconds.
- Task 2
payout-job.js: never two runs at once
Fix startPayouts(runOnce, { every, clock }) in payout-job.js so runs never overlap. The first run starts every ms after the start. Each next run starts every ms after the last run finished, whether it worked or failed, and never while a run is still going. A run that works logs payouts: run finished, <paid> riders paid; a run that fails logs payouts: run failed, <message>, and the job carries on. stop() stops the job: no new run starts after it, even when a run that was in flight finishes later, and a job stopped before its first run never runs.
Keep the two log lines, the arguments and the real clock as the default; use only the clock's functions for timers. The check holds runs back on its own pretend clock, makes one fail, and stops the job with a run in flight.
- Task 3
payout-job.test.js: a test that would have caught it
Write a vitest test file, payout-job.test.js, that would have caught this bug. The check runs your file three times: beside the original payout-job.js, where at least one test must fail; beside the fixed payout-job.js, where every test must pass; and beside a wrong fix whose running flag stays set after a failed run (so the job never runs again), where at least one test must fail.
So use the pretend clock from fake-clock.js and test both sides: with a run held back, moving the clock several intervals starts no second run; and after a run fails, the next run still starts every ms later. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.js: overlap the payout runs on purpose
- payout-job.js: never two runs at once
- payout-job.test.js: a test that would have caught it
More in Time
Build The job that outlasted its interval yourself
Tapri's backend grew busy, and Kabir keeps handing you incidents that only happen when two things run at once. Each is a race interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.