Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d220fa6

Browse files
committed
fix: allow BackgroundConsumer caller to pass on_fatal_exception to be informed of fatal processing errors
1 parent 48b8f69 commit d220fa6

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

google/api_core/bidi.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -624,17 +624,15 @@ def on_response(response):
624624
``open()``ed yet.
625625
on_response (Callable[[protobuf.Message], None]): The callback to
626626
be called for every response on the stream.
627-
reraise_exceptions (bool): Whether to reraise exceptions during
628-
the lifetime of the consumer, generally those that are not
629-
handled by `BidiRpc`'s `should_recover` or `should_terminate`.
630-
Default `False`.
627+
on_fatal_exception (Callable[[Exception], None]): The callback to
628+
be called on fatal errors during consumption. Default None.
631629
"""
632630

633-
def __init__(self, bidi_rpc, on_response, reraise_exceptions=False):
631+
def __init__(self, bidi_rpc, on_response, on_fatal_exception=None):
634632
self._bidi_rpc = bidi_rpc
635633
self._on_response = on_response
636634
self._paused = False
637-
self._reraise_exceptions = reraise_exceptions
635+
self._on_fatal_exception = on_fatal_exception
638636
self._wake = threading.Condition()
639637
self._thread = None
640638
self._operational_lock = threading.Lock()
@@ -681,17 +679,17 @@ def _thread_main(self, ready):
681679
exc,
682680
exc_info=True,
683681
)
684-
if self._reraise_exceptions:
685-
raise
682+
if self._on_fatal_exception is not None:
683+
self._on_fatal_exception(exc)
686684

687685
except Exception as exc:
688686
_LOGGER.exception(
689687
"%s caught unexpected exception %s and will exit.",
690688
_BIDIRECTIONAL_CONSUMER_NAME,
691689
exc,
692690
)
693-
if self._reraise_exceptions:
694-
raise
691+
if self._on_fatal_exception is not None:
692+
self._on_fatal_exception(exc)
695693

696694
_LOGGER.info("%s exiting", _BIDIRECTIONAL_CONSUMER_NAME)
697695

@@ -734,6 +732,7 @@ def stop(self):
734732

735733
self._thread = None
736734
self._on_response = None
735+
self._on_fatal_exception = None
737736

738737
@property
739738
def is_active(self):

tests/unit/test_bidi.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,11 @@ def test_stop_error_logs(self, caplog):
919919
assert not error_logs, f"Found unexpected ERROR logs: {error_logs}"
920920
bidi_rpc.is_active = False
921921

922-
def test_fatal_exceptions_will_shutdown_consumer(self, caplog):
922+
def test_fatal_exceptions_can_inform_consumer(self, caplog):
923923
"""
924924
https://github.com/googleapis/python-api-core/issues/820
925925
Exceptions thrown in the BackgroundConsumer not caught by `should_recover` / `should_terminate`
926-
on the RPC should be bubbled back to the caller if `reraise_exceptions` is `True`.
926+
on the RPC should be bubbled back to the caller through `on_fatal_exception` if passed.
927927
"""
928928
caplog.set_level(logging.DEBUG)
929929

@@ -935,14 +935,16 @@ def test_fatal_exceptions_will_shutdown_consumer(self, caplog):
935935
bidi_rpc.is_active = True
936936
on_response = mock.Mock(spec=["__call__"])
937937

938+
on_fatal_exception = mock.Mock(spec=["__call__"])
939+
938940
bidi_rpc.open.side_effect = fatal_exception
939941

940942
consumer = bidi.BackgroundConsumer(
941-
bidi_rpc, on_response, reraise_exceptions=True
943+
bidi_rpc, on_response, on_fatal_exception
942944
)
943945

944-
with pytest.raises(type(fatal_exception)):
945-
consumer.start()
946+
consumer.start()
947+
# let the background thread run for a while before exiting
948+
time.sleep(0.1)
946949

947-
# let the background thread run for a while before exiting
948-
time.sleep(0.1)
950+
on_fatal_exception.assert_called_once_with(fatal_exception)

0 commit comments

Comments
 (0)