def test_basic_cancel(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) for i in range(2): channel.basic_publish(exchange='', routing_key=self.name, body='a') queue = channel.basic_consume(queue=self.name) for i in range(2): msg1 = queue.get() self.assertEqual(msg1.body, 'a') channel.basic_ack(msg1.delivery_tag) result = channel.basic_cancel(queue.consumer_tag) self.assertEqual(result.consumer_tag, queue.consumer_tag) channel.basic_publish(exchange='', routing_key=self.name, body='b') with self.assertRaises(Empty): queue.get(timeout=0.5) channel.queue_delete(queue=self.name)
def test_queue_bind(self): "We can bind and unbind, and the queue receives messages when bound." qname = 'test-queue-bind' conn = Connection(self.amqp_url) with conn.channel() as channel: channel.queue_declare(queue=qname) channel.exchange_declare(exchange=qname, type='direct') channel.basic_publish(exchange=qname, routing_key=qname, body='a') channel.queue_bind(exchange=qname, queue=qname, routing_key=qname) channel.basic_publish(exchange=qname, routing_key=qname, body='b') channel.queue_unbind(exchange=qname, queue=qname, routing_key=qname) channel.basic_publish(exchange=qname, routing_key=qname, body='c') msg = channel.basic_get(queue=qname) self.assertEquals(msg.body, 'b') self.assertEquals(msg['message_count'], 0)
def test_bug3_loop(self): i = [0] conn = Connection(self.amqp_url) qname = 'test-bug3-%s' % random.random() def cb(msg): i[0] += 1 msg.ack() self.assertEqual(msg.body, 'x') if i[0] == 3: conn.close() channel = conn.allocate_channel() channel.queue_declare(queue=qname) for _ in range(3): channel.basic_publish( routing_key=qname, body='x' ) channel.basic_consume(queue=qname, callback=cb) conn.join() self._epilogue(qname, 0)
def test_greenlet_no_interleaving(self): "Messages published at the same time and channel are not garbled." conn = Connection(self.amqp_url) messages = [ 'asdf' * 10000000, 'ghjk' * 10000000 ] def publish_message(msg): channel.basic_publish( routing_key='large-messages', body=msg ) channel = conn.allocate_channel() channel.queue_declare(queue='large-messages') q = channel.basic_consume(queue='large-messages') pool = Pool() pool.map_async(publish_message, messages) for i in range(2): msg = q.get() try: messages.remove(msg.body) except ValueError: raise AssertionError("Received unknown message") msg.ack()
def test_properties(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) headers = { "content_type": 'a', "content_encoding": 'b', #"headers": "delivery_mode": 2, "priority": 1, "correlation_id": 'd', "reply_to": 'e', "expiration": 'f', "message_id": 'g', "timestamp": 1, "type_": 'h', "user_id": 'guest', # that one needs to match real user "app_id": 'j', "cluster_id": 'k', "custom": 'l', "blah2": [True, 1, -1, 4611686018427387904L, -4611686018427387904L, [1,2,3,4, {"a":"b", "c":[]}]], } channel.basic_publish(exchange='', routing_key=self.name, body='a', headers=headers.copy()) r = channel.basic_get(queue=self.name, no_ack=True) self.assertEqual(r.body, 'a') self.assertEqual(headers, r.headers) channel.queue_delete(queue=self.name)
def test_basic_cancel(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) for i in range(2): channel.basic_publish(exchange='', routing_key=self.name, body='a') queue = channel.basic_consume(queue=self.name) for i in range(2): msg1 = queue.get() self.assertEqual(msg1.body, 'a') channel.basic_ack(msg1.delivery_tag) result = channel.basic_cancel(queue.consumer_tag) self.assertEqual(result.consumer_tag, queue.consumer_tag) channel.basic_publish(exchange='', routing_key=self.name, body='b') with self.assertRaises(Empty): queue.get(timeout=0.5) channel.queue_delete(queue=self.name)
def test_queue_delete_not_found(self): """NotFound is raised if we delete a queue that doesn't exist.""" conn = Connection(self.amqp_url) with conn.channel() as channel: with self.assertRaises(NotFound): channel.queue_delete(queue='not_existing_queue')
def test_basic_consume_fail(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: with self.assertRaises(exceptions.NotFound): channel.basic_consume(queue='bad_q_name')
def test_basic_consume_fail(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: with self.assertRaises(exceptions.NotFound): channel.basic_consume(queue='bad_q_name')
def test_persistent(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body=self.msg) # persistence=default channel.basic_publish(exchange='', routing_key=self.name, body=self.msg, headers={'delivery_mode': 2}) channel.basic_publish(exchange='', routing_key=self.name, body=self.msg, headers={'delivery_mode': 1}) result = channel.basic_get(queue=self.name, no_ack=True) self.assertTrue('delivery_mode' not in result.headers) result = channel.basic_get(queue=self.name, no_ack=True) self.assertTrue('delivery_mode' in result.headers) self.assertEquals(result.headers['delivery_mode'], 2) result = channel.basic_get(queue=self.name, no_ack=True) self.assertTrue('delivery_mode' in result.headers) self.assertEquals(result.headers['delivery_mode'], 1) channel.queue_delete(queue=self.name)
def test_persistent(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body=self.msg) # persistence=default channel.basic_publish(exchange='', routing_key=self.name, body=self.msg, headers={'delivery_mode': 2}) channel.basic_publish(exchange='', routing_key=self.name, body=self.msg, headers={'delivery_mode': 1}) result = channel.basic_get(queue=self.name, no_ack=True) self.assertTrue('delivery_mode' not in result.headers) result = channel.basic_get(queue=self.name, no_ack=True) self.assertTrue('delivery_mode' in result.headers) self.assertEquals(result.headers['delivery_mode'], 2) result = channel.basic_get(queue=self.name, no_ack=True) self.assertTrue('delivery_mode' in result.headers) self.assertEquals(result.headers['delivery_mode'], 1) channel.queue_delete(queue=self.name)
def test_queue_delete_not_found(self): """NotFound is raised if we delete a queue that doesn't exist.""" conn = Connection(self.amqp_url) with conn.channel() as channel: with self.assertRaises(NotFound): channel.queue_delete(queue='not_existing_queue')
def test_channel_interleaving(self): """Messages published on different channels are interleaved. We test that if we try to publish a small message while a large message is uploading on a different channel, the small message will be received first. """ conn = Connection(self.amqp_url) messages = [ 'ghjk', 'asdf' * 10000000, ] def publish_message(msg): with conn.channel() as channel: channel.basic_publish( routing_key='large-messages', body=msg ) channel = conn.allocate_channel() channel.queue_declare(queue='large-messages') q = channel.basic_consume(queue='large-messages') gevent.spawn(publish_message, messages[1]) gevent.spawn_later(0.1, publish_message, messages[0]) for i in range(2): msg = q.get() assert msg.body == messages[i] msg.ack()
def _epilogue(self, qname, expected): conn = Connection(self.amqp_url) with conn.channel() as channel: q = channel.queue_declare(queue=qname) try: self.assertEqual(q.message_count, expected) finally: channel.queue_delete(queue=qname)
def _epilogue(self, qname, expected): conn = Connection(self.amqp_url) with conn.channel() as channel: q = channel.queue_declare(queue=qname) try: self.assertEqual(q.message_count, expected) finally: channel.queue_delete(queue=qname)
def test_exclusive_queue(self): conn = Connection(self.amqp_url) with conn.channel() as channel: channel.queue_declare(queue='exclusive-queue', exclusive=True) with conn.channel() as channel: with self.assertRaises(ResourceLocked): channel.queue_declare(queue='exclusive-queue')
def test_basic_return(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.confirm_select() with self.assertRaises(exceptions.MessageReturnedNoRoute): channel.basic_publish(exchange='', routing_key=self.name, mandatory=True, body='')
def test_properties(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) headers = { "content_type": 'a', "content_encoding": 'b', #"headers": "delivery_mode": 2, "priority": 1, "correlation_id": 'd', "reply_to": 'e', "expiration": 'f', "message_id": 'g', "timestamp": 1, "type_": 'h', "user_id": 'guest', # that one needs to match real user "app_id": 'j', "cluster_id": 'k', "custom": 'l', "blah2": [ True, 1, -1, 4611686018427387904L, -4611686018427387904L, [1, 2, 3, 4, { "a": "b", "c": [] }] ], } channel.basic_publish(exchange='', routing_key=self.name, body='a', headers=headers.copy()) r = channel.basic_get(queue=self.name, no_ack=True) self.assertEqual(r.body, 'a') self.assertEqual(headers, r.headers) channel.queue_delete(queue=self.name)
def test_basic_publish_bad_exchange_publisher_acks(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.confirm_select() with self.assertRaises(exceptions.NotFound): channel.basic_publish(exchange='invalid_exchange', routing_key='xxx', body='')
def test_basic_publish_bad_exchange_publisher_acks(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.confirm_select() with self.assertRaises(exceptions.NotFound): channel.basic_publish( exchange='invalid_exchange', routing_key='xxx', body='')
def test_exclusive_consume(self): """Testing exclusive basic_consume""" conn = Connection(self.amqp_url) channel = conn.allocate_channel() channel.queue_declare('exclusive-consume') channel.basic_consume(queue='exclusive-consume', exclusive=True) channel2 = conn.allocate_channel() with self.assertRaises(AccessRefused): channel2.basic_consume(queue='exclusive-consume')
def test_basic_return(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.confirm_select() with self.assertRaises(exceptions.MessageReturnedNoRoute): channel.basic_publish(exchange='', routing_key=self.name, mandatory=True, body='')
def test_queue_redeclare_args(self): """We cannot redeclare a queue if we change its arguments.""" qname = 'test-redeclare-queue-args' conn = Connection(self.amqp_url) conn.connect() with conn.channel() as channel: channel.queue_declare(queue=qname, arguments={}) with self.assertRaises(PreconditionFailed): channel.queue_declare(queue=qname, arguments={'x-expires': 101})
def amqp_setup(): global conn conn = Connection(AMQP_URL) with conn.channel() as channel: channel.exchange_declare("unit_test_room") channel.queue_declare(queue='listener1') channel.queue_declare(queue='listener2') channel.queue_bind(queue="listener1", exchange="unit_test_room", routing_key="user1") channel.queue_bind(queue="listener2", exchange="unit_test_room", routing_key="user2")
class AMQPConnection(object): """ Manage AMQP connections """ connection = None def __init__(self, observer): logger.debug("observer %s" % type(observer)) self.observer = observer self.connection = Connection( os.environ.get('AMQP_URL', 'amqp://*****:*****@localhost/'), heartbeat=100 ) self.connection.on_connect(self.on_connect) self.connection.on_error(self.on_error) def connect(self): logger.debug("connect") self.connection.connect() def close(self): try: self.connection.close() except Exception as err: pass def on_connect(self,connection): self.observer.on_connect(connection) def on_error(self,connection): """No error handling yet !""" pass
def test_queue_redeclare(self): """We can redeclare a queue only if we don't change its settings.""" qname = 'test-redeclare-queue' conn = Connection(self.amqp_url) with conn.channel() as channel: channel.queue_declare(queue=qname, auto_delete=False) # Test can redeclare auto_delete queue channel.queue_declare(queue=qname, auto_delete=False) with self.assertRaises(PreconditionFailed): channel.queue_declare(queue=qname, auto_delete=True)
def test_queue_redeclare(self): """We can redeclare a queue only if we don't change its settings.""" qname = 'test-redeclare-queue' conn = Connection(self.amqp_url) with conn.channel() as channel: channel.queue_declare(queue=qname, auto_delete=False) # Test can redeclare auto_delete queue channel.queue_declare(queue=qname, auto_delete=False) with self.assertRaises(PreconditionFailed): channel.queue_declare(queue=qname, auto_delete=True)
def test_queue_redeclare_args(self): """We cannot redeclare a queue if we change its arguments.""" qname = 'test-redeclare-queue-args' conn = Connection(self.amqp_url) conn.connect() with conn.channel() as channel: channel.queue_declare(queue=qname, arguments={}) with self.assertRaises(PreconditionFailed): channel.queue_declare( queue=qname, arguments={'x-expires': 101} )
def test_purge(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body=self.msg) r = channel.queue_purge(queue=self.name) self.assertEqual(r.message_count, 1) r = channel.queue_purge(queue=self.name) self.assertEqual(r.message_count, 0) channel.queue_delete(queue=self.name)
def amqp_setup(): global conn conn = Connection(AMQP_URL) with conn.channel() as channel: channel.exchange_declare("unit_test_room") channel.queue_declare(queue='listener1') channel.queue_declare(queue='listener2') channel.queue_bind(queue="listener1", exchange="unit_test_room", routing_key="user1") channel.queue_bind(queue="listener2", exchange="unit_test_room", routing_key="user2")
def test_parallel_queue_declare(self): conn = Connection(self.amqp_url) conn.connect() channel = conn.allocate_channel() def declare(name): return channel.queue_declare(queue=name) g = Group() res = g.map(declare, queues) assert len(res) == len(queues) assert all(isinstance(r, FrameQueueDeclareOk) for r in res)
def test_consume_queue(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body=self.msg) queue = channel.basic_consume(queue=self.name, no_ack=True) result = queue.get() self.assertEqual(result.body, self.msg) channel.queue_delete(queue=self.name)
def test_basic_reject_dead_letter_exchange(self): client = Connection(self.amqp_url) client.connect() product = client.server_properties.get('product') vstring = client.server_properties.get('version', '') version = tuple(int(v) for v in vstring.split('.') if v.isdigit()) if product != 'RabbitMQ' or version < (2, 8, 0): raise SkipTest( "Dead letter exchanges are only supported in RabbitMQ 2.8.0 " "and later." ) with client.channel() as channel: # Set up the dead letter exchange channel.exchange_declare(exchange='dead-letter', type='fanout') queue = channel.queue_declare(exclusive=True, auto_delete=True) dlxqname = queue.queue channel.queue_bind(queue=dlxqname, exchange='dead-letter') dead = channel.basic_consume(queue=dlxqname) # Declare a new queue and publish a message to it channel.queue_declare( queue='test-dead-letter', arguments={'x-dead-letter-exchange': 'dead-letter'} ) channel.basic_publish( exchange='', routing_key='test-dead-letter', body='a' ) # Get the message and reject it r = channel.basic_get(queue='test-dead-letter') self.assertEqual(r.body, 'a') self.assertTrue(not r.redelivered) channel.basic_reject(r.delivery_tag, requeue=False) # Check that we received it via the dead letter queue r = dead.get(timeout=5) assert r is not None self.assertEqual(r.body, 'a') self.assertEqual(r.headers['x-death'][0]['reason'], 'rejected') self.assertTrue(not r.redelivered) dead.cancel()
def test_bug3_wait(self): conn = Connection(self.amqp_url) qname = 'test-bug3-%s' % random.random() with conn.channel() as channel: channel.queue_declare(queue=qname) for i in range(3): channel.basic_publish(routing_key=qname, body='x') q = channel.basic_consume(qname) for i in range(3): msg = q.get() channel.basic_ack(msg.delivery_tag) self.assertEqual(msg.body, 'x') self._epilogue(qname, 0) conn.close()
def test_basic_return2(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.confirm_select() channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, mandatory=True, body='') with self.assertRaises(exceptions.MessageReturnedNoConsumers): channel.basic_publish(exchange='', routing_key=self.name, immediate=True, body='') with client.channel() as channel: channel.queue_delete(queue=self.name)
def test_purge(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish( exchange='', routing_key=self.name, body=self.msg ) r = channel.queue_purge(queue=self.name) self.assertEqual(r.message_count, 1) r = channel.queue_purge(queue=self.name) self.assertEqual(r.message_count, 0) channel.queue_delete(queue=self.name)
def test_basic_reject_no_requeue(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body='a') r = channel.basic_get(queue=self.name) self.assertEqual(r.body, 'a') self.assertTrue(not r.redelivered) channel.basic_reject(r.delivery_tag, requeue=False) r = channel.basic_get(queue=self.name) self.assertTrue(r is None) channel.queue_delete(queue=self.name)
def test_consume_queue(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish( exchange='', routing_key=self.name, body=self.msg ) queue = channel.basic_consume(queue=self.name, no_ack=True) result = queue.get() self.assertEqual(result.body, self.msg) channel.queue_delete(queue=self.name)
def test_basic_ack_fail(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body='a') queue = channel.basic_consume(queue=self.name) with self.assertRaises(exceptions.PreconditionFailed): channel.tx_select() channel.basic_ack(999) channel.tx_commit() with client.channel() as channel: queue = channel.basic_consume(queue=self.name) result = queue.get() channel.tx_select() channel.basic_ack(result.delivery_tag) channel.tx_commit() with self.assertRaises(exceptions.PreconditionFailed): channel.basic_ack(result.delivery_tag) channel.tx_commit() with client.channel() as channel: channel.queue_delete(queue=self.name)
def wrapper(self, *args, **kwargs): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: r = method(self, channel, *args, **kwargs) client.close() return r
def __init__(self, observer): logger.debug("observer %s" % type(observer)) self.observer = observer self.connection = Connection( os.environ.get('AMQP_URL', 'amqp://*****:*****@localhost/'), heartbeat=100 ) self.connection.on_connect(self.on_connect) self.connection.on_error(self.on_error)
def test_sync_diff_connections(): """We can pass messages between different AMQP connections.""" # publish a message to the exchange message_body = 'test_sync_diff_connections message 2' with conn.channel() as channel: channel.basic_publish(exchange='unit_test_room', routing_key='user1', body=message_body) conn2 = Connection(AMQP_URL) with conn2.channel() as channel: q = channel.basic_consume(queue='listener1') msg = q.get() # check that message was correctly published eq_(msg.body, message_body) # acknowledge the message channel.basic_ack(msg.delivery_tag)
def test_basic_reject_no_requeue(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body='a') r = channel.basic_get(queue=self.name) self.assertEqual(r.body, 'a') self.assertTrue(not r.redelivered) channel.basic_reject(r.delivery_tag, requeue=False) r = channel.basic_get(queue=self.name) self.assertTrue(r is None) channel.queue_delete(queue=self.name)
def test_basic_reject_dead_letter_exchange(self): client = Connection(self.amqp_url) client.connect() product = client.server_properties.get('product') vstring = client.server_properties.get('version', '') version = tuple(int(v) for v in vstring.split('.') if v.isdigit()) if product != 'RabbitMQ' or version < (2, 8, 0): raise SkipTest( "Dead letter exchanges are only supported in RabbitMQ 2.8.0 " "and later.") with client.channel() as channel: # Set up the dead letter exchange channel.exchange_declare(exchange='dead-letter', type='fanout') queue = channel.queue_declare(exclusive=True, auto_delete=True) dlxqname = queue.queue channel.queue_bind(queue=dlxqname, exchange='dead-letter') dead = channel.basic_consume(queue=dlxqname) # Declare a new queue and publish a message to it channel.queue_declare( queue='test-dead-letter', arguments={'x-dead-letter-exchange': 'dead-letter'}) channel.basic_publish(exchange='', routing_key='test-dead-letter', body='a') # Get the message and reject it r = channel.basic_get(queue='test-dead-letter') self.assertEqual(r.body, 'a') self.assertTrue(not r.redelivered) channel.basic_reject(r.delivery_tag, requeue=False) # Check that we received it via the dead letter queue r = dead.get(timeout=5) assert r is not None self.assertEqual(r.body, 'a') self.assertEqual(r.headers['x-death'][0]['reason'], 'rejected') self.assertTrue(not r.redelivered) dead.cancel()
def tearDown(self): conn = Connection(self.amqp_url) conn.connect() channel = conn.allocate_channel() for queue in self.declared_queues: try: channel.queue_delete(queue=queue) except Exception: channel = conn.allocate_channel() for exchange in self.declared_exchanges: try: channel.exchange_delete(exchange=exchange) except Exception: channel = conn.allocate_channel() conn.close()
def test_bug3_wait(self): conn = Connection(self.amqp_url) qname = 'test-bug3-%s' % random.random() with conn.channel() as channel: channel.queue_declare(queue=qname) for i in range(3): channel.basic_publish( routing_key=qname, body='x' ) q = channel.basic_consume(qname) for i in range(3): msg = q.get() channel.basic_ack(msg.delivery_tag) self.assertEqual(msg.body, 'x') self._epilogue(qname, 0) conn.close()
def test_sync_diff_connections(): """We can pass messages between different AMQP connections.""" # publish a message to the exchange message_body = 'test_sync_diff_connections message 2' with conn.channel() as channel: channel.basic_publish( exchange='unit_test_room', routing_key='user1', body=message_body ) conn2 = Connection(AMQP_URL) with conn2.channel() as channel: q = channel.basic_consume(queue='listener1') msg = q.get() # check that message was correctly published eq_(msg.body, message_body) # acknowledge the message channel.basic_ack(msg.delivery_tag)
def test_basic_ack_fail(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, body='a') queue = channel.basic_consume(queue=self.name) with self.assertRaises(exceptions.PreconditionFailed): channel.tx_select() channel.basic_ack(999) channel.tx_commit() with client.channel() as channel: queue = channel.basic_consume(queue=self.name) result = queue.get() channel.tx_select() channel.basic_ack(result.delivery_tag) channel.tx_commit() with self.assertRaises(exceptions.PreconditionFailed): channel.basic_ack(result.delivery_tag) channel.tx_commit() with client.channel() as channel: channel.queue_delete(queue=self.name)
def test_basic_get_ack(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) for i in range(4): channel.basic_publish(exchange='', routing_key=self.name, body=self.msg + str(i)) msgs = [] for i in range(4): result = channel.basic_get(queue=self.name) self.assertEqual(result.body, self.msg + str(i)) self.assertEqual(result.redelivered, False) msgs.append(result) result = channel.basic_get(queue=self.name) self.assertTrue(result is None) channel.queue_delete(queue=self.name)
def amqp_teardown(): global conn conn.close() conn2 = Connection(AMQP_URL) with conn2.channel() as channel: channel.exchange_delete("unit_test_room") with conn2.channel() as channel: channel.queue_delete(queue='listener1') with conn2.channel() as channel: channel.queue_delete(queue='listener2') conn2.close()
def test_bug3_loop(self): i = [0] conn = Connection(self.amqp_url) qname = 'test-bug3-%s' % random.random() def cb(msg): i[0] += 1 msg.ack() self.assertEqual(msg.body, 'x') if i[0] == 3: conn.close() channel = conn.allocate_channel() channel.queue_declare(queue=qname) for _ in range(3): channel.basic_publish(routing_key=qname, body='x') channel.basic_consume(queue=qname, callback=cb) conn.join() self._epilogue(qname, 0)
def test_queue_bind(self): "We can bind and unbind, and the queue receives messages when bound." qname = 'test-queue-bind' conn = Connection(self.amqp_url) with conn.channel() as channel: channel.queue_declare(queue=qname) channel.exchange_declare(exchange=qname, type='direct') channel.basic_publish(exchange=qname, routing_key=qname, body='a') channel.queue_bind(exchange=qname, queue=qname, routing_key=qname) channel.basic_publish(exchange=qname, routing_key=qname, body='b') channel.queue_unbind(exchange=qname, queue=qname, routing_key=qname) channel.basic_publish(exchange=qname, routing_key=qname, body='c') msg = channel.basic_get(queue=qname) self.assertEquals(msg.body, 'b') self.assertEquals(msg['message_count'], 0)
def test_basic_get_ack(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.queue_declare(queue=self.name) for i in range(4): channel.basic_publish(exchange='', routing_key=self.name, body=self.msg + str(i)) msgs = [] for i in range(4): result = channel.basic_get(queue=self.name) self.assertEqual(result.body, self.msg + str(i)) self.assertEqual(result.redelivered, False) msgs.append(result) result = channel.basic_get(queue=self.name) self.assertTrue(result is None) channel.queue_delete(queue=self.name)
def amqp_teardown(): global conn conn.close() conn2 = Connection(AMQP_URL) with conn2.channel() as channel: channel.exchange_delete("unit_test_room") with conn2.channel() as channel: channel.queue_delete(queue='listener1') with conn2.channel() as channel: channel.queue_delete(queue='listener2') conn2.close()
def test_broken_ack_on_close(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: decl = channel.queue_declare() qname = decl.queue channel.basic_publish(exchange='', routing_key=qname, body='a') r = channel.basic_get(queue=qname) self.assertEquals(r.body, 'a') channel.queue_delete(queue=qname) client.close()
def test_basic_return2(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.confirm_select() channel.queue_declare(queue=self.name) channel.basic_publish(exchange='', routing_key=self.name, mandatory=True, body='') with self.assertRaises(exceptions.MessageReturnedNoConsumers): channel.basic_publish(exchange='', routing_key=self.name, immediate=True, body='') with client.channel() as channel: channel.queue_delete(queue=self.name)