Specs using AMQP

22 views
Skip to first unread message

Arvicco

unread,
Oct 12, 2010, 6:18:54 AM10/12/10
to AMQP
Guys,

I've been trying to write some specs using AMQP and for me, it was
extremely frustrating experience. Even simplest of specs seem to fall
apart for some mysterious reasons. Take this dead-simple spec for
example (http://gist.github.com/621931):

require 'mq'

describe 'AMQP' do
it('sends data to queue') do
EM.run do
AMQP.start(logging: false) do
q = MQ.new.queue("test_sink")
q.subscribe do |hdr, data|
data.should == 'data'
EM.next_tick {
q.unsubscribe; q.delete
AMQP.stop { EM.stop }
}
end
end
EM.add_timer(0.2) do
MQ.queue('test_sink').publish 'data'
end
end
end

Absolutely nothing to it, just create new queue, subscribe to it and
publish some data into it. Make sure consumer received the data and
kill the loop. It passes (hoorray!). BUT... If I make a copy of this
correct example and try to run two examples at once, it just hangs on
second one. AMQP logging shows that data is published, but never
received...

What on Earth is wrong with this? Could some AMQP guru show me where
am I missing some secret ingredient? ;)

I know, some people say - just mock the hell out of AMQP using Moque
or whatever, and only rely on actual broker in your integration
tests... This approach may be ok for some cases, but not for all...
Why can't I just use AMQP loops in a simple controlled way such as
above? :(

Aman Gupta

unread,
Oct 12, 2010, 1:37:29 PM10/12/10
to ruby...@googlegroups.com
I think you need to

      Thread.current[:mq] = nil 

Since you're using MQ.queue. This should probably be cleaned up automatically in AMQP.stop or when the reactor goes down.

  Aman


--
You received this message because you are subscribed to the Google Groups "AMQP" group.
To post to this group, send email to ruby...@googlegroups.com.
To unsubscribe from this group, send email to ruby-amqp+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-amqp?hl=en.


Vlad Tepes

unread,
Oct 12, 2010, 2:19:40 PM10/12/10
to ruby...@googlegroups.com
RSpec does not play nicely with EventMachine. There's a gem called em-spec that may help you. It didn't work at all for my purposes--I eventually ended up writing my code to use bunny for testing purposes, and the AMQP gem otherwise. Far from ideal, but it did work.

Daniel DeLeo

unread,
Oct 13, 2010, 11:06:27 PM10/13/10
to ruby...@googlegroups.com
On Tue, Oct 12, 2010 at 11:19 AM, Vlad Tepes <agyar...@gmail.com> wrote:
> RSpec does not play nicely with EventMachine. There's a gem called em-spec
> that may help you. It didn't work at all for my purposes--I eventually ended
> up writing my code to use bunny for testing purposes, and the AMQP gem
> otherwise. Far from ideal, but it did work.
>

I have some rspec tests running against a live broker. I monkey
patched this code in:

module ::AMQP
def self.hard_reset!
MQ.reset
stop
EM.stop
Thread.current[:mq], @conn = nil, nil
end
end

And then I call it from an EM timer to end the test:

EM.add_timer(0.1) do
AMQP.hard_reset!
end


Makes the tests that use it relatively slow, but it works.

Dan DeLeo

Arvicco

unread,
Oct 14, 2010, 11:10:04 AM10/14/10
to AMQP
Thanks a lot for your tips, guys!

Seems like most of my problems were caused by AMQP state leaking
between examples...
Using code similar to Daniel's resolved most of them. EM-Spec was
somewhat helpful,
but unfortunately it was not easy to properly setup/stop AMQP loop
with it. Also, its
handling of exceptions was not that great.

I've put together the things I've learned about writing decent evented
AMQP specs
into new 'amqp-spec' gem. It's largely based on em-spec but handles
AMQP-specific
setup/teardown better. Hopefully, it'll make it easier for other guys
to write specs
against live broker if their use case so requires.
Reply all
Reply to author
Forward
0 new messages