Skip to content

Commit

Permalink
Close dangling nats threads before #close and #reconnect test
Browse files Browse the repository at this point in the history
  • Loading branch information
vankiru committed Jan 10, 2025
1 parent 2d82317 commit f727700
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
47 changes: 31 additions & 16 deletions spec/client_reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -620,27 +620,42 @@
end
end

it "closes all its threads before reconnection" do
responder = NATS.connect
requester = NATS.connect(
reconnect: true,
reconnect_time_wait: 2,
max_reconnect_attempts: 1
)
describe "#process_op_error" do
# Close all dangling nats threads from previous tests
before do
Thread.list.each do |thread|
thread.exit if thread.name&.start_with?("nats:")
end
end

responder.subscribe("foo") { |msg| msg.respond("bar") }
requester.request("foo")
let(:responder) { NATS.connect }

nats_threads = Thread.list.select do |thread|
thread.name&.start_with?("nats:")
let(:requester) do
NATS.connect(
reconnect: true,
reconnect_time_wait: 2,
max_reconnect_attempts: 1
)
end

@s.restart
sleep 2
it "closes all its threads before reconnection" do
responder.subscribe("foo") { |msg| msg.respond("bar") }
requester.request("foo")

nats_threads = Thread.list.select do |thread|
thread.name&.start_with?("nats:")
end

expect(Thread.list & nats_threads).to be_empty
@s.restart
sleep 2

responder.close
requester.close
pp "RECONNECT"
pp "NATS: #{nats_threads.map{|t|[t.name, t.object_id]}}"
pp "Thread: #{Thread.list.map{|t|[t.name, t.object_id]}}"
expect(Thread.list & nats_threads).to be_empty

responder.close
requester.close
end
end
end
10 changes: 10 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@

describe "#close" do
context "when client has established a connection" do
before do
# Close all dangling nats threads from previous tests
Thread.list.each do |thread|
thread.exit if thread.name&.start_with?("nats:")
end
end

let(:responder) { NATS.connect(servers: [@s.uri]) }
let(:requester) { NATS.connect(servers: [@s.uri]) }

Expand All @@ -574,6 +581,9 @@
requester.close
responder.close

pp "CLOSE"
pp "NATS: #{nats_threads.map{|t|[t.name, t.object_id]}}"
pp "Thread: #{Thread.list.map{|t|[t.name, t.object_id]}}"
expect(Thread.list & nats_threads).to be_empty
end
end
Expand Down

0 comments on commit f727700

Please sign in to comment.