Skip to content

Commit

Permalink
revert publish confirm start() changes, required unit test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenzie-grimes-noaa committed Nov 1, 2023
1 parent d5df264 commit cbdb7be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions python/idsse_common/idsse/common/publish_confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
23 changes: 20 additions & 3 deletions python/idsse_common/test/test_publish_confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cbdb7be

Please sign in to comment.