Recover the board and close owners
Bob leaves the pending revision-two cues unread. He unsubscribes, then queries the board and gets the complete current state. This is the same recovery path a reconnecting player can use. Hand cursors can start again from fresh publications.
The example checks this final board:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 4C 6B696E67 | txt "king" |
| 009 | 01 | bit false |
| 00A | 0F 0A 04000000 0900 0000 | map leaf, size 10: @4 → @9 |
| 014 | 6C 737175617265 | txt "square" |
| 01B | 02 0F000000000000 00 | i64 15 |
| 024 | 0F 0A 14000000 1B00 0000 | map leaf, size 10: @14 → @1B |
| 02E | 5C 636F6C6F72 | txt "color" |
| 034 | 3C 726564 | txt "red" |
| 038 | 0F 0A 2E000000 3400 0000 | map leaf, size 10: @2E → @34 |
| 042 | 07 12 08030000 0A00 0000 24000000 3800 0000 | map branch, size 18, depth 0 (root), slots 3, 8, 9: @A → @24 → @38 |
| 054 | 4C 6B696E67 | txt "king" |
| 059 | 01 | bit false |
| 05A | 0F 0A 54000000 5900 0000 | map leaf, size 10: @54 → @59 |
| 064 | 6C 737175617265 | txt "square" |
| 06B | 02 04000000000000 00 | i64 4 |
| 074 | 0F 0A 64000000 6B00 0000 | map leaf, size 10: @64 → @6B |
| 07E | 5C 636F6C6F72 | txt "color" |
| 084 | 5C 626C61636B | txt "black" |
| 08A | 0F 0A 7E000000 8400 0000 | map leaf, size 10: @7E → @84 |
| 094 | 07 12 08030000 5A00 0000 74000000 8A00 0000 | map branch, size 18, depth 0 (root), slots 3, 8, 9: @5A → @74 → @8A |
| 0A6 | 4C 72656437 | txt "red7" |
| 0AB | 0F 0A A6000000 4200 0000 | map leaf, size 10: @A6 → @42 |
| 0B5 | 6C 626C61636B31 | txt "black1" |
| 0BC | 0F 0A B5000000 9400 0000 | map leaf, size 10: @B5 → @94 |
| 0C6 | 07 0E 08010000 AB00 0000 BC000000 | map branch, size 14, depth 0 (root), slots 3, 8: @AB → @BC |
| 0D4 | 8C 7265766973696F 6E | txt "revision" |
| 0DD | 02 02000000000000 00 | i64 2 |
| 0E6 | 0F 0A D4000000 DD00 0000 | map leaf, size 10: @D4 → @DD |
| 0F0 | 6C 706965636573 | txt "pieces" |
| 0F7 | 0F 0A F0000000 C600 0000 | map leaf, size 10: @F0 → @C6 |
| 101 | 4C 7475726E | txt "turn" |
| 106 | 3C 726564 | txt "red" |
| 10A | 0F 0A 01010000 0601 0000 | map leaf, size 10: @101 → @106 |
| 114 | 07 12 00850000 E600 0000 F7000000 0A01 0000 | map branch, size 18, depth 0 (root), slots 8, 10, 15: @E6 → @F7 → @10A |
| 126 | 14010000 00000000 | root → @114; previous: none |
Closing the owners releases their subscriptions and registrations and cancels their remaining exchanges. The example checks that all plugin resource counts are zero. Each language version also closes owners if an earlier operation fails. Its scope or cleanup block releases database and document handles on either path.
Run the complete progression
The entry point runs the steps in order against one database. Every chapter above displays code taken from that executable program. Run the downloaded program to check the assertions, then change a landing or expected revision to explore the stored mutations.
Use the Events reference for operation contracts, queue limits and cancellation behavior. Read Event coordination for the separation between stored facts and temporary messages. A durable work queue needs stored work records and a recovery policy beyond these temporary exchanges.
Code
func recoverAndClose(
game *Game,
bob *events.Owner,
subscription *events.Subscription[events.CheckersTableCursorData],
expected map[string]any,
) {
check(game.t, subscription.Close())
if !reflect.DeepEqual(game.board(), expected) {
game.t.Fatal("recovered board is incorrect")
}
game.close()
status, err := game.db.Events().Status()
if err != nil {
game.t.Fatal(err)
}
defer status.Close()
counters, err := stardust.Decode[map[string]int64](status)
if err != nil {
game.t.Fatal(err)
}
for name := range counters {
value := counters[name]
if value != 0 {
game.t.Fatal("event state leaked")
}
}
document, err := stardust.Encode(expected)
if err != nil {
game.t.Fatal(err)
}
defer document.Close()
output, err := document.Serialize(stardust.DUST, 0)
if err != nil {
game.t.Fatal(err)
}
fmt.Print(string(output))
}func TestCheckers(t *testing.T) {
if err := stardust.Activate(os.Getenv("STARDUST_TEST_LICENSE")); err != nil {
t.Fatal(err)
}
source := os.Getenv("CHECKERS_FIXTURE")
if source == "" {
source = "../checkers.dust"
}
bytes, err := os.ReadFile(source)
if err != nil {
t.Fatal(err)
}
formats := map[string]stardust.Format{
"dust": stardust.DUST,
"json": stardust.JSON,
"edn": stardust.EDN,
"tron": stardust.TRON,
}
formatName := os.Getenv("CHECKERS_FORMAT")
if formatName == "" {
formatName = "dust"
}
format, found := formats[formatName]
if !found {
t.Fatal("choose dust, json, edn or tron")
}
document, err := stardust.Parse(bytes, format)
if err != nil {
t.Fatal(err)
}
defer document.Close()
fixture, err := stardust.Decode[struct {
Seed map[string]any `json:"seed"`
Definitions map[string]map[string]any `json:"definitions"`
Expected map[string]any `json:"expected"`
}](document)
if err != nil {
t.Fatal(err)
}
db, err := openCheckers(filepath.Join(t.TempDir(), "checkers.db"), filepath.Join(t.TempDir(), "sdk"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
game := &Game{t: t, db: db}
defer game.close()
if _, err := stardust.MergePatchValue(db, fixture.Seed, nil); err != nil {
t.Fatal(err)
}
for kind, definitions := range fixture.Definitions {
for path, definition := range definitions {
if _, err := stardust.PatchDefinitionValue(db, kind, path, definition); err != nil {
t.Fatal(err)
}
}
}
alice, bob, rules := game.owner(), game.owner(), game.owner()
subscription := shareCursors(game, alice, bob)
moveOnePiece(game, alice, bob, rules, subscription)
listLegalMoves(game, bob, rules)
makeTwoJumps(game, bob, rules)
previewADrag(game, alice, rules)
recoverAndClose(game, bob, subscription, fixture.Expected)
}