Created
February 17, 2012 15:28
-
-
Save thom-nic/1854024 to your computer and use it in GitHub Desktop.
Testing ruby-amqp and evented-spec with MiniTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'evented-spec' | |
require 'minitest/autorun' | |
require "amqp" | |
class TestConsumer | |
attr_reader :connection_string, :queue_name, | |
:connection, :channel, :queue, | |
:msg_count, :errors | |
def initialize opts={} | |
@connection_string = opts[:connection_string] || 'amqp://localhost' | |
@queue_name = opts[:queue_name] || '' | |
@queue_opts = opts[:queue_opts] || {} | |
@msg_count = 0 | |
@errors = 0 | |
end | |
def start | |
AMQP.connect @connection_string do |conn, connect_ok| | |
@connection = conn | |
AMQP::Channel.new @connection do |channel, channel_ok| | |
@channel = channel | |
@channel.queue @queue_name, @queue_opts do |queue| | |
@queue = queue | |
@queue_name = queue.name | |
@queue.subscribe do |meta, payload| | |
begin | |
self.on_message meta, payload | |
metadata.ack | |
rescue Exception => err | |
self.on_error meta, payload, err | |
# TODO automatic retry/ backout logic | |
end | |
end | |
end | |
end | |
end | |
end | |
def on_message meta, payload | |
raise ArgumentError, "Boom!" if payload == "Exception!" | |
unless payload.nil? | |
@msg_count += 1 | |
end | |
end | |
def on_error meta, payload, err | |
@errors += 1 | |
meta.ack # don't leave msg on the queue | |
end | |
end | |
describe TestConsumer do | |
include EventedSpec::AMQPSpec | |
default_timeout 5 | |
amqp_before do | |
@q_name = "test.spec" | |
@channel = AMQP::Channel.new | |
@exchange = @channel.default_exchange | |
end | |
amqp_before :each do | |
@consumer = TestConsumer.new :queue_name => @q_name | |
@consumer.start | |
end | |
amqp_after :each do | |
# @consumer.connection.disconnect | |
end | |
# context "when executing default behavior" do | |
it "will have the correct initial state" do | |
@consumer.connection_string.must_equal "amqp://localhost" | |
@consumer.queue_name.must_equal @q_name | |
@consumer.msg_count.must_equal 0 | |
@consumer.errors.must_equal 0 | |
done(0.1) | |
end | |
it "must start up correctly" do | |
done(0.3) { | |
@consumer.connection.wont_be_nil | |
@consumer.channel.wont_be_nil | |
@consumer.queue.wont_be_nil | |
@consumer.connection.connected?.must_equal true | |
} | |
end | |
it "should increment the counter" do | |
@channel.queue @q_name do |q| | |
q.purge # purge old messages first. | |
@exchange.publish "hi", :routing_key => @q_name | |
end | |
done(0.3) { | |
@consumer.msg_count.must_equal 1 | |
@consumer.errors.must_equal 0 | |
} | |
end | |
it "should catch an exception" do | |
@channel.queue @q_name do |q| | |
q.purge # purge old messages first. | |
@exchange.publish "Exception!", :routing_key => @q_name | |
end | |
done(0.3) { | |
@consumer.msg_count.must_equal 0 | |
@consumer.errors.must_equal 1 | |
} | |
end | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment