def test_create_topic(self): TOPIC_NAME = 'a-new-topic' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) self.assertTrue(topic.exists()) self.assertEqual(topic.name, TOPIC_NAME)
def test_create_topic(self): new_topic_name = 'a-new-topic' topic = Topic(new_topic_name) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) self.assertTrue(topic.exists()) self.assertEqual(topic.name, new_topic_name)
def test_message_pull_mode_e2e(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME, timestamp_messages=True) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) SUBSCRIPTION_NAME = 'subscribing-now' subscription = Subscription(SUBSCRIPTION_NAME, topic) self.assertFalse(subscription.exists()) subscription.create() self.to_delete.append(subscription) MESSAGE_1 = b'MESSAGE ONE' MESSAGE_2 = b'MESSAGE ONE' EXTRA_1 = 'EXTRA 1' EXTRA_2 = 'EXTRA 2' topic.publish(MESSAGE_1, extra=EXTRA_1) topic.publish(MESSAGE_2, extra=EXTRA_2) received = subscription.pull(max_messages=2) ack_ids = [recv[0] for recv in received] subscription.acknowledge(ack_ids) messages = [recv[1] for recv in received] def _by_timestamp(message): return message.timestamp message1, message2 = sorted(messages, key=_by_timestamp) self.assertEqual(message1.data, MESSAGE_1) self.assertEqual(message1.attributes['extra'], EXTRA_1) self.assertEqual(message2.data, MESSAGE_2) self.assertEqual(message2.attributes['extra'], EXTRA_2)
def test_create_subscription(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) SUBSCRIPTION_NAME = 'subscribing-now' subscription = Subscription(SUBSCRIPTION_NAME, topic) self.assertFalse(subscription.exists()) subscription.create() self.to_delete.append(subscription) self.assertTrue(subscription.exists()) self.assertEqual(subscription.name, SUBSCRIPTION_NAME) self.assertTrue(subscription.topic is topic)
def test_list_subscriptions(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) subscriptions_to_create = [ 'new%d' % (1000 * time.time(),), 'newer%d' % (1000 * time.time(),), 'newest%d' % (1000 * time.time(),), ] for subscription_name in subscriptions_to_create: subscription = Subscription(subscription_name, topic) subscription.create() self.to_delete.append(subscription) # Retrieve the subscriptions. all_subscriptions, _ = pubsub.list_subscriptions() created = [subscription for subscription in all_subscriptions if subscription.name in subscriptions_to_create and subscription.topic.name == TOPIC_NAME] self.assertEqual(len(created), len(subscriptions_to_create))
def test_message_pull_mode_e2e(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) SUBSCRIPTION_NAME = 'subscribing-now' subscription = Subscription(SUBSCRIPTION_NAME, topic) self.assertFalse(subscription.exists()) subscription.create() self.to_delete.append(subscription) MESSAGE = b'MESSAGE' EXTRA = 'EXTRA' topic.publish(MESSAGE, extra=EXTRA) received = subscription.pull() ack_ids = [recv[0] for recv in received] subscription.acknowledge(ack_ids) messages = [recv[1] for recv in received] message, = messages self.assertEqual(message.data, MESSAGE) self.assertEqual(message.attributes, {'extra': EXTRA})
def test_list_subscriptions(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) subscriptions_to_create = [ 'new%d' % (1000 * time.time(), ), 'newer%d' % (1000 * time.time(), ), 'newest%d' % (1000 * time.time(), ), ] for subscription_name in subscriptions_to_create: subscription = Subscription(subscription_name, topic) subscription.create() self.to_delete.append(subscription) # Retrieve the subscriptions. all_subscriptions, _ = pubsub.list_subscriptions() created = [ subscription for subscription in all_subscriptions if subscription.name in subscriptions_to_create and subscription.topic.name == TOPIC_NAME ] self.assertEqual(len(created), len(subscriptions_to_create))