Concurrency and races · Chapter 14 of 15
Events out of order
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.
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: show out for delivery after delivery, on purpose
Write reproduce() in repro.js. Make a view with createOrderView({ notify }) from order-view.js, with a notify that does nothing. Make the four events of one order, each { orderId, seq, status, at }: seq 1 'paid', seq 2 'cooking', seq 3 'out for delivery', seq 4 'delivered', with any orderId and an at in ms that rises with seq. Apply them with view.apply in the order the queue handed them over, seq 1, 2, 4, 3, and resolve to view.status(orderId).
The check runs your reproduce() twice: beside the original order-view.js it must resolve to 'out for delivery' (the late event won), and beside a fixed order-view.js it must resolve to 'delivered'. No real waiting: you are the queue, so the arrival order is yours, and reproduce() must finish within 2 seconds.
- Task 2
order-view.js: the status never goes back
Fix createOrderView({ notify }) in order-view.js so an event older than the one already shown for its order changes nothing. A newer event (a bigger seq than any applied for that order) changes the status, logs order <id>: <status> and calls notify(orderId, status) once. An older event, or the same seq again, keeps the status, calls no notify and logs order <id>: dropped event <seq> (<status>), already at <last seq>.
Each order keeps its own last seq: events of one order never drop another order's. Keep the applied log line and status(orderId) as they are. The check applies every arrival order of four events, duplicates, two orders mixed, and events from two machines whose clocks disagree by 3 minutes, so comparing the events' at does not work.
- Task 3
order-view.test.js: a test that would have caught it
Write a vitest test file, order-view.test.js, that would have caught the late event. The check runs your file three times: beside the original order-view.js, where at least one test must fail; beside the fixed order-view.js, where every test must pass; and beside a wrong fix that keeps the event with the latest at, where at least one test must fail.
So test both sides: events of one order applied out of order end on the newest status, with no notify for the late one; and events applied in order whose at disagrees with seq (the rider app's clock 3 minutes behind the kitchen's) each change the status. Each test must finish within 3 seconds.
What you will learn
- repro.js: show out for delivery after delivery, on purpose
- order-view.js: the status never goes back
- order-view.test.js: a test that would have caught it
More in Time
Build "Events out of order" 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.