CoursesBackend engineering from first request to production

Puzzle 2 · 3 of 18

Can two clients prove the rule?

Test a retry through the running service and read what survived.

All puzzles in this course

The problem

A test double standing in for the database can return success without writing anything. How would a reviewer know your server actually preserved the bookmark?

See it

Can two clients prove the rule? Two callers Same key Restart Read river

Scroll sideways to see the full diagram.

  1. Two workers submit river.

    Step 1 of 4
  2. The database admits one new row.

    Step 2 of 4
  3. A fresh process opens the same file.

    Step 3 of 4
  4. The test checks the actual list.

    Step 4 of 4

The building block

An integration test checks several real components together. Here the client, server and database all run. Run the supplied test. It creates an empty database, starts the server and sends two writes with the same name from separate workers. It expects one creation and one existing result in either order. It stops the process, starts another against the same file and reads back the list. This distinguishes a durable write from a value held only in memory. Two workers exercise contention without proving correctness for every possible ordering. The unique key provides the underlying guarantee.

Design a test that catches a believable bug

Suppose deletion removes river from a process's working copy and replies 204. An immediate read sees an empty list. An implementation that forgot to change the database passes both checks. Restart the process against the same database and read again: river reappears. Now try the correct implementation and predict the second delete. The row remains absent and the fixture returns 404. Record responses and final rows separately. A test that recreates the database between the two processes also loses the evidence it meant to inspect.

Try it

Run the browser experiment

Optional local lab

Complete the deletion assignment in the lab. Show that removing the row survives a restart and that a second delete follows your documented contract.

Open the local service source files

Check yourself

Both calls return a success status. What evidence is still missing?

Not quite. Start order cannot establish persistence. Re-read that part ↑

Right. An integration test can check replies, stored state and persistence across a fresh process.

Why reuse the database file across the restart?

Not quite. The new process must reconstruct the earlier durable state. Re-read that part ↑

Right. The new process must reconstruct the earlier durable state.

Two racing inserts finish in a different order on another run. Which expectation stays valid?

Right. Their order may vary while the unique key still holds.

Not quite. Their order may vary while the unique key still holds. Re-read that part ↑

Sources

What you now know

An integration test can check replies, stored state and persistence across a fresh process.