From 96a266ffcc44fc1f12fb36f0968fe41caede9dbf Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Wed, 31 Jan 2024 15:21:59 +0000 Subject: [PATCH] TST: Create new topic for each message handler test --- tests/cloud/pub_sub/test_message_handler.py | 131 +++++++++++++------- 1 file changed, 84 insertions(+), 47 deletions(-) diff --git a/tests/cloud/pub_sub/test_message_handler.py b/tests/cloud/pub_sub/test_message_handler.py index ec19319d0..af4f5f3d0 100644 --- a/tests/cloud/pub_sub/test_message_handler.py +++ b/tests/cloud/pub_sub/test_message_handler.py @@ -1,5 +1,6 @@ import datetime import math +import uuid from unittest.mock import patch from octue.cloud.emulators._pub_sub import ( @@ -17,24 +18,32 @@ from tests.base import BaseTestCase -QUESTION_UUID = "1c766d5e-4c6c-456a-b1af-17c7f453999d" +parent = MockService(service_id="my-org/my-service:1.0.0", backend=GCPPubSubBackend(project_name=TEST_PROJECT_NAME)) -mock_topic = MockTopic(name="my-org.my-service.1-0-0", project_name=TEST_PROJECT_NAME) +def create_mock_topic_and_subscription(): + """Create a question UUID, mock topic, and mock subscription. -mock_subscription = MockSubscription( - name=f"my-org.my-service.1-0-0.answers.{QUESTION_UUID}", - topic=mock_topic, - project_name=TEST_PROJECT_NAME, -) -mock_subscription.create() + :return (str, octue.cloud.emulators._pub_sub.MockTopic, octue.cloud.emulators._pub_sub.MockSubscription): question UUID, topic, and subscription + """ + question_uuid = str(uuid.uuid4()) + topic = MockTopic(name="my-org.my-service.1-0-0", project_name=TEST_PROJECT_NAME) -parent = MockService(service_id="my-org/my-service:1.0.0", backend=GCPPubSubBackend(project_name=TEST_PROJECT_NAME)) + subscription = MockSubscription( + name=f"my-org.my-service.1-0-0.answers.{question_uuid}", + topic=topic, + project_name=TEST_PROJECT_NAME, + ) + + subscription.create() + return question_uuid, topic, subscription class TestOrderedMessageHandler(BaseTestCase): def test_timeout(self): """Test that a TimeoutError is raised if message handling takes longer than the given timeout.""" + question_uuid, _, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -48,6 +57,8 @@ def test_timeout(self): def test_in_order_messages_are_handled_in_order(self): """Test that messages received in order are handled in order.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -59,10 +70,10 @@ def test_in_order_messages_are_handled_in_order(self): child = MockService(backend=GCPPubSubBackend(project_name=TEST_PROJECT_NAME)) messages = [ - {"event": {"kind": "test"}, "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}}, - {"event": {"kind": "test"}, "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}}, - {"event": {"kind": "test"}, "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}}, - {"event": {"kind": "finish-test"}, "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}}, + {"event": {"kind": "test"}, "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}}, + {"event": {"kind": "test"}, "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}}, + {"event": {"kind": "test"}, "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}}, + {"event": {"kind": "finish-test"}, "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}}, ] for message in messages: @@ -78,6 +89,8 @@ def test_in_order_messages_are_handled_in_order(self): def test_out_of_order_messages_are_handled_in_order(self): """Test that messages received out of order are handled in order.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -91,19 +104,19 @@ def test_out_of_order_messages_are_handled_in_order(self): messages = [ { "event": {"kind": "test", "order": 1}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 2}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 0}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "finish-test", "order": 3}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -129,6 +142,8 @@ def test_out_of_order_messages_with_end_message_first_are_handled_in_order(self) """Test that messages received out of order and with the final message (the message that triggers a value to be returned) are handled in order. """ + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -142,19 +157,19 @@ def test_out_of_order_messages_with_end_message_first_are_handled_in_order(self) messages = [ { "event": {"kind": "finish-test", "order": 3}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 1}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 2}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 0}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -178,6 +193,8 @@ def test_out_of_order_messages_with_end_message_first_are_handled_in_order(self) def test_no_timeout(self): """Test that message handling works with no timeout.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -191,15 +208,15 @@ def test_no_timeout(self): messages = [ { "event": {"kind": "test", "order": 0}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 1}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "finish-test", "order": 2}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -216,6 +233,8 @@ def test_no_timeout(self): def test_delivery_acknowledgement(self): """Test that a delivery acknowledgement message is handled correctly.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -227,11 +246,11 @@ def test_delivery_acknowledgement(self): messages = [ { "event": {"kind": "delivery_acknowledgement", "datetime": datetime.datetime.utcnow().isoformat()}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "result"}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -243,6 +262,8 @@ def test_delivery_acknowledgement(self): def test_error_raised_if_heartbeat_not_received_before_checked(self): """Test that an error is raised if a heartbeat isn't received before a heartbeat is first checked for.""" + question_uuid, _, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler(subscription=mock_subscription, receiving_service=parent) @@ -254,6 +275,8 @@ def test_error_raised_if_heartbeat_not_received_before_checked(self): def test_error_raised_if_heartbeats_stop_being_received(self): """Test that an error is raised if heartbeats stop being received within the maximum interval.""" + question_uuid, _, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -269,6 +292,8 @@ def test_error_raised_if_heartbeats_stop_being_received(self): def test_error_not_raised_if_heartbeat_has_been_received_in_maximum_allowed_interval(self): """Test that an error is not raised if a heartbeat has been received in the maximum allowed interval.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -285,11 +310,11 @@ def test_error_not_raised_if_heartbeat_has_been_received_in_maximum_allowed_inte "kind": "delivery_acknowledgement", "datetime": datetime.datetime.utcnow().isoformat(), }, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "result"}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -304,6 +329,8 @@ def test_error_not_raised_if_heartbeat_has_been_received_in_maximum_allowed_inte def test_time_since_last_heartbeat_is_none_if_no_heartbeat_received_yet(self): """Test that the time since the last heartbeat is `None` if no heartbeat has been received yet.""" + question_uuid, _, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -316,10 +343,12 @@ def test_total_run_time_is_none_if_handle_messages_has_not_been_called(self): """Test that the total run time for the message handler is `None` if the `handle_messages` method has not been called. """ + question_uuid, _, mock_subscription = create_mock_topic_and_subscription() message_handler = OrderedMessageHandler(subscription=mock_subscription, receiving_service=parent) self.assertIsNone(message_handler.total_run_time) def test_time_since_missing_message_is_none_if_no_missing_messages(self): + question_uuid, _, mock_subscription = create_mock_topic_and_subscription() message_handler = OrderedMessageHandler(subscription=mock_subscription, receiving_service=parent) self.assertIsNone(message_handler.time_since_missing_message) @@ -327,6 +356,8 @@ def test_missing_messages_at_start_can_be_skipped(self): """Test that the first n messages can be skipped if they aren't received after a given time period if subsequent messages have been received. """ + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -344,19 +375,19 @@ def test_missing_messages_at_start_can_be_skipped(self): messages = [ { "event": {"kind": "test", "order": 2}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 3}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 4}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "finish-test", "order": 5}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -378,6 +409,8 @@ def test_missing_messages_at_start_can_be_skipped(self): def test_missing_messages_in_middle_can_skipped(self): """Test that missing messages in the middle of the event stream can be skipped.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -393,15 +426,15 @@ def test_missing_messages_in_middle_can_skipped(self): messages = [ { "event": {"kind": "test", "order": 0}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 1}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 2}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -414,7 +447,7 @@ def test_missing_messages_in_middle_can_skipped(self): # Send a final message. child._send_message( message={"kind": "finish-test", "order": 5}, - attributes={"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + attributes={"question_uuid": question_uuid, "sender_type": "CHILD"}, topic=mock_topic, ) @@ -433,6 +466,8 @@ def test_missing_messages_in_middle_can_skipped(self): def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self): """Test that multiple blocks of missing messages in the middle of the event stream can be skipped.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -448,15 +483,15 @@ def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self): messages = [ { "event": {"kind": "test", "order": 0}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 1}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 2}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -469,7 +504,7 @@ def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self): # Send another message. child._send_message( message={"kind": "test", "order": 5}, - attributes={"message_number": 5, "question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + attributes={"message_number": 5, "question_uuid": question_uuid, "sender_type": "CHILD"}, topic=mock_topic, ) @@ -480,19 +515,19 @@ def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self): messages = [ { "event": {"kind": "test", "order": 20}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 21}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "test", "order": 22}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, { "event": {"kind": "finish-test", "order": 23}, - "attributes": {"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + "attributes": {"question_uuid": question_uuid, "sender_type": "CHILD"}, }, ] @@ -518,6 +553,8 @@ def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self): def test_all_messages_missing_apart_from_result(self): """Test that the result message is still handled if all other messages are missing.""" + question_uuid, mock_topic, mock_subscription = create_mock_topic_and_subscription() + with patch("octue.cloud.pub_sub.message_handler.SubscriberClient", MockSubscriber): message_handler = OrderedMessageHandler( subscription=mock_subscription, @@ -535,7 +572,7 @@ def test_all_messages_missing_apart_from_result(self): # Send the result message. child._send_message( message={"kind": "finish-test", "order": 1000}, - attributes={"question_uuid": QUESTION_UUID, "sender_type": "CHILD"}, + attributes={"question_uuid": question_uuid, "sender_type": "CHILD"}, topic=mock_topic, ) @@ -548,7 +585,7 @@ def test_all_messages_missing_apart_from_result(self): class TestPullAndEnqueueMessage(BaseTestCase): def test_pull_and_enqueue_available_messages(self): """Test that pulling and enqueuing a message works.""" - question_uuid = "4d31bb46-66c4-4e68-831f-e51e17e651ef" + question_uuid, mock_topic, _ = create_mock_topic_and_subscription() with ServicePatcher(): mock_subscription = MockSubscription( @@ -588,7 +625,7 @@ def test_pull_and_enqueue_available_messages(self): def test_timeout_error_raised_if_result_message_not_received_in_time(self): """Test that a timeout error is raised if a result message is not received in time.""" - question_uuid = "4d31bb46-66c4-4e68-831f-e51e17e651ef" + question_uuid, mock_topic, _ = create_mock_topic_and_subscription() with ServicePatcher(): mock_subscription = MockSubscription(