Skip to content

Commit

Permalink
Add a test for traceback and update wiki
Browse files Browse the repository at this point in the history
Signed-off-by: Gajowniczek, Artur <artur.gajowniczek@point72.com>
  • Loading branch information
argaj committed Jul 12, 2024
1 parent 4ddc99f commit 58c58b9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
50 changes: 50 additions & 0 deletions csp/tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions docs/wiki/how-tos/Write-Realtime-Input-Adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,9 @@ 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`.

0 comments on commit 58c58b9

Please sign in to comment.