def test_subscribe_dict_message_with_attributes(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) assert exception_caughted is False # prepare subscriber subscriber = pubsub_client.SubscribeClient(self.project, self.cred) self.subscription = subscriber.create_subscription( self.topic, 'fake-subscription') # publish dict data = {'f1': 2, 'f2': '3', 'f3': [4, 5, 6]} publisher.publish( self.topic, data, callback=lambda message_id: self.__on_published(message_id), addition1='test1', addition2='test2') # open subscription channel, and start receiving message self.future = self.subscription.open( callback=lambda message: self.__on_received(message)) # wait for callback time.sleep(1) # verify if message has been received assert self.received_message is not None assert self.published_message_id == self.received_message['message_id'] assert self.received_message['data'] == data assert self.received_message['attributes']['addition1'] == 'test1' assert self.received_message['attributes']['addition2'] == 'test2'
def test_create_subscription_twice(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) assert exception_caughted is False # prepare subscriber self.subscription = pubsub_client.SubscribeClient( self.project, self.cred) self.subscription.create_subscription(self.topic, 'fake-subscription') self.subscription.create_subscription(self.topic, 'fake-subscription') # publish bytes publisher.publish( self.topic, b'bytes data', callback=lambda message_id: self.__on_published(message_id)) # open subscription channel, and start receiving message self.future = self.subscription.open( callback=lambda message: self.__on_received(message)) # wait for callback time.sleep(1) # verify if message has been received assert self.received_message is not None assert self.published_message_id == self.received_message['message_id'] assert self.received_message['data'] == b'bytes data' assert self.received_message['attributes'] == {}
def test_subscribe_message_without_callback(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) assert exception_caughted is False # prepare subscriber subscriber = pubsub_client.SubscribeClient(self.project, self.cred) self.subscription = subscriber.create_subscription( self.topic, 'fake-subscription') # publish bytes publisher.publish( self.topic, b'bytes data', callback=lambda message_id: self.__on_published(message_id)) # open subscription channel, and start receiving message self.future = self.subscription.open() # wait for callback time.sleep(1) # close subscription channel, and open again with callback self.subscription.close() self.future = self.subscription.open( callback=lambda message: self.__on_received(message)) # wait for callback time.sleep(1) # verify if message has been processed in previous channel assert self.received_message is None
def test_unsupported_data_type(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) self.assertFalse(exception_caughted) # publish to the topic in sync way with self.assertRaises(ValueError): publisher.publish(self.topic, 12345)
def __publisher(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) self.assertFalse(exception_caughted) # publish bytes logger.info('start publishing message') for _ in range(5): publisher.publish(self.topic, b'bytes data', callback=lambda message_id: self.__on_published(message_id))
def test_sync_publish(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) assert exception_caughted is False # publish to the topic in sync way message_id, _ = publisher.publish(self.topic, b'bytes data') # verify if message_id is returned assert message_id is not None
def test_subscribe_message(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # prepare subscriber self.subscription = pubsub_client.SubscribeClient( self.project, self.cred) self.subscription.create_subscription(self.topic, 'fake-subscription') with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: executor.submit(lambda: self.__waitter()) self.__publisher() # subscriber service MUST run in main thread self.__subscriber() # verify if message has been received assert self.received_message is not None assert self.received_message['data'] == b'bytes data' assert self.received_message['attributes'] == {} assert self.received_message_counts.value == 5
def test_publish_bytes_data(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) assert exception_caughted is False # publish bytes publisher.publish( self.topic, b'bytes data', callback=lambda message_id: self.__on_published(message_id)) # wait for callback time.sleep(1) # verify if message has been published assert self.published_message_id is not None
def test_subscribe_message(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # prepare subscriber subscriber = pubsub_client.SubscribeClient(self.project, self.cred) self.subscription = subscriber.create_subscription(self.topic, 'fake-subscription') with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: executor.submit(lambda: self.__waitter()) # executor.submit(lambda: self.__publisher()) self.__publisher() # subscriber service is running in main thread self.__subscriber() # verify if message has been received # self.assertTrue(self.received_message is not None) assert self.received_message is not None assert self.published_message_id == self.received_message['message_id'] assert self.received_message['data'] == 'bytes data' assert self.received_message['attributes'] == {} self.assertEqual(self.received_message_counts, 5) assert self.received_message_counts == 5
def test_publish_dict_with_attributes(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) publisher.create_topic(self.topic) # get configuration of the topic before sending request exception_caughted = False try: publisher.get_topic(self.topic) except Exception as e: exception_caughted = True logger.exception('unexpected exception was caughted: {}'.format(e)) assert exception_caughted is False # publish dict data = {'f1': 1, 'f2': '2', 'f3': [3, 4, 5]} publisher.publish( self.topic, data, callback=lambda message_id: self.__on_published(message_id), addition1='test1', addition2='test2') # wait for callback time.sleep(1) # verify if message has been published assert self.published_message_id is not None
def test_get_topic_without_emulator(self): # prepare publisher publisher = pubsub_client.PublisherClient(self.project, self.cred) # get configuration of an non-exist topic without retry policy with self.assertRaises(ServiceUnavailable): publisher.get_topic(self.topic, retry=None)
) logger = logging.getLogger(__name__) # ==================================== def callback(payload): logger.info(payload) if __name__ == '__main__': project = None if 'PUBSUB_PROJECT_ID' in os.environ else 'pubsub-trial-198610' cred = None if 'GOOGLE_APPLICATION_CREDENTIALS' in os.environ else './pubsub-trial-f5e6dbba824c.json' logger.info('project: {}, cred: {}'.format(project, cred)) # prepare publisher publisher = pubsub_client.PublisherClient(project, cred) topic = publisher.create_topic('test-topic') # prepare subscriber subscriber = pubsub_client.SubscribeClient(project, cred) subscription = subscriber.create_subscription('test-topic', 'test-sub2') # publish bytes publisher.publish('test-topic', b'bytes data', callback=lambda message_id: logger.info(message_id)) # publish string publisher.publish('test-topic', 'string data') # publish dict data = {'f1': 1, 'f2': '2', 'f3': [3, 4, 5]} publisher.publish('test-topic', data, addition1='test1', addition2='test2')