Skip to content

Commit 73206be

Browse files
committed
Test event callback exception handling
1 parent 1666c22 commit 73206be

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tests/unit_tests/core/test_event.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from concurrent.futures import Future
33
from dataclasses import dataclass
44
from queue import Queue
5+
from unittest import mock
56

67
import pytest
78

@@ -76,6 +77,21 @@ def test_correlation_id(publisher: EventPublisher[MyEvent]) -> None:
7677
assert f.result(timeout=_TIMEOUT) == correlation_id
7778

7879

80+
def test_callback_exceptions_are_contained(publisher: EventPublisher[MyEvent]):
81+
event = MyEvent("foo")
82+
c_id = "bar"
83+
84+
# First call should raise exception, next should be fine
85+
handler = mock.Mock(side_effect=[ValueError("Bad Event"), ()])
86+
publisher.subscribe(handler)
87+
publisher.subscribe(handler)
88+
89+
publisher.publish(event, c_id)
90+
91+
# Both handlers should be called and the exception should not be raised from publish
92+
handler.assert_has_calls([mock.call(event, c_id), mock.call(event, c_id)])
93+
94+
7995
def _drain(queue: Queue) -> Iterable:
8096
while not queue.empty():
8197
yield queue.get_nowait()

0 commit comments

Comments
 (0)