Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def __init__(
self._closing = threading.Lock()
self._closed = False
self._close_callbacks = []
self._regular_shutdown_thread = None # Created on intentional shutdown.

# Generate a random client id tied to this object. All streaming pull
# connections (initial and re-connects) will then use the same client
Expand Down Expand Up @@ -539,13 +540,13 @@ def close(self, reason=None):
an "intentional" shutdown. This is passed to the callbacks
specified via :meth:`add_close_callback`.
"""
thread = threading.Thread(
self._regular_shutdown_thread = threading.Thread(
name=_REGULAR_SHUTDOWN_THREAD_NAME,
daemon=True,
target=self._shutdown,
kwargs={"reason": reason},
)
thread.start()
self._regular_shutdown_thread.start()

def _shutdown(self, reason=None):
"""Run the actual shutdown sequence (stop the stream and all helper threads).
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_streaming_pull_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,19 @@ def make_running_manager(**kwargs):
)


def await_manager_shutdown(manager, timeout=None):
# NOTE: This method should be called after manager.close(), i.e. after the shutdown
# thread has been created and started.
shutdown_thread = manager._regular_shutdown_thread

if shutdown_thread is None: # pragma: NO COVER
raise Exception("Shutdown thread does not exist on the manager instance.")

shutdown_thread.join(timeout=timeout)
if shutdown_thread.is_alive(): # pragma: NO COVER
pytest.fail("Shutdown not completed in time.")


def test_close():
(
manager,
Expand All @@ -561,6 +574,7 @@ def test_close():
) = make_running_manager()

manager.close()
await_manager_shutdown(manager, timeout=3)

consumer.stop.assert_called_once()
leaser.stop.assert_called_once()
Expand All @@ -583,6 +597,7 @@ def test_close_inactive_consumer():
consumer.is_active = False

manager.close()
await_manager_shutdown(manager, timeout=3)

consumer.stop.assert_not_called()
leaser.stop.assert_called_once()
Expand All @@ -596,6 +611,7 @@ def test_close_idempotent():

manager.close()
manager.close()
await_manager_shutdown(manager, timeout=3)

assert scheduler.shutdown.call_count == 1

Expand Down Expand Up @@ -640,6 +656,7 @@ def test_close_no_dispatcher_error():
dispatcher.start()

manager.close()
await_manager_shutdown(manager, timeout=3)

error_callback.assert_not_called()

Expand All @@ -651,6 +668,7 @@ def test_close_callbacks():

manager.add_close_callback(callback)
manager.close(reason="meep")
await_manager_shutdown(manager, timeout=3)

callback.assert_called_once_with(manager, "meep")

Expand All @@ -660,6 +678,7 @@ def test_close_blocking_scheduler_shutdown():
scheduler = manager._scheduler

manager.close()
await_manager_shutdown(manager, timeout=3)

scheduler.shutdown.assert_called_once_with(await_msg_callbacks=True)

Expand All @@ -669,6 +688,7 @@ def test_close_nonblocking_scheduler_shutdown():
scheduler = manager._scheduler

manager.close()
await_manager_shutdown(manager, timeout=3)

scheduler.shutdown.assert_called_once_with(await_msg_callbacks=False)

Expand All @@ -690,6 +710,7 @@ def fake_nack(self):
manager._messages_on_hold._messages_on_hold.append(messages[2])

manager.close()
await_manager_shutdown(manager, timeout=3)

assert sorted(nacked_messages) == [b"msg1", b"msg2", b"msg3"]

Expand Down