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:

json224 B
{
  "revision": 2,
  "turn": "red",
  "pieces": {
    "red7": {
      "color": "red",
      "square": 15,
      "king": false
    },
    "black1": {
      "color": "black",
      "square": 4,
      "king": false
    }
  }
}

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)
}