Skip to content

Commit

Permalink
Add websocket cluster reconnect spec
Browse files Browse the repository at this point in the history
Signed-off-by: Waldemar Quevedo <[email protected]>
  • Loading branch information
wallyqs committed Jun 17, 2023
1 parent fd62864 commit d4210d5
Showing 1 changed file with 100 additions and 5 deletions.
105 changes: 100 additions & 5 deletions spec/client_cluster_tls_reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@
'pid_file' => '/tmp/nats_cluster_s1.pid',
'host' => '127.0.0.1',
'port' => 4232,
'cluster_port' => 6232
'cluster_port' => 6232,
'wsport' => 8232
}

s2_config_opts = {
'pid_file' => '/tmp/nats_cluster_s2.pid',
'host' => '127.0.0.1',
'port' => 4233,
'cluster_port' => 6233
'cluster_port' => 6233,
'wsport' => 8233
}

s3_config_opts = {
'pid_file' => '/tmp/nats_cluster_s3.pid',
'host' => '127.0.0.1',
'port' => 4234,
'cluster_port' => 6234
'cluster_port' => 6234,
'wsport' => 8234
}

nodes = []
Expand All @@ -46,6 +49,18 @@
port: #{config_opts['port']}
pid_file: '#{config_opts['pid_file']}'
websocket {
port: #{config_opts['wsport']}
tls {
cert_file: "./spec/configs/certs/nats-service.localhost/server.pem"
key_file: "./spec/configs/certs/nats-service.localhost/server-key.pem"
ca_file: "./spec/configs/certs/nats-service.localhost/ca.pem"
timeout: 10
}
# NOTE: Force to reconnect using any other hostname other than the initial one.
advertise: 'server-B.clients.nats-service.localhost:#{config_opts['wsport']}'
}
tls {
cert_file: "./spec/configs/certs/nats-service.localhost/server.pem"
key_file: "./spec/configs/certs/nats-service.localhost/server-key.pem"
Expand Down Expand Up @@ -85,8 +100,6 @@
end

it 'should reconnect to nodes discovered from seed server' do
skip 'flapping test'

# Nodes join to cluster before we try to connect
[@s2, @s3].each do |s|
s.start_server(true)
Expand Down Expand Up @@ -163,5 +176,87 @@
end
end
end

it 'should reconnect to nodes discovered from seed server with WebSockets' do
# Nodes join to cluster before we try to connect
[@s2, @s3].each do |s|
s.start_server(true)
end

begin
mon = Monitor.new
reconnected = mon.new_cond

nats = NATS::IO::Client.new
disconnects = 0
nats.on_disconnect do
disconnects += 1
end

closes = 0
nats.on_close do
closes += 1
end

reconnects = 0
nats.on_reconnect do
reconnects += 1
mon.synchronize do
reconnected.signal
end
end

errors = []
nats.on_error do |e|
puts e
errors << e
end

# Connect to first server only and trigger reconnect
ctx = OpenSSL::SSL::SSLContext.new
ctx.set_params
ctx.ca_file = "./spec/configs/certs/nats-service.localhost/ca.pem"
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
ctx.verify_hostname = true

nats.connect("wss://server-A.clients.nats-service.localhost:8232", {
dont_randomize_servers: true, reconnect: true, tls: {
context: ctx
}})
expect(nats.connected_server.to_s).to eql("wss://server-A.clients.nats-service.localhost:8232")

nats.subscribe("hello") { |msg, reply| nats.publish(reply, '') }
nats.flush
nats.request("hello", 'world')

expect(nats.connected?).to eql(true)
@s1.kill_server
sleep 0.1
mon.synchronize do
reconnected.wait(3)
end

# Confirm that it has reconnected.
expect(nats.connected?).to eql(true)

# Reconnected...
expect(nats.instance_variable_get("@hostname")).to eql("server-A.clients.nats-service.localhost")
expect(nats.connected_server.to_s).to_not eql("")
expect(["wss://127.0.0.1:8233", "wss://127.0.0.1:8234"].include?(nats.connected_server.to_s)).to eql(true)
nats.request("hello", 'world', timeout: 1)

expect(reconnects).to eql(1)
expect(disconnects).to eql(1)

expect(errors.count >= 1).to eql(true)

nats.close
ensure
# Wrap up test
[@s2, @s3].each do |s|
s.kill_server
end
end
end
end
end

0 comments on commit d4210d5

Please sign in to comment.