From 933b6088f318de242e527b95ee2911e7c196fd79 Mon Sep 17 00:00:00 2001 From: Jamie Cox Date: Thu, 23 May 2019 11:29:58 +0100 Subject: [PATCH 1/2] Use the recommended pattern of checking for failed publishing to pubsub --- test/component/test_component.py | 18 ++++++++++-------- test/scripts/publish_message.py | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/test/component/test_component.py b/test/component/test_component.py index 01e9a36..68e293d 100644 --- a/test/component/test_component.py +++ b/test/component/test_component.py @@ -1,10 +1,10 @@ import json -import time import uuid from unittest import TestCase import pika from coverage.python import os +from google.api_core.exceptions import GoogleAPIError from google.cloud import pubsub_v1 @@ -64,13 +64,15 @@ def publish_to_pubsub(self, tx_id, case_id, questionnaire_id): } }) - future = publisher.publish(topic_path, - data=data.encode('utf-8'), - eventType='OBJECT_FINALIZE', - bucketId='123', - objectId=tx_id) - while not future.done(): - time.sleep(1) + try: + publisher.publish(topic_path, + data=data.encode('utf-8'), + eventType='OBJECT_FINALIZE', + bucketId='123', + objectId=tx_id) \ + .result(timeout=30) + except GoogleAPIError: + assert False, "Failed to publish message to pubsub" print(f'Message published to {topic_path}') diff --git a/test/scripts/publish_message.py b/test/scripts/publish_message.py index afc630a..e81fc2e 100644 --- a/test/scripts/publish_message.py +++ b/test/scripts/publish_message.py @@ -1,8 +1,8 @@ import json import sys -import time import uuid +from google.api_core.exceptions import GoogleAPIError from google.cloud import pubsub_v1 @@ -24,13 +24,14 @@ } }) - future = publisher.publish(topic_path, - data=data.encode('utf-8'), - eventType='OBJECT_FINALIZE', - bucketId='123', - objectId=tx_id) - - while not future.done(): - time.sleep(1) + try: + publisher.publish(topic_path, + data=data.encode('utf-8'), + eventType='OBJECT_FINALIZE', + bucketId='123', + objectId=tx_id) \ + .result(timeout=30) + except GoogleAPIError: + print("Failed to publish message to pubsub") print(f'Message published to {topic_path}') From 022506637273d7687dc0c15199bc041dbca0c770 Mon Sep 17 00:00:00 2001 From: Jamie Cox Date: Thu, 23 May 2019 11:44:42 +0100 Subject: [PATCH 2/2] Add a slight wait to allow for connection delay greater than 0.05 --- test/component/test_component.py | 15 +++++++++------ test/scripts/publish_message.py | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/test/component/test_component.py b/test/component/test_component.py index 68e293d..c7cc637 100644 --- a/test/component/test_component.py +++ b/test/component/test_component.py @@ -1,4 +1,5 @@ import json +import time import uuid from unittest import TestCase @@ -64,13 +65,15 @@ def publish_to_pubsub(self, tx_id, case_id, questionnaire_id): } }) + future = publisher.publish(topic_path, + data=data.encode('utf-8'), + eventType='OBJECT_FINALIZE', + bucketId='123', + objectId=tx_id) + if not future.done(): + time.sleep(1) try: - publisher.publish(topic_path, - data=data.encode('utf-8'), - eventType='OBJECT_FINALIZE', - bucketId='123', - objectId=tx_id) \ - .result(timeout=30) + future.result(timeout=30) except GoogleAPIError: assert False, "Failed to publish message to pubsub" diff --git a/test/scripts/publish_message.py b/test/scripts/publish_message.py index e81fc2e..db0b5b8 100644 --- a/test/scripts/publish_message.py +++ b/test/scripts/publish_message.py @@ -1,5 +1,6 @@ import json import sys +import time import uuid from google.api_core.exceptions import GoogleAPIError @@ -24,13 +25,15 @@ } }) + future = publisher.publish(topic_path, + data=data.encode('utf-8'), + eventType='OBJECT_FINALIZE', + bucketId='123', + objectId=tx_id) + if not future.done(): + time.sleep(1) try: - publisher.publish(topic_path, - data=data.encode('utf-8'), - eventType='OBJECT_FINALIZE', - bucketId='123', - objectId=tx_id) \ - .result(timeout=30) + future.result(timeout=30) except GoogleAPIError: print("Failed to publish message to pubsub")