"""An interrupt or input cancel that arrives while a finished turn's end is still being committed must say the run already ended: its outcome is fixed, and it is recorded as completed. Only the ledger writer, held at that commit through the inspection node, opens this window on demand.""" import json import threading import unittest from harness import ROOT, Albedo, Provider, exclusive_class, text from inspect_support import Inspection @exclusive_class class SessionFinishTest(unittest.TestCase): def setUp(self): self.started = threading.Event() self.answer = threading.Event() self.addCleanup(self.answer.set) def script(_): self.started.set() self.answer.wait(30) return text("finished answer") self.provider = Provider(script) self.addCleanup(self.provider.close) def prepare(self, app): app.daemon.env["ALBEDO_INSPECT"] = "1" def start_turn(self, app): """A session whose turn waits on the provider, with that turn's snapshot and input id.""" session = app.session() with app.api(f"/sessions/{session}?tail=0") as response: response.read() with app.prompt(session, "answer once") as response: input_id = json.load(response)["id"] self.assertTrue(self.started.wait(20)) with app.api(f"/sessions/{session}?tail=0") as response: snapshot = json.load(response) return session, input_id, snapshot def hold_finish(self, app): """Lets the turn return and holds the writer at the commit of its end.""" probe = Inspection(app, ROOT / "test/e2e/albedo_writer_step_probe.erl") self.assertEqual(probe.call("start"), "held") self.answer.set() self.assertEqual(probe.call("hold_before", '[<<"-finish_turn/">>]'), "held") return probe def outcome(self, app, session, input_id): app.idle(session) with app.api(f"/sessions/{session}/inputs/{input_id}") as response: return json.load(response)["turn"]["state"] def test_interrupt_during_the_finish_commit_reports_already_ended(self): with Albedo(self.provider, prepare=self.prepare) as app: session, input_id, snapshot = self.start_turn(app) replies = [] def interrupt(): with app.api( f"/sessions/{session}/interrupt", { "run_id": snapshot["status"]["run_id"], "through_input_order": snapshot["input_order"], }, ) as response: replies.append(json.load(response)) caller = threading.Thread(target=interrupt) probe = self.hold_finish(app) try: caller.start() # Its cancellation of queued inputs commits right behind the # run's end, so the session answers before the end lands. self.assertEqual( probe.call("queued", '[<<"cancel_through">>]'), "queued" ) finally: probe.call("release") caller.join(20) self.assertEqual([reply["state"] for reply in replies], ["already_ended"]) self.assertEqual(self.outcome(app, session, input_id), "completed") self.assertEqual(len(self.provider.requests), 1) def test_input_cancel_during_the_finish_commit_is_not_pending(self): with Albedo(self.provider, prepare=self.prepare) as app: session, input_id, _ = self.start_turn(app) probe = self.hold_finish(app) try: with app.api( f"/sessions/{session}/inputs/{input_id}/cancel", {} ) as response: self.assertEqual(json.load(response)["result"], "not_pending") finally: probe.call("release") self.assertEqual(self.outcome(app, session, input_id), "completed") if __name__ == "__main__": unittest.main()