Skip to content

Commit

Permalink
Merge branch 'BranchIntl-delete_queue_if_unused_if_empty'
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Jan 29, 2024
2 parents 74a5f33 + 3addf94 commit 72bbdf4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/rabbitmq/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ def declare_queue(vhost, name, attributes)
decode_resource(response)
end

def delete_queue(vhost, name)
decode_resource(@connection.delete("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"))
def delete_queue(vhost, name, if_unused = false, if_empty = false)
response = @connection.delete("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}") do |req|
req.params["if-unused"] = true if if_unused
req.params["if-empty"] = true if if_empty
end
decode_resource(response)
end

def list_queue_bindings(vhost, queue, query = {})
Expand Down
8 changes: 7 additions & 1 deletion lib/rabbitmq/http/client/response_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ def decode_response_body(body)
def decode_resource_collection(response)
collection = response.body.is_a?(Array) ? response.body : response.body.fetch('items')

collection.map { |i| Hashie::Mash.new(i) }
collection.map do |i|
if i == []
Hashie::Mash.new()
else
Hashie::Mash.new(i)
end
end
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/integration/api_endpoints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,35 @@ def await_event_propagation
it "deletes a queue" do
q = @channel.queue(queue_name, durable: false)
subject.delete_queue("/", queue_name)
expect { subject.queue_info("/", queue_name) }.to raise_error(Faraday::ResourceNotFound)
end

it "doesn't delete non-empty queue if if-empty is set" do
q = @channel.queue(queue_name, durable: false)
q.publish("hello")
expect do
subject.delete_queue("/", queue_name, false, true)
end.to raise_error(Faraday::ClientError)

subject.purge_queue("/", q.name)
subject.delete_queue("/", queue_name, false, true)
expect { subject.queue_info("/", queue_name) }.to raise_error(Faraday::ResourceNotFound)
end

it "doesn't delete used queue if if-unused is set" do
q = @channel.queue(queue_name, durable: false)
# Simulate the queue being used by creating a consumer
consumer = q.subscribe do |_delivery_info, _properties, _body|
# consumer block
end

expect do
subject.delete_queue("/", queue_name, true, false)
end.to raise_error(Faraday::ClientError)

consumer.cancel
subject.delete_queue("/", queue_name, true, false)
expect { subject.queue_info("/", queue_name) }.to raise_error(Faraday::ResourceNotFound)
end
end

Expand Down

0 comments on commit 72bbdf4

Please sign in to comment.