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
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 test_rabbitmq_clear_exchanges(rabbitmq, rabbitmq_proc): """Declare exchange, and clear it by clear_rabbitmq.""" channel = rabbitmq.channel() assert channel.state == channel.OPEN # list exchanges no_exchanges = rabbitmq_proc.list_exchanges() # declare exchange and list exchanges afterwards exchange = Exchange(channel, 'cache-in') exchange.declare() exchanges = rabbitmq_proc.list_exchanges() # make sure it differs assert exchanges != no_exchanges clear_rabbitmq(rabbitmq_proc, rabbitmq) # list_exchanges again and make sure it's empty cleared_exchanges = rabbitmq_proc.list_exchanges() assert no_exchanges == cleared_exchanges
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 test_rabbitmq_clear_exchanges(rabbitmq, rabbitmq_proc): """ Declare exchange, and clear it by clear_rabbitmq. """ from rabbitpy import Exchange channel = rabbitmq.channel() assert channel.state == channel.OPEN # list exchanges no_exchanges = rabbitmq_proc.list_exchanges() # declare exchange and list exchanges afterwards exchange = Exchange(channel, 'cache-in') exchange.declare() exchanges = rabbitmq_proc.list_exchanges() # make sure it differs assert exchanges != no_exchanges clear_rabbitmq(rabbitmq_proc, rabbitmq) # list_exchanges again and make sure it's empty cleared_exchanges = rabbitmq_proc.list_exchanges() assert no_exchanges == cleared_exchanges