21/28
Part 5 of 6 · The worked exampleLearn and do

Trace the reported trap, then write the test that catches it

One candidate lost the round on one rule, with the tests in front of them (1 report). So when you finish a level, trace it by hand with a small case, out loud, before you run anything.

Here's the trace for the retry rule. Three tasks, two workers:

StepCallWaiting, in orderReserved
1enqueue a, b, ca, b, c
2reserve("w1") gives ab, ca by w1
3reserve("w2") gives bca by w1, b by w2
4fail("w1", "a")a, cb by w2
5reserve("w3") gives ac

Step 4 is the trap. A version that appends a gets c at step 5.

Then turn the trace into a test. A good test for this rule needs a task enqueued after the failed one to still be waiting. With only a and b, there's nothing for a to wrongly fall behind, and the broken version passes. This is the habit that proves a test: break the code on purpose and check that the test fails. The practice problem's tests were checked this way, against fourteen broken versions.

Predict

A test enqueues a and b, reserves both, fails a, and checks that the next reserve gives a. Does it catch a queue that puts a failed task at the back?

Pick an answer above to see the rest