-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from ICRAR/liu-322-top-level-catch
LIU-322: Catch async exceptions
- Loading branch information
Showing
5 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Optional | ||
from dlg.event import EventFirer, EventHandler, Event | ||
import pytest | ||
|
||
|
||
class MockEventSource(EventFirer): | ||
def fireEvent(self, eventType, **kwargs): | ||
self._fireEvent(eventType, **kwargs) | ||
|
||
|
||
class MockThrowingEventHandler(EventHandler): | ||
def __init__(self) -> None: | ||
self.wasCalled = False | ||
|
||
def handleEvent(self, e: Event) -> None: | ||
self.wasCalled = True | ||
raise RuntimeError("MockThrow", e) | ||
|
||
|
||
class MockEventHandler(EventHandler): | ||
def __init__(self) -> None: | ||
self.lastEvent: Optional[Event] = None | ||
|
||
def handleEvent(self, e: Event) -> None: | ||
self.lastEvent = e | ||
|
||
|
||
def test_listener_exception_interrupts_later_handlers(): | ||
eventSource = MockEventSource() | ||
handler1 = MockEventHandler() | ||
handler2 = MockEventHandler() | ||
throwingHandler = MockThrowingEventHandler() | ||
eventSource.subscribe(handler1, "raise") | ||
eventSource.subscribe(throwingHandler, "raise") | ||
eventSource.subscribe(handler2, "raise") | ||
|
||
with pytest.raises(RuntimeError): | ||
eventSource.fireEvent("raise", prop="value") | ||
|
||
assert throwingHandler.wasCalled | ||
assert handler1.lastEvent is not None | ||
assert getattr(handler1.lastEvent, "prop") == "value" | ||
assert handler2.lastEvent is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import threading | ||
from dlg.drop import InputFiredAppDROP | ||
from dlg.event import Event, EventHandler | ||
import pytest | ||
|
||
|
||
class MockThrowingDrop(InputFiredAppDROP): | ||
def run(): | ||
raise RuntimeError("Drop throw") | ||
|
||
|
||
class MockThrowingHandler(EventHandler): | ||
def handleEvent(self, e: Event) -> None: | ||
raise RuntimeError("Handler throw") | ||
|
||
|
||
def test_async_execute_catches_and_logs_unexpected_exception( | ||
caplog: pytest.LogCaptureFixture, | ||
): | ||
drop = MockThrowingDrop("t", "t", n_effective_inputs=1) | ||
handler = MockThrowingHandler() | ||
drop.subscribe(handler) | ||
|
||
thread = drop.async_execute() | ||
assert isinstance(thread, threading.Thread) | ||
thread.join() | ||
|
||
assert "Handler throw" in caplog.text | ||
# execute should handle exceptions in the run method | ||
assert "Drop throw" not in caplog.text | ||
|
||
|
||
def test_execute_propogates_unexpected_exception(): | ||
drop = MockThrowingDrop("t", "t", n_effective_inputs=1) | ||
handler = MockThrowingHandler() | ||
drop.subscribe(handler) | ||
|
||
with pytest.raises(RuntimeError) as e: | ||
drop.execute() | ||
|
||
assert "Handler throw" in str(e.value) | ||
assert "Drop throw" not in str(e.value) |