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
(defn recover-and-close [db bob subscription owners expected]
;; Bob abandons the pending cues and reads the current board.
(runtime/unsubscribe subscription)
(assert (= expected (board db)))
(doseq [owner (reverse @owners)]
(runtime/close-owner db owner))
(reset! owners [])
(assert (every? zero? (vals (with-open [result (sd/events-status db)] (sd/decode result)))))
(with-open [result (sd/encode expected)]
(println (sd/serialize result :dust))))(defn -main [fixture-path format-name]
(sd/activate (str/trim (System/getenv "STARDUST_TEST_LICENSE")))
(let [fixture (with-open [source (sd/parse (Files/readAllBytes (.toPath (io/file fixture-path))) (keyword format-name))]
(sd/decode source {:keys :string}))
directory (.toFile (Files/createTempDirectory "stardust-checkers-" (make-array FileAttribute 0)))]
(try
(with-open [db (open-checkers
(.getPath (io/file directory "checkers.db"))
(.getPath (io/file directory "sdk")))]
(prepare db fixture)
(let [owners (atom [])
open-owner #(let [owner (runtime/open-owner db)] (swap! owners conj owner) owner)
alice (open-owner)
bob (open-owner)
rules (open-owner)]
(try
(let [subscription (share-cursors db alice bob)]
(move-one-piece db alice bob rules subscription)
(list-legal-moves db bob rules)
(make-two-jumps db bob rules)
(preview-a-drag db alice rules)
(recover-and-close db bob subscription owners (get fixture "expected")))
(finally (doseq [owner (reverse @owners)] (runtime/close-owner db owner))))))
(finally (doseq [file (reverse (file-seq directory))] (io/delete-file file))))))