Test::Net::RabbitMQ - A mock RabbitMQ implementation for use when testing.
use Test::Net::RabbitMQ; my $mq = Test::Net::RabbitMQ->new; $mq->connect; $mq->channel_open(1); $mq->exchange_declare(1, 'order'); $mq->queue_declare(1, 'new-orders'); $mq->queue_bind(1, 'new-orders', 'order', 'order.new'); $mq->publish(1, 'order.new', 'hello!', { exchange => 'order' }); $mq->consume(1, 'new-orders'); my $msg = $mq->recv; # Or my $msg = $mq->get(1, 'order.new', {});
Test::Net::RabbitMQ is a terrible approximation of using the real thing, but hopefully will allow you to test systems that use Net::RabbitMQ without having to use an actual RabbitMQ instance.
The general overview is that calls to publish pushes a message into one or more queues (or none if there are no bindings) and calls to recv pop them.
publish
recv
This module has all the features I've needed to successfully test our RabbitMQ-using application. Patches are welcome if I'm missing something you need! At the moment there are a number of shortcomings:
If false then any calls to connect will die to emulate a failed connection.
If set to true (which you can do at any time) then a message will be emmitted to STDERR any time a message is added to a queue.
Opens a channel with the specific number.
Closes the specific channel.
Connects this instance. Does nothing except set connected to true. Will throw an exception if you've set connectable to false.
connected
connectable
Sets the queue that will be popped when recv is called.
Disconnects this instance by setting connected to false.
Creates an exchange of the specified name.
Get a message from the queue, if there is one.
Like Net::RabbitMQ, this will return a hash containing the following information:
Net::RabbitMQ
{ body => 'Magic Transient Payload', # the reconstructed body routing_key => 'nr_test_q', # route the message took exchange => 'nr_test_x', # exchange used delivery_tag => 1, # (inc'd every recv or get) redelivered => 0, # always 0 message_count => 0, # always 0 }
Binds the specified queue to the specified exchange using the provided routing key. Note that, at the moment, this doesn't work with AMQP wildcards. Only with exact matches of the routing key.
Creates a queue of the specified name.
Unbinds the specified routing key from the provided queue and exchange.
Publishes the specified body with the supplied routing key. If there is a binding that matches then the message will be added to the appropriate queue(s).
Provided you've called consume then calls to recv will pop the next message of the queue. Note that this method does not block.
consume
pop
{ body => 'Magic Transient Payload', # the reconstructed body routing_key => 'nr_test_q', # route the message took exchange => 'nr_test_x', # exchange used delivery_tag => 1, # (inc'd every recv or get) consumer_tag => '', # Always blank currently props => $props, # hashref sent in }
Cory G Watson, <gphat at cpan.org>
<gphat at cpan.org>
Copyright 2010 Cory G Watson.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Crixa, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Crixa
CPAN shell
perl -MCPAN -e shell install Crixa
For more information on module installation, please visit the detailed CPAN module installation guide.