Learn
Your first model
Find a lost update, inspect its trace, and change the design.
No installation or P experience is needed. You will use the browser Playground.
Run the broken design
Open the lost-update model. Press Check to run its test. Two clients each increment a counter. The assertion expects 2, but an execution can leave 1. Read the failing assertion and inspect the saved trace before changing the code.
Read the model
Each machine is a state machine with an inbox. send puts an event in another machine's inbox, and the checker decides when it's delivered. Here the Counter serves reads and writes, and each Client reads, then writes back the value plus one:
machine Counter {
// P initializes integers to 0. Each machine owns its own variables.
var value: int;
start state Serving {
on ReadValue do (client: Client) {
send client, ValueRead, value;
}
// This overwrites the value; it does not add to it.
on WriteValue do (newValue: int) {
value = newValue;
}
on ReadFinalValue do (testHarness: Main) {
send testHarness, ValueRead, value;
}
}
}
// Each client reads once, then writes back the value it saw plus one.
// Reading and writing are separate scheduler steps: the read may become stale.
machine Client {
var counter: Counter;
var owner: Main;
var rememberedValue: int;
start state Reading {
entry (input: (counter: Counter, owner: Main)) {
counter = input.counter;
owner = input.owner;
send counter, ReadValue, this;
}
on ValueRead do (replyValue: int) {
rememberedValue = replyValue;
goto Writing;
}
}
state Writing {
entry {
// Another client may have read the same value before this write arrives.
send counter, WriteValue, rememberedValue + 1;
send owner, ClientFinished;
goto Finished;
}
}
state Finished { }
}The test machine, Main, starts the Counter and two Clients. Once both Clients are done, it asserts that the counter reached 2:
assert finalValue == 2, format("expected 2 increments, counter is {0}", finalValue);
test tcTwoClients [main=Main]: { Main, Counter, Client };If you have installed the CLI and saved the Playground file as LostUpdate.p, the equivalent check reports the same failure, among other lines (a full sample is in the reference):
$ snappea check LostUpdate.p tcTwoClients
FAIL tcTwoClients (bug found after ...)
Assertion Failed: LostUpdate.p:85:7 expected 2 increments, counter is 1
Replay: snappea replay PCheckerOutput/tcTwoClients/bug.schedule LostUpdate.pThe trace shows exactly how the update was lost:
1/10 Main creates the Counter and two Clients. The counter starts at 0.
The fix is a design change: replace the read and write with one Increment event that the Counter applies itself. For this small model, automatic exhaustive exploration can finish and find no bug. Read the result to confirm whether it completed.
on Increment do { value = value + 1; }Check the fix
In the Playground, change the Counter to handle one increment event. Change each Client to send that event instead of reading and writing. Keep the final assertion: two completed increments must leave 2. Declare the new event and remove unused read/write declarations. Run the checker again.
event Increment;
// Counter.Serving: replace on ReadValue and on WritValueRead with:
on Increment do { value = value + 1; }
// Client.Run: replace its entry with:
entry (input: (counter: Counter, owner: Main)) {
send input.counter, Increment;
send input.owner, ClientFinished;
}Keep ValueRead and the Counter's ReadFinalValue handler: Main still uses them to verify the final value.
The fixed design should pass. Read the result to see whether exploration completed or reached its budget. A pass checks this bounded model, not every possible implementation.
You have changed the design, not just the test order. Next, use the agent workflow to carry a model's rules into application tests.