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:
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
fn recover_and_close(
game: &Game<'_>,
subscription: events::Subscription<'_, events::CheckersTableCursorData>,
owners: [events::Owner<'_>; 3],
expected: &Value,
) -> Outcome {
subscription.close()?;
assert_eq!(&game.board()?, expected);
for owner in owners {
owner.close()?;
}
let status: Value = stardust::decode(&game.db.events().status()?)?;
assert_eq!(
status,
json!({"owners":0,"subscriptions":0,"registrations":0,"rpcs":0,"queued":0})
);
println!(
"{}",
String::from_utf8(stardust::encode(expected)?.serialize(Format::DUST, 100)?)?
);
Ok(())
}fn main() -> Outcome {
stardust::activate(std::env::var("STARDUST_TEST_LICENSE")?.trim())?;
let arguments: Vec<_> = std::env::args().skip(1).collect();
let format = match arguments.get(1).map(String::as_str) {
Some("dust") => Format::DUST,
Some("json") => Format::JSON,
Some("edn") => Format::EDN,
Some("tron") => Format::TRON,
_ => return Err("choose dust, json, edn or tron".into()),
};
let fixture: Value =
stardust::decode(&stardust::parse(std::fs::read(&arguments[0])?, format)?)?;
let directory = std::env::temp_dir().join(format!(
"stardust-checkers-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_nanos()
));
std::fs::create_dir(&directory)?;
let result = (|| -> Outcome {
let db = open_checkers(
directory
.join("checkers.db")
.to_str()
.ok_or("path is not UTF-8")?,
directory.join("sdk").to_str().ok_or("path is not UTF-8")?,
)?;
let game = Game { db: &db };
prepare(&game, &fixture)?;
let alice = events::Owner::open(&db)?;
let bob = events::Owner::open(&db)?;
let rules = events::Owner::open(&db)?;
let subscription = share_cursors(&game, &bob)?;
move_one_piece(&game, &alice, &bob, &rules)?;
list_legal_moves(&game, &bob, &rules)?;
make_two_jumps(&game, &bob, &rules)?;
preview_a_drag(&game, &alice, &rules)?;
recover_and_close(
&game,
subscription,
[alice, bob, rules],
&fixture["expected"],
)
})();
std::fs::remove_dir_all(directory)?;
result
}