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_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_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_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_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_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_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_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 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 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)
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_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_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 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_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_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_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_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 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_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_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_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_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_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 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_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 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 test_close(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.channel_close()
def test_close(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: channel.channel_close()
logging.basicConfig() logger = logging.getLogger('test_app') logger.setLevel(logging.DEBUG) conn = Connection( os.environ.get('AMQP_URL', 'amqp://*****:*****@localhost/'), heartbeat=5 ) @conn.on_connect def on_connect(conn): # send some test messages with conn.channel() as channel: channel.basic_publish( exchange='manage', routing_key='all.apps.launch', body='{"application": "apps.pinger.pinger"}', ) gevent.sleep(3.0) conn.close() if __name__ == '__main__': conn.connect() try: conn.join() except KeyboardInterrupt: conn.close()
@conn.on_connect def on_connect(conn): global last_num with conn.channel() as channel: channel.exchange_declare(exchange='example', type='direct', durable=True) channel.confirm_select() while True: channel.basic_publish(exchange='example', routing_key='counter', body=str(last_num), headers={ 'delivery_mode': 2, 'time': str(time.time()) }) print(datetime.datetime.now().strftime('[%H:%M:%S]'), "Published message '%d'" % last_num) last_num += 1 gevent.sleep(1) if __name__ == '__main__': conn.connect() try: conn.join() except KeyboardInterrupt: conn.close()