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_without_emulator(self): # prepare subscriber subscriber = pubsub_client.SubscribeClient(self.project, self.cred) with self.assertRaises(ServiceUnavailable): subscriber.create_subscription(self.topic, 'fake-subscription', retry=None)
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_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 __subscriber(self): # prepare subscriber subscriber = pubsub_client.SubscribeClient(self.project, self.cred) self.subscription = subscriber.create_subscription(self.topic, 'fake-subscription') self.service = sub_service.SubscriptionService(self.subscription) logger.info('start subscribing message') self.service.run(callback=lambda message: self.__on_received(message))
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_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_subscribe_non_exist_topic(self): # prepare subscriber subscriber = pubsub_client.SubscribeClient(self.project, self.cred) with self.assertRaises(NotFound): subscriber.create_subscription(self.topic, 'fake-subscription')
from soocii_pubsub_lib import pubsub_client, sub_service # ========== Initial Logger ========== logging.basicConfig( level=logging.DEBUG, format= '[%(asctime)-15s][%(levelname)-5s][%(filename)s][%(funcName)s#%(lineno)d] %(message)s' ) logger = logging.getLogger(__name__) # ==================================== def callback(payload): logger.info(payload) # ack message return True if __name__ == '__main__': project = os.getenv('PUBSUB_PROJECT_ID', 'pubsub-trial-198610') cred = os.getenv('GOOGLE_APPLICATION_CREDENTIALS', './pubsub-trial.json') logger.info('project: {}, cred: {}'.format(project, cred)) # prepare subscriber subscriber = pubsub_client.SubscribeClient(project, cred) subscription = subscriber.create_subscription('test-topic', 'test-sub2') service = sub_service.SubscriptionService(subscription) service.run(callback)