def test_timeout_burst(self): """Check that if we have a large burst of messages in our queue that we can fetch them with a timeout without needing to receive any more messages.""" message = Message( channel=self.channel, body='the quick brown fox jumps over the lazy dog', properties=dict(content_type='application/json', content_encoding='utf-8')) for i in xrange(100): self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) messages = [] def cb(x): messages.append(x) x.ack() self.channel.basic_consume(TEST_QUEUE, callback=cb) for i in xrange(100): self.connection.drain_events(timeout=0.2) self.assertEqual(len(messages), 100)
def test_delete_empty(self): """Test that the queue doesn't get deleted if it is not empty""" self.channel.exchange_declare(self.TEST_QUEUE, 'direct') self.channel.queue_declare(self.TEST_QUEUE) self.channel.queue_bind(self.TEST_QUEUE, self.TEST_QUEUE, self.TEST_QUEUE) message = Message( channel=self.channel, body='the quick brown fox jumps over the lazy dog', properties=dict(content_type='application/json', content_encoding='utf-8')) self.channel.basic_publish(message, self.TEST_QUEUE, self.TEST_QUEUE) with self.assertRaises(ChannelError): self.channel.queue_delete(self.TEST_QUEUE, if_empty=True) # We need to make a new channel after a ChannelError self.channel = self.connection.channel() x = self.channel.basic_get(self.TEST_QUEUE) self.assertTrue(x.body) self.channel.queue_delete(self.TEST_QUEUE, if_empty=True)
def test_nonascii_headers(self): message = Message(channel=self.channel, body='the quick brown fox jumps over the lazy dog', properties=dict(content_type='application/json', content_encoding='utf-8', headers={'key': '¯\_(ツ)_/¯'})) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE)
def test_send_message(self): message = Message(channel=self.channel, body='the quick brown fox jumps over the lazy dog', properties=dict(content_type='application/json', content_encoding='utf-8')) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.assertGreater(self.channel.queue_purge(TEST_QUEUE), 2) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE)
def test_send_message(self): message = Message( 'the quick brown fox jumps over the lazy dog', properties={ 'content_type': 'application/json', 'content_encoding': 'utf-8', }, ) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.assertGreater(self.channel.queue_purge(TEST_QUEUE), 2) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE)
def test_basic_get_ack(self): message = Message("the quick brown fox jumps over the lazy dog", properties=dict(content_type="application/json", content_encoding="utf-8")) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) while True: x = self.channel.basic_get(TEST_QUEUE) if x: break self.assertIs(self.channel, x.channel) self.assertIn("message_count", x.delivery_info) self.assertIn("redelivered", x.delivery_info) self.assertEqual(x.delivery_info["routing_key"], TEST_QUEUE) self.assertEqual(x.delivery_info["exchange"], TEST_QUEUE) self.assertTrue(x.delivery_info["delivery_tag"]) self.assertTrue(x.properties["content_type"]) self.assertTrue(x.body) x.ack()
def test_timeout(self): """Check that our ``drain_events`` call actually times out if there are no messages.""" message = Message('the quick brown fox jumps over the lazy dog', properties=dict(content_type='application/json', content_encoding='utf-8')) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) messages = [] def cb(x): messages.append(x) x.ack() self.channel.basic_consume(TEST_QUEUE, callback=cb) self.connection.drain_events(timeout=0.1) with self.assertRaises(socket.timeout): self.connection.drain_events(timeout=0.1) self.assertEquals(len(messages), 1)
def test_basic_get_ack(self): message = Message( 'the quick brown fox jumps over the lazy dog', properties=dict(content_type='application/json', content_encoding='utf-8')) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) self.channel.basic_publish(message, TEST_QUEUE, TEST_QUEUE) while True: x = self.channel.basic_get(TEST_QUEUE) if x: break self.assertIs(self.channel, x.channel) self.assertIn('message_count', x.delivery_info) self.assertIn('redelivered', x.delivery_info) self.assertEqual(x.delivery_info['routing_key'], TEST_QUEUE) self.assertEqual(x.delivery_info['exchange'], TEST_QUEUE) self.assertTrue(x.delivery_info['delivery_tag']) self.assertTrue(x.properties['content_type']) self.assertTrue(x.body) x.ack()
@task(accept_magic_kwargs=False) def T(): pass tid = uuid() P = TaskPool() hostname = socket.gethostname() task = {'task': T.name, 'args': (), 'kwargs': {}, 'id': tid, 'flags': 0} app = current_app._get_current_object() def on_task(req): req.execute_using_pool(P) def on_ack(*a): pass m = Message(None, {}, {}, task) x = Consumer(on_task, hostname=hostname, app=app) x.update_strategies() name = T.name ts = time() for i in range(100000): x.strategies[name](m, m.body, on_ack) print(time() - ts)