Skip to content

Commit

Permalink
Fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun committed Aug 23, 2024
1 parent fccfff1 commit 3199832
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
33 changes: 16 additions & 17 deletions spec/amqp-client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,15 @@ describe AMQP::Client do
end
end

it "should set connection name" do
it "should set connection name", tags: "slow" do
AMQP::Client.start(name: "My Name") do |_|
names = Array(String).new
5.times do
HTTP::Client.get("http://guest:guest@#{AMQP::Client::AMQP_HOST}:15672/api/connections") do |resp|
conns = JSON.parse resp.body_io
names = conns.as_a.map &.dig("client_properties", "connection_name")
with_http_api do |api|
5.times do
names = api.connections.map &.dig("client_properties", "connection_name")
break if names.includes? "My name"
sleep 1
end
sleep 1
end
names.should contain "My Name"
end
Expand Down Expand Up @@ -413,39 +412,39 @@ describe AMQP::Client do
end
end

describe "Channel" do
it "#basic_consume block=true will raise Connection::ClosedException if broker closed connection" do
describe "Channel raises Connection::ClosedException", tags: "slow" do
it "#basic_consume block=true" do
with_channel do |ch|
ch.queue_declare("foobar")
ch.basic_publish "", "", "foobar"
q = ch.queue
q.publish "foobar"
expect_raises(AMQP::Client::Connection::ClosedException) do
ch.basic_consume "foobar", block: true do
ch.basic_consume q.name, block: true do
with_http_api do |api|
api.close_all_connections
api.close_connections(1)
end
end
end
end
end

it "#basic_publish will raise Connection::ClosedException if broker closed connection" do
it "#basic_publish" do
with_channel do |ch|
with_http_api do |api|
api.close_all_connections
api.close_connections(1)
end
expect_raises(AMQP::Client::Connection::ClosedException) do
ch.basic_publish "", "", "foobar"
end
end
end

it "#basic_publish_confirm will raise Connection::ClosedException if broker closed connection" do
it "#basic_publish_confirm" do
with_channel do |ch|
with_http_api do |api|
api.close_all_connections
api.close_connections(1)
end
expect_raises(AMQP::Client::Connection::ClosedException) do
ch.basic_publish "", "", "foobar"
ch.basic_publish_confirm "", "", "foobar"
end
end
end
Expand Down
27 changes: 18 additions & 9 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,38 @@ module TestHelpers
end

struct ManagementApi
def initialize(host, port)
@http = HTTP::Client.new("localhost", 15672)
@http.basic_auth "guest", "guest"
def initialize(uri : URI)
@http = HTTP::Client.new uri
username = uri.user || "guest"
password = uri.password || "guest"
@http.basic_auth username, password
end

def connections
get("/api/connections") do |resp|
get("/api/connections/") do |resp|
JSON.parse(resp.body_io).as_a
end
end

def close_all_connections
connections.each do |conn|
name = conn["name"].as_s
delete("/api/connections/#{URI.encode_path_segment name}")
def close_connections(amount : Int)
loop do
conns = connections
conns.each do |conn|
name = conn["name"].as_s
delete("/api/connections/#{URI.encode_path_segment name}")
amount -= 1
end
break if amount <= 0
sleep 1
end
end

forward_missing_to @http
end

def with_http_api(&)
yield ManagementApi.new("localhost", 15671)
uri = URI.parse ENV.fetch("MGMT_URL", "http://guest:guest@#{AMQP::Client::AMQP_HOST}:15672")
yield ManagementApi.new(uri)
end
end

Expand Down

0 comments on commit 3199832

Please sign in to comment.