Skip to content

test: fix BackgroundConsumer error log #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion google/api_core/bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ def _thread_main(self, ready):
_LOGGER.debug("waiting for recv.")
response = self._bidi_rpc.recv()
_LOGGER.debug("recved response.")
self._on_response(response)
if self._on_response is not None:
self._on_response(response)

except exceptions.GoogleAPICallError as exc:
_LOGGER.debug(
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import queue
import threading
import time

try:
from unittest import mock
Expand Down Expand Up @@ -894,3 +895,26 @@ def close_side_effect():

# calling stop twice should not result in an error.
consumer.stop()

def test_stop_error_logs(self, caplog):
"""
Closing the client should result in no internal error logs

https://github.com/googleapis/python-api-core/issues/788
"""
caplog.set_level(logging.DEBUG)
bidi_rpc = mock.create_autospec(bidi.BidiRpc, instance=True)
bidi_rpc.is_active = True
on_response = mock.Mock(spec=["__call__"])

consumer = bidi.BackgroundConsumer(bidi_rpc, on_response)

consumer.start()
consumer.stop()
# let the background thread run for a while before exiting
time.sleep(0.1)
bidi_rpc.is_active = False
# running thread should not result in error logs
error_logs = [r.message for r in caplog.records if r.levelname == "ERROR"]
assert not error_logs, f"Found unexpected ERROR logs: {error_logs}"
bidi_rpc.is_active = False