Skip to content

Instantly share code, notes, and snippets.

@randomcamel
Created January 4, 2012 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save randomcamel/1558556 to your computer and use it in GitHub Desktop.
Save randomcamel/1558556 to your computer and use it in GitHub Desktop.
Script to load a queue and then cause the error 'Error subscribing to queue qtest.broadcast (Bunny::ProtocolError)'
#!/usr/bin/env ruby
require 'rubygems'
require 'bunny'
# Chris Doherty, confused by Bunny::Queue#subscribe semantics.
# run with `ruby bunny_test.rb load` followed by `ruby bunny_test.rb consume`.
class BunnyTest
def initialize(messaging_host, queues, options = {})
@bunny = Bunny.new :host => messaging_host, :logging => (options[:logging] || false)
@bunny.start
# declare queues.
@queues = {}
queues.each do |queue|
@queues[queue] = @bunny.queue queue, :auto_delete => false, :exclusive => false
end
# bind to default exchange. this is a direct exchange: no use for topic or fanout yet.
@exchange = @bunny.exchange('')
# @bunny.qos :prefetch_count => 1
@consumer_tag = "#{$$}-#{self.object_id}"
end
def send_msg(routing_key, msg)
@exchange.publish msg, :key => routing_key
end
def fetch(queue)
output = @queues[queue].pop
return output
end
def fetch_wait(queue)
@queues[queue].subscribe( :consumer_tag => @consumer_tag ) do |output|
return output
end
end
def load_queue(count)
count.times do |i|
send_msg SEND_KEY, "message -> #{i}, sending pid -> #{$$}"
end
end
def consume_queue(limit=1000)
10000.times do
output = fetch_wait RECV_Q
puts "Received: #{output.inspect}"
end
end
end
trap(:INT) { exit! }
SEND_KEY = RECV_Q = "qtest.broadcast"
worker = BunnyTest.new 'localhost', RECV_Q
case ARGV[0]
when "load"
worker.load_queue 10
when "consume"
worker.consume_queue
else
puts "'load' or 'consume' only."
end
Received: {:header=>#<Qrack::Protocol::Header:0x1080b1248 @weight=0, @properties={:content_type=>"application/octet-stream", :delivery_mode=>1, :priority=>0}, @size=34, @klass=Qrack::Protocol::Basic>, :payload=>"message -> 0, sending pid -> 23355", :delivery_details=>{:consumer_tag=>"23388-2213936240", :exchange=>"", :delivery_tag=>1, :routing_key=>"qtest.broadcast", :redelivered=>false}}
/Users/randomcamel/.rbenv/versions/1.8.7-p352/lib/ruby/gems/1.8/gems/bunny-0.7.8/lib/bunny/client08.rb:81:in `check_response': Error subscribing to queue qtest.broadcast (Bunny::ProtocolError)
from /Users/randomcamel/.rbenv/versions/1.8.7-p352/lib/ruby/gems/1.8/gems/bunny-0.7.8/lib/bunny/subscription08.rb:80:in `setup_consumer'
from /Users/randomcamel/.rbenv/versions/1.8.7-p352/lib/ruby/gems/1.8/gems/bunny-0.7.8/lib/qrack/subscription.rb:53:in `consume'
from /Users/randomcamel/.rbenv/versions/1.8.7-p352/lib/ruby/gems/1.8/gems/bunny-0.7.8/lib/bunny/queue08.rb:311:in `subscribe'
from qtest.rb:35:in `fetch_wait'
from qtest.rb:49:in `consume_queue'
from qtest.rb:48:in `times'
from qtest.rb:48:in `consume_queue'
from qtest.rb:65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment