Skip to content

Commit

Permalink
Implement Queue#reject
Browse files Browse the repository at this point in the history
  • Loading branch information
soupmatt committed Sep 4, 2012
1 parent 60b7e1b commit dcc32aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/bunny/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ def ack(opts = {})
self.delivery_tag = nil
end

# Rejects one or more messages delivered via the _Deliver_ or _Get_-_Ok_ methods. The client can
# ask to reject a single message.
#
# @option opts [String] :delivery_tag
#
# @option opts [Boolean] :requeue (true)
# If set to @true@, the server will attempt to requeue the message.
# If requeue is false or the requeue attempt fails the messages are
# discarded or dead-lettered.
def reject(opts = {})
# Set delivery tag
if delivery_tag.nil? and opts[:delivery_tag].nil?
raise Bunny::RejectionError, "No delivery tag received"
else
self.delivery_tag = opts[:delivery_tag] if delivery_tag.nil?
end

opts = {:delivery_tag => delivery_tag, :requeue => true}.merge(opts)

client.send_frame(Qrack::Protocol::Basic::Reject.new(opts))

# reset delivery tag
self.delivery_tag = nil
end

# Binds a queue to an exchange. Until a queue is bound it won't receive
# any messages. Queues are bound to the direct exchange '' by default.
# If error occurs, a {Bunny::ProtocolError} is raised.
Expand Down
22 changes: 22 additions & 0 deletions spec/spec_09/queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ def message_count(queue, sleep_time = 0.1)
q.purge.should == :purge_ok
end

it "should put a message back when it is rejected" do
q = @b.queue('test1')
5.times { @default_exchange.publish('Yet another test message', :key => 'test1') }
message_count(q).should == 5
msg = q.pop(:ack => true)
message_count(q).should == 4
q.reject
message_count(q).should == 5
q.purge.should == :purge_ok
end

it "should discard the message when it is rejected with requeue set false" do
q = @b.queue('test1')
5.times { @default_exchange.publish('Yet another test message', :key => 'test1') }
message_count(q).should == 5
msg = q.pop(:ack => true)
message_count(q).should == 4
q.reject(:requeue => false)
message_count(q).should == 4
q.purge.should == :purge_ok
end

it "should raise an error when delete fails" do
q = @b.queue('test1')
lambda {q.delete(:queue => 'bogus')}.should raise_error(Bunny::ForcedChannelCloseError)
Expand Down

0 comments on commit dcc32aa

Please sign in to comment.