Skip to content

Commit

Permalink
Synchronize Client#add_service
Browse files Browse the repository at this point in the history
  • Loading branch information
vankiru committed Jan 27, 2025
1 parent 5a7b2c5 commit dcbf7fd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
17 changes: 9 additions & 8 deletions lib/nats/io/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Client
include MonitorMixin
include Status

attr_reader :status, :server_info, :server_pool, :options, :stats, :uri, :subscription_executor, :reloader, :subs
attr_reader :status, :server_info, :server_pool, :options, :stats, :uri, :subscription_executor, :reloader, :services

DEFAULT_PORT = {nats: 4222, ws: 80, wss: 443}.freeze
DEFAULT_URI = "nats://localhost:#{DEFAULT_PORT[:nats]}".freeze
Expand Down Expand Up @@ -242,6 +242,9 @@ def initialize(uri = nil, opts = {})
# Draining
@drain_t = nil

# Service API
@services = []

# Prepare for calling connect or automatic delayed connection
parse_and_validate_options if uri || opts.any?

Expand Down Expand Up @@ -738,15 +741,13 @@ def flush(timeout = 10)
end
end

def services
@services ||= []
end

def add_service(options)
service = NATS::Service.new(self, options)
synchronize do
service = NATS::Service.new(self, options)
services << service

services << service
service
service
end
end

alias_method :servers, :server_pool
Expand Down
4 changes: 3 additions & 1 deletion spec/service/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
end

let(:client) { NATS.connect }
let(:subs) { client.instance_variable_get("@subs") }

let(:service) { client.add_service(name: "foo", queue: "queue") }

subject do
Expand Down Expand Up @@ -192,7 +194,7 @@
it "drains handler subscription" do
subject.stop

expect(client.subs.values).to include(
expect(subs.values).to include(
having_attributes(subject: "foo.bar", drained: true)
)
end
Expand Down
7 changes: 4 additions & 3 deletions spec/service/monitoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
let(:stats) { {**basic, started: "2025-01-24T05:40:37Z"} }

let(:client) { NATS.connect }
let(:subs) { client.instance_variable_get("@subs") }

before(:all) do
@server = NatsServerControl.new
Expand All @@ -37,7 +38,7 @@
it "starts monitoring with the specified prefix" do
subject

expect(client.subs.values).to include(
expect(subs.values).to include(
having_attributes(subject: "$FOO.PING"),
having_attributes(subject: "$FOO.PING.foo"),
having_attributes(subject: "$FOO.PING.foo.bar"),
Expand All @@ -57,7 +58,7 @@
it "starts monitoring with the default prefix" do
subject

expect(client.subs.values).to include(
expect(subs.values).to include(
having_attributes(subject: "$SRV.PING"),
having_attributes(subject: "$SRV.PING.foo"),
having_attributes(subject: "$SRV.PING.foo.bar"),
Expand Down Expand Up @@ -119,7 +120,7 @@
it "drains monitoring subscriptions" do
subject.stop

expect(client.subs.values).to include(
expect(subs.values).to include(
having_attributes(subject: "$SRV.PING", drained: true),
having_attributes(subject: "$SRV.PING.foo", drained: true),
having_attributes(subject: "$SRV.PING.foo.bar", drained: true),
Expand Down

0 comments on commit dcbf7fd

Please sign in to comment.