def test_rabbitmq_clear_queues(rabbitmq, rabbitmq_proc): """ Declare queue, and clear it by clear_rabbitmq. """ from rabbitpy import Queue channel = rabbitmq.channel() assert channel.state == channel.OPEN # list queues no_queues = rabbitmq_proc.list_queues() assert not no_queues # declare queue, and get new output queue = Queue(channel, 'fastlane') queue.declare() queues = rabbitmq_proc.list_queues() assert len(queues) > 0 # make sure it's different and clear it assert queues != no_queues clear_rabbitmq(rabbitmq_proc, rabbitmq) # list_queues again and make sure it's empty cleared_queues = rabbitmq_proc.list_queues() assert no_queues == cleared_queues
def test_publish(rabbitmq, coney): channel = rabbitmq.channel() exchange = Exchange(channel, "my-exchange", "direct") exchange.declare() queue = Queue(channel, "my-queue") queue.declare() queue.bind(exchange, "my-routing-key") coney.publish("Hi", exchange_name="my-exchange", routing_key="my-routing-key") for message in queue.consume(): assert message.body == b"Hi" queue.stop_consuming()
def clear_rabbitmq(process, rabbitmq_connection): """ Clear queues and exchanges from given rabbitmq process. :param RabbitMqExecutor process: rabbitmq process :param rabbitpy.Connection rabbitmq_connection: connection to rabbitmq """ from rabbitpy import Exchange, Queue channel = rabbitmq_connection.channel() process.set_environ() for exchange in process.list_exchanges(): if exchange.startswith('amq.'): # ---------------------------------------------------------------- # From rabbit docs: # https://www.rabbitmq.com/amqp-0-9-1-reference.html # ---------------------------------------------------------------- # Exchange names starting with "amq." are reserved for pre-declared # and standardised exchanges. The client MAY declare an exchange # starting with "amq." if the passive option is set, or the # exchange already exists. Error code: access-refused # ---------------------------------------------------------------- continue ex = Exchange(channel, exchange) ex.delete() for queue in process.list_queues(): if queue.startswith('amq.'): # ---------------------------------------------------------------- # From rabbit docs: # https://www.rabbitmq.com/amqp-0-9-1-reference.html # ---------------------------------------------------------------- # Queue names starting with "amq." are reserved for pre-declared # and standardised queues. The client MAY declare a queue starting # with "amq." if the passive option is set, or the queue already # exists. Error code: access-refused # ---------------------------------------------------------------- continue qu = Queue(channel, queue) qu.delete()
def factory(name, exchange, routing_key): channel = rabbitmq.channel() exchange = Exchange(channel, exchange, auto_delete=False, durable=True) exchange.declare() assert exchange.name in rabbitmq_proc.list_exchanges() queue = Queue(channel, name, auto_delete=False, durable=True) queue.declare() queue.bind(exchange, routing_key=routing_key) assert name in rabbitmq_proc.list_queues() return exchange, queue