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:

TRON302 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0044C 6B696E67txt "king"
00901bit false
00A0F 0A 04000000 0900 0000map leaf, size 10: @4 → @9
0146C 737175617265txt "square"
01B02 0F000000000000 00i64 15
0240F 0A 14000000 1B00 0000map leaf, size 10: @14 → @1B
02E5C 636F6C6F72txt "color"
0343C 726564txt "red"
0380F 0A 2E000000 3400 0000map leaf, size 10: @2E → @34
04207 12 08030000 0A00 0000 24000000 3800 0000map branch, size 18, depth 0 (root), slots 3, 8, 9: @A → @24 → @38
0544C 6B696E67txt "king"
05901bit false
05A0F 0A 54000000 5900 0000map leaf, size 10: @54 → @59
0646C 737175617265txt "square"
06B02 04000000000000 00i64 4
0740F 0A 64000000 6B00 0000map leaf, size 10: @64 → @6B
07E5C 636F6C6F72txt "color"
0845C 626C61636Btxt "black"
08A0F 0A 7E000000 8400 0000map leaf, size 10: @7E → @84
09407 12 08030000 5A00 0000 74000000 8A00 0000map branch, size 18, depth 0 (root), slots 3, 8, 9: @5A → @74 → @8A
0A64C 72656437txt "red7"
0AB0F 0A A6000000 4200 0000map leaf, size 10: @A6 → @42
0B56C 626C61636B31txt "black1"
0BC0F 0A B5000000 9400 0000map leaf, size 10: @B5 → @94
0C607 0E 08010000 AB00 0000 BC000000map branch, size 14, depth 0 (root), slots 3, 8: @AB → @BC
0D48C 7265766973696F 6Etxt "revision"
0DD02 02000000000000 00i64 2
0E60F 0A D4000000 DD00 0000map leaf, size 10: @D4 → @DD
0F06C 706965636573txt "pieces"
0F70F 0A F0000000 C600 0000map leaf, size 10: @F0 → @C6
1014C 7475726Etxt "turn"
1063C 726564txt "red"
10A0F 0A 01010000 0601 0000map leaf, size 10: @101 → @106
11407 12 00850000 E600 0000 F7000000 0A01 0000map branch, size 18, depth 0 (root), slots 8, 10, 15: @E6 → @F7 → @10A

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

go858 B
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))
}
go1.8 kB
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)
}