Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move specs from node specs to vhost stats spec #643

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 0 additions & 87 deletions spec/api/node_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,5 @@ describe LavinMQ::HTTP::NodesController do
data.has_key?(key).should be_true
end
end

it "should update queue data" do
Server.update_stats_rates

response = get("/api/nodes")
body = JSON.parse(response.body)
data = body.as_a.first.as_h
declared_queues = data["queue_declared"].as_i
deleted_queues = data["queue_deleted"].as_i
Server.vhosts["/"].declare_queue("this_queue_should_not_exist", false, false)
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
body.as_a.empty?.should be_false
data = body.as_a.first.as_h
data["queue_declared"].as_i.should eq(declared_queues + 1)
data["queue_deleted"].as_i.should eq deleted_queues
Server.vhosts["/"].delete_queue("this_queue_should_not_exist")
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
body.as_a.empty?.should be_false
data = body.as_a.first.as_h
data["queue_declared"].as_i.should eq(declared_queues + 1)
data["queue_deleted"].as_i.should eq(deleted_queues + 1)
end

it "should not delete stats when connection is closed" do
wait_for { Server.connections.sum(&.channels.size).zero? }
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
data = body.as_a.first.as_h
channels_created = data["channel_created"].as_i
channels_closed = data["channel_closed"].as_i
with_channel do
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
data = body.as_a.first.as_h
data["channel_created"].as_i.should eq(channels_created + 1)
data["channel_closed"].as_i.should eq(channels_closed)
end
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
data = body.as_a.first.as_h
data["channel_created"].as_i.should eq(channels_created + 1)
data["channel_closed"].as_i.should eq(channels_closed + 1)
end

it "should count connections open / closed once" do
response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
data = body.as_a.first.as_h
connections_created = data["connection_created"].as_i
connections_closed = data["connection_closed"].as_i
with_channel do
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
data = body.as_a.first.as_h
data["connection_created"].as_i.should eq(connections_created + 1)
data["connection_closed"].as_i.should eq(connections_closed)
end
Server.update_stats_rates

response = get("/api/nodes")
response.status_code.should eq 200
body = JSON.parse(response.body)
data = body.as_a.first.as_h
data["connection_created"].as_i.should eq(connections_created + 1)
data["connection_closed"].as_i.should eq(connections_closed + 1)
end
end
end
63 changes: 63 additions & 0 deletions spec/vhost_stats_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "./spec_helper"
require "../src/lavinmq/config"

describe LavinMQ::VHost do
describe "Stats" do
it "should update queue data" do
vhost = Server.vhosts["/"]
Server.update_stats_rates

initial_declared_queues = vhost.queue_declared_count
initial_deleted_queues = vhost.queue_deleted_count
Server.vhosts["/"].declare_queue("this_queue_should_not_exist", false, false)
Server.update_stats_rates

vhost.queue_declared_count.should eq(initial_declared_queues + 1)
vhost.queue_deleted_count.should eq initial_deleted_queues
Server.vhosts["/"].delete_queue("this_queue_should_not_exist")
Server.update_stats_rates

vhost.queue_declared_count.should eq(initial_declared_queues + 1)
vhost.queue_deleted_count.should eq(initial_deleted_queues + 1)
end

it "should not delete stats when connection is closed" do
vhost = Server.vhosts["/"]
wait_for { Server.connections.sum(&.channels.size).zero? }

Server.update_stats_rates
initial_channels_created = vhost.channel_created_count
initial_channels_closed = vhost.channel_closed_count

with_channel do |ch|
Server.update_stats_rates

vhost.channel_created_count.should eq(initial_channels_created + 1)
vhost.channel_closed_count.should eq(initial_channels_closed)
ch.close
end

Server.update_stats_rates
vhost.channel_created_count.should eq(initial_channels_created + 1)
vhost.channel_closed_count.should eq(initial_channels_closed + 1)
end

it "should count connections open / closed once" do
vhost = Server.vhosts["/"]
initial_connections_created = vhost.connection_created_count
initial_connections_closed = vhost.connection_closed_count

with_channel do
Server.update_stats_rates
vhost.connection_created_count.should eq(initial_connections_created + 1)
vhost.connection_closed_count.should eq(initial_connections_closed)
end

wait_for { [email protected]? }

Server.update_stats_rates
vhost.connection_created_count.should eq(initial_connections_created + 1)
vhost.connection_closed_count.should eq(initial_connections_closed + 1)
end
end
end
Loading