def test_pubsub(self): # create topic if needed client = SubscriberClient() try: topic_id = 'container-analysis-occurrences-v1' topic_name = {"name": f"projects/{PROJECT_ID}/topics/{topic_id}"} publisher = PublisherClient() publisher.create_topic(topic_name) except AlreadyExists: pass subscription_id = 'container-analysis-test-{}'.format(uuid.uuid4()) subscription_name = client.subscription_path(PROJECT_ID, subscription_id) samples.create_occurrence_subscription(subscription_id, PROJECT_ID) # I can not make it pass with multiple messages. My guess is # the server started to dedup? message_count = 1 try: job_done = threading.Event() receiver = MessageReceiver(message_count, job_done) client.subscribe(subscription_name, receiver.pubsub_callback) for i in range(message_count): occ = samples.create_occurrence(self.image_url, self.note_id, PROJECT_ID, PROJECT_ID) time.sleep(SLEEP_TIME) samples.delete_occurrence(basename(occ.name), PROJECT_ID) time.sleep(SLEEP_TIME) # We saw occational failure with 60 seconds timeout, so we bumped it # to 180 seconds. # See also: python-docs-samples/issues/2894 job_done.wait(timeout=180) print('done. msg_count = {}'.format(receiver.msg_count)) assert message_count <= receiver.msg_count finally: # clean up client.delete_subscription({"subscription": subscription_name})
def test_pubsub(self): # create topic if needed client = SubscriberClient() try: topic_id = 'container-analysis-occurrences-v1' topic_name = client.topic_path(PROJECT_ID, topic_id) publisher = PublisherClient() publisher.create_topic(topic_name) except AlreadyExists: pass subscription_id = 'drydockOccurrences' subscription_name = client.subscription_path(PROJECT_ID, subscription_id) samples.create_occurrence_subscription(subscription_id, PROJECT_ID) tries = 0 success = False while not success and tries < TRY_LIMIT: print(tries) tries += 1 receiver = samples.MessageReceiver() client.subscribe(subscription_name, receiver.pubsub_callback) # test adding 3 more occurrences total_created = 3 for _ in range(total_created): occ = samples.create_occurrence(self.image_url, self.note_id, PROJECT_ID, PROJECT_ID) sleep(SLEEP_TIME) samples.delete_occurrence(basename(occ.name), PROJECT_ID) sleep(SLEEP_TIME) print('done. msg_count = {}'.format(receiver.msg_count)) success = receiver.msg_count == total_created if receiver.msg_count != total_created: raise AssertionError # clean up client.delete_subscription(subscription_name)
def test_pubsub(self): # create topic if needed client = SubscriberClient() try: topic_id = 'container-analysis-occurrences-v1' topic_name = client.topic_path(PROJECT_ID, topic_id) publisher = PublisherClient() publisher.create_topic(topic_name) except AlreadyExists: pass subscription_id = 'container-analysis-test-{}'.format(uuid.uuid4()) subscription_name = client.subscription_path(PROJECT_ID, subscription_id) samples.create_occurrence_subscription(subscription_id, PROJECT_ID) # I can not make it pass with multiple messages. My guess is # the server started to dedup? message_count = 1 try: job_done = threading.Event() receiver = MessageReceiver(message_count, job_done) client.subscribe(subscription_name, receiver.pubsub_callback) for i in range(message_count): occ = samples.create_occurrence(self.image_url, self.note_id, PROJECT_ID, PROJECT_ID) time.sleep(SLEEP_TIME) samples.delete_occurrence(basename(occ.name), PROJECT_ID) time.sleep(SLEEP_TIME) job_done.wait(timeout=60) print('done. msg_count = {}'.format(receiver.msg_count)) assert message_count <= receiver.msg_count finally: # clean up client.delete_subscription(subscription_name)
def create_topic_with_schema(project_id, topic_id, schema_id, message_encoding): """Create a topic resource with a schema.""" # [START pubsub_create_topic_with_schema] from google.api_core.exceptions import AlreadyExists, InvalidArgument from google.cloud.pubsub import PublisherClient, SchemaServiceClient from google.pubsub_v1.types import Encoding # TODO(developer): Replace these variables before running the sample. # project_id = "your-project-id" # topic_id = "your-topic-id" # schema_id = "your-schema-id" # Choose either BINARY or JSON as valid message encoding in this topic. # message_encoding = "BINARY" publisher_client = PublisherClient() topic_path = publisher_client.topic_path(project_id, topic_id) schema_client = SchemaServiceClient() schema_path = schema_client.schema_path(project_id, schema_id) if message_encoding == "BINARY": encoding = Encoding.BINARY elif message_encoding == "JSON": encoding = Encoding.JSON else: encoding = Encoding.ENCODING_UNSPECIFIED try: response = publisher_client.create_topic( request={ "name": topic_path, "schema_settings": { "schema": schema_path, "encoding": encoding }, }) print(f"Created a topic:\n{response}") except AlreadyExists: print(f"{topic_id} already exists.") except InvalidArgument: print( "Please choose either BINARY or JSON as a valid message encoding type." )