Skip to content

Commit

Permalink
Move specs from node specs to vhost stats spec (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun authored and viktorerlingsson committed Sep 19, 2024
1 parent a397bff commit 1025f5e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 87 deletions.
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 { vhost.@connections.empty? }

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

0 comments on commit 1025f5e

Please sign in to comment.