From cbdb7be7e6e009c98fad64aeba9f9dbcf7fa6b38 Mon Sep 17 00:00:00 2001 From: Mackenzie Grimes - NOAA Affiliate Date: Wed, 1 Nov 2023 15:47:58 -0600 Subject: [PATCH] revert publish confirm start() changes, required unit test changes --- .../idsse/common/publish_confirm.py | 5 ++++ .../idsse_common/test/test_publish_confirm.py | 23 ++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/python/idsse_common/idsse/common/publish_confirm.py b/python/idsse_common/idsse/common/publish_confirm.py index d50712fe..733fb82d 100644 --- a/python/idsse_common/idsse/common/publish_confirm.py +++ b/python/idsse_common/idsse/common/publish_confirm.py @@ -136,6 +136,11 @@ def publish_message(self, logger.error('Publish message problem : %s', str(e)) return False + def start(self): + """Start thread to connect RabbitMQ queue and prepare to publish messages.""" + super().start() + time.sleep(.2) + def run(self): """Run the thread, i.e. get connection etc...""" set_corr_id_context_var('PublishConfirm') diff --git a/python/idsse_common/test/test_publish_confirm.py b/python/idsse_common/test/test_publish_confirm.py index 0ae1707b..03275291 100644 --- a/python/idsse_common/test/test_publish_confirm.py +++ b/python/idsse_common/test/test_publish_confirm.py @@ -145,7 +145,6 @@ def publish_confirm(monkeypatch: MonkeyPatch, context: MockPika) -> PublishConfi # tests def test_publish_confirm_start_and_stop(publish_confirm: PublishConfirm): publish_confirm.start() - sleep(0.1) # allow thread to start assert publish_confirm._connection and publish_confirm._connection.is_open assert publish_confirm._channel and publish_confirm._channel.is_open @@ -167,7 +166,6 @@ def mock_confirm_delivery(self: context.Channel, callback: Callable[[Method], No context.Channel.confirm_delivery = mock_confirm_delivery publish_confirm.start() - sleep(0.1) assert publish_confirm._records.nacked == 1 assert publish_confirm._records.acked == 0 @@ -222,11 +220,30 @@ def test_callback(): assert publish_confirm._channel is None publish_confirm._start_with_callback(test_callback) - sleep(.1) # ensure that callback has time to run and send its message + sleep(.1) # ensure that our test's callback has time to run and send its message assert publish_confirm._records.message_number == 1 assert publish_confirm._records.deliveries[1] == example_message +def test_start_without_callback_sleeps(publish_confirm: PublishConfirm, monkeypatch: MonkeyPatch): + def mock_sleep_function(secs: float): + # If this is not the call originating from PublishConfirm.start(), let it really sleep. + # Mocking all sleep() calls seemed to break Thread operations (unit test ran forever) + if secs != 0.2: + sleep(secs) + + mock_sleep = Mock(wraps=mock_sleep_function) + monkeypatch.setattr('idsse.common.publish_confirm.time.sleep', mock_sleep) + + # if no callback passed, start() should sleep internally to ensure RabbitMQ callbacks complete + publish_confirm.start() + + # mock sleep someimtes captures a call from PublishConfirm.run(), due to a race condition + # between this test's thread and the PublishConfirm thread. Both results are acceptable + sleep_call_args = [call.args for call in mock_sleep.call_args_list] + assert set(sleep_call_args) in [set([(0.2,)]), set([(0.2,), (5,)])] + + def test_wait_for_channel_returns_when_ready(monkeypatch: MonkeyPatch, context: MockPika): monkeypatch.setattr('idsse.common.publish_confirm.SelectConnection', context.SelectConnection) pub_conf = PublishConfirm(conn=EXAMPLE_CONN, exchange=EXAMPLE_EXCH, queue=EXAMPLE_QUEUE)