From 382321d03a568f9be3d172e66b29d4c020ce5fc4 Mon Sep 17 00:00:00 2001 From: "Gajowniczek, Artur" Date: Fri, 12 Jul 2024 11:18:56 -0400 Subject: [PATCH] Add a test for traceback and update wiki Signed-off-by: Gajowniczek, Artur --- csp/tests/test_engine.py | 50 +++++++++++++++++++ .../how-tos/Write-Realtime-Input-Adapters.md | 4 ++ 2 files changed, 54 insertions(+) diff --git a/csp/tests/test_engine.py b/csp/tests/test_engine.py index 88b3db9ba..0982ead59 100644 --- a/csp/tests/test_engine.py +++ b/csp/tests/test_engine.py @@ -841,6 +841,56 @@ def graph(): with self.assertRaisesRegex(ValueError, "Dummy exception message"): csp.run(graph, starttime=datetime(2020, 1, 1), endtime=timedelta(seconds=1)) + def test_adapter_manager_engine_shutdown_traceback(self): + from csp.impl.adaptermanager import AdapterManagerImpl, ManagedSimInputAdapter + from csp.impl.wiring import py_managed_adapter_def + + d = {} + + class TestAdapterManager: + def __init__(self): + self._impl = None + + def subscribe(self): + return TestAdapter(self) + + def _create(self, engine, memo): + self._impl = TestAdapterManagerImpl(engine) + return self._impl + + class TestAdapterManagerImpl(AdapterManagerImpl): + def __init__(self, engine): + super().__init__(engine) + + def start(self, starttime, endtime): + pass + + def stop(self): + pass + + def process_next_sim_timeslice(self, now): + try: + [].pop() + except IndexError as e: + d['tb'] = traceback.format_exc() + self.shutdown_engine(e) + + class TestAdapterImpl(ManagedSimInputAdapter): + def __init__(self, manager_impl): + pass + + TestAdapter = py_managed_adapter_def("TestAdapter", TestAdapterImpl, ts[int], TestAdapterManager) + + def graph(): + adapter = TestAdapterManager() + nc = adapter.subscribe() + csp.add_graph_output("nc", nc) + + with self.assertRaises(IndexError): + csp.run(graph, starttime=datetime(2020, 1, 1), endtime=timedelta(seconds=1)) + + self.assertTrue('[].pop()' in d["tb"]) + def test_feedback(self): # Dummy example class Request(csp.Struct): diff --git a/docs/wiki/how-tos/Write-Realtime-Input-Adapters.md b/docs/wiki/how-tos/Write-Realtime-Input-Adapters.md index 2f0195d5a..badde71a7 100644 --- a/docs/wiki/how-tos/Write-Realtime-Input-Adapters.md +++ b/docs/wiki/how-tos/Write-Realtime-Input-Adapters.md @@ -405,3 +405,7 @@ csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=10), re ``` Do note that realtime adapters will only run in realtime engines (note the `realtime=True` argument to `csp.run`). + +## Engine shutdown + +In case a pushing thread hits a terminal error, an exception can be passed to the main engine thread to shut down gracefully through a `shutdown_engine(exc: Exception)` method exposed by `PushInputAdapter`, `PushPullInputAdapter` and `AdapterManagerImpl`.