Workbench / The TiffinBox Build
S39 · V3·38
Two Kinds of Time
Volume 3, chapter 38. Stage 38 put a fixed clock into a server test, which showed what else read that clock: the deadline that stops a client trickling its request one byte at a time. With the clock fixed, or stepped back an hour by NTP, a 62-byte head at one byte per 100 ms got through in 6.2 s and was answered; the deadline was 800 ms. This stage moves deadlines to elapsed time (System.nanoTime, compared by subtraction), and adds a concurrency test: 320 orders placed and moved at once while a display follows the stream, checked against the queue's own invariants. Run against a server with one lock removed, it failed every time.
Stage 39: two kinds of time
The brief
What you'll build.
- Deadlines on elapsed time:
DeadlineInputStreamtakes a deadline innanoTimeand aLongSupplierticker, and compares by subtraction; the server builds it fromSystem.nanoTime(), not itsClock; ConcurrencyTest: 16 customers place 20 orders each while 4 kitchen threads accept, finish and hand them over, and a display reads the kitchen's event stream. Afterwards it checks lifecycle order per order, the display's last word against the server's, the queue's contents and its internal consistency;- tests:
aSlowClientIsCutOffWhateverTheServersClock,aDeadlineSurvivesTheCounterWrapping, and the concurrency test; - a mutant run: the concurrency test against a server without
queuesLock, to see it fail.
Why a real company needs this. A frozen or adjustable clock in staging, a test or a replay is normal; a deadline that silently stops working there turns any slow client into a held connection. And concurrency code needs a test that runs real interleavings and checks invariants, or the first test of the locking is production.
Why now. Chapter 38 is concurrency: threads and executors, the memory model, locks and atomics, virtual threads and structured concurrency, deadlock, starvation and contention, and how to test. TiffinBox's server is already concurrent (a virtual thread per connection, striped locks, a log writer thread); this stage tests it as such.
What you'll have at the end.
- deadlines that hold under any business clock;
- a concurrency test that is known to fail when a lock is missing;
- ADR-0041: two kinds of time, and how concurrency is tested.
Theory to own
- Wall-clock against monotonic time. Which questions does each answer? (The chapter's exercises, "Time, clocks and concurrency"; Companion, Gaps 1.)
- Comparing
nanoTimevalues. Whya - b > 0and nota > b? (System.nanoTimedocumentation.) - Invariants under concurrency. What should be true of the kitchen queue whatever the interleaving? (Stage 36; Companion, Gaps 3.)
- Detecting deadlock among virtual threads. Would the JVM tell you? (Companion, Gaps 2.)
Build it
Step 1: trickle a request under three clocks
Write exp/39/Drip.java: a server with an 800 ms head deadline and a client sending a 62-byte request head one byte every 100 ms, under the system clock, a fixed clock, and a clock that steps back an hour after the third byte.
Checkpoint. exp/39/drip-before.out.
Step 2: deadlines on nanoTime
Change DeadlineInputStream to take long deadlineNanos and a LongSupplier ticker, failing when ticker - deadline > 0; build it in serve from System.nanoTime(). Add the two tests.
Checkpoint. exp/39/drip-after.out; exp/39/wrap.out; 45 server tests.
Step 3: a concurrency test
Write exp/39/Stress.java, then ConcurrencyTest: customers on virtual threads post orders; kitchen threads take each order id from the display's stream and post accept 10, ready and handover, stopping at random to leave some open; the display records every order event. Check the invariants afterwards, and make KitchenQueue.consistent() public for it.
Checkpoint. exp/39/stress.out; 46 server tests.
Step 4: make it fail once
Copy Server.java with queuesLock.lock() and unlock() removed into exp/39/mutant, compile it with the rest of the server, and run the stress test four times.
Checkpoint. exp/39/mutant-nolock.out.
Senior and architect lens
1. Name the kind of time in the type. Instant and Clock for business time; a long of nanoseconds and a LongSupplier ticker for elapsed time. Mixing them is what happened here, and the types made it easy (both "now").
2. ADR-0041: two kinds of time; testing concurrency. Write it with these points:
- Business time: from the server's
Clock(stage 38), fixed in tests and replays. - Elapsed time: deadlines, timeouts and measurements from
System.nanoTime(), compared by subtraction; never from theClock. - Concurrency tests: a load test with real interleavings, invariant checks afterwards, request timeouts as the deadlock detector, and a recorded run against a mutant.
- Locks: stripes per order and customer, one lock for the queues, never nested except customer then queues; reviewed for order whenever a new lock is added.
3. A test you haven't seen fail is a hypothesis. The stress test passing on the real code was evidence only after it failed on the mutant. The same applies to monitoring alerts and health checks.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
timeouts in other places (log append's 10 s get, SSE 15 s ping) |
they use library timed waits, already monotonic; but none is tested under load | stage 53 (resilience: timeouts and budgets) |
| a deadlock among virtual threads in production | no JVM report; requests hang | stage 42 (performance: JFR), stage 53 (resilience: request timeouts) |
| more locks as features grow | lock-order mistakes | stage 44 (build systems: a lock-order check), ADR-0041 review |
| a 2-core CI machine | interleavings differ from production | stage 55 (backend testing: repeated runs, jcstress for the queue) |
Evidence
exp/39/drip-before.out(6.2 s, answered) anddrip-after.out(cut off in about 1 s) under all three clocks.exp/39/wrap.out;aDeadlineSurvivesTheCounterWrappingpasses.exp/39/stress.outall zeros;exp/39/mutant-nolock.outshows the test failing withoutqueuesLock.- core 51, server 46 tests;
golden master: 47 exchanges, identical; the suite passes 44. - ADR-0041 is committed.
Carry forward
System.nanoTimefor deadlines, compared by subtraction, everywhere a timeout is computed.ConcurrencyTestgrows with every new concurrent feature (stage 52's background jobs, stage 53's timeouts).- Mutant runs for any new locking, recorded in
exp/.