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:

edn238 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

python584 B
async def recover_and_close(game, bob, subscription, expected):
    # A reconnect reads authoritative facts instead of replaying missed cues.
    await subscription.close()
    assert await game.board() == expected
    await game.close()
    async with await game.db.events().status() as status:
        assert await sd.decode(status) == {
            "owners": 0,
            "subscriptions": 0,
            "registrations": 0,
            "rpcs": 0,
            "queued": 0,
        }
    async with await sd.encode(expected) as result:
        print(await result.serialize("dust"))
python1.2 kB
async def main():
    source, format_ = sys.argv[1:3]
    await sd.activate(os.environ["STARDUST_TEST_LICENSE"].strip())
    async with await sd.parse(Path(source).read_bytes(), format_) as document:
        fixture = await sd.decode(document)
    with tempfile.TemporaryDirectory(prefix="stardust-checkers-") as directory:
        async with await open_checkers(
            str(Path(directory) / "checkers.db"), str(Path(directory) / "sdk")
        ) as db:
            game = Game(db)
            try:
                await prepare(game, fixture)
                alice, bob, rules = await game.owner(), await game.owner(), await game.owner()
                subscription = await share_cursors(game, alice, bob)
                await move_one_piece(game, alice, bob, rules, subscription)
                await list_legal_moves(game, bob, rules)
                await make_two_jumps(game, bob, rules)
                await preview_a_drag(game, alice, rules)
                await recover_and_close(game, bob, subscription, fixture["expected"])
            finally:
                if game.owners:
                    await game.close()


if __name__ == "__main__":
    asyncio.run(main())