"""Catch an interrupt record outliving the cell it was for. An interrupt that reaches a cell still queued is remembered until the cell starts. A cell refused at the cell limit never starts, so only a check inside a real kernel, with the queue held behind a running cell, sees whether the record was dropped with it. """ import unittest from remote_kernel_test import OwnerChannel LIMIT_ONE = """ import albedo_cells as kernel kernel.BACKGROUND_SECONDS = 0.2 kernel.LIMITS["cell"] = 1 """ HOLD = "import asyncio\nawait asyncio.sleep(1.5)\n" INSPECT = "print('pending', sorted(kernel.INTERRUPTS))\n" class CellInterruptsTest(unittest.TestCase): def setUp(self): self.channel = OwnerChannel([]) self.addCleanup(self.channel.close) self.assertEqual(self.channel.recv()["type"], "ready") def done(self, id, code): self.channel.send({"type": "execute", "id": id, "code": code}) return self.channel.wait_for( lambda frame: ( frame["type"] == "done" and frame["id"] == id and frame["status"] != "backgrounded" ) ) def test_an_interrupted_cell_refused_at_the_cell_limit_leaves_no_record(self): self.assertEqual(self.done("limit", LIMIT_ONE)["status"], "ok") self.channel.send({"type": "execute", "id": "hold", "code": HOLD}) # The serving loop waits on "hold" until it is backgrounded, so the # next cell is queued, and the interrupt finds it there. self.channel.send({"type": "execute", "id": "refused", "code": "pass"}) self.channel.send({"type": "interrupt", "id": "refused", "reason": "cancelled"}) refused = self.channel.wait_for( lambda frame: frame["type"] == "done" and frame["id"] == "refused" ) self.assertIn("16 cells are still running", refused["output"]) wake = self.channel.wait_for( lambda frame: ( frame["type"] == "call" and frame.get("method") == "cells.completed" ) ) self.channel.send( {"type": "reply", "id": wake["id"], "value": {"ok": True, "value": None}} ) inspected = self.done("inspect", INSPECT) self.assertIn("pending []", inspected["output"], inspected) if __name__ == "__main__": unittest.main()