Skip to content

Commit

Permalink
Merge pull request #39 from nats-io/info-parse-fix
Browse files Browse the repository at this point in the history
Fix INFO parsing while connect init attempt
  • Loading branch information
wallyqs authored Jul 22, 2019
2 parents 7f78ee2 + faa411c commit 8cb619e
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
nats-pure (0.6.0)
nats-pure (0.6.2)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
- [ ] increase test coverage
- [ ] max payload err handling
- [ ] read dynamic socket buffer
- [ ] nuid
- [X] nuid
9 changes: 7 additions & 2 deletions lib/nats/io/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,13 @@ def process_connect_init
if !line or line.empty?
raise ConnectError.new("nats: protocol exception, INFO not received")
end
_, info_json = line.split(' ')
process_info(info_json)

if match = line.match(NATS::Protocol::INFO)
info_json = match.captures.first
process_info(info_json)
else
raise ConnectError.new("nats: protocol exception, INFO not valid")
end

case
when (server_using_secure_connection? and client_using_secure_connection?)
Expand Down
2 changes: 1 addition & 1 deletion lib/nats/io/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
module NATS
module IO
# NOTE: These are all announced to the server on CONNECT
VERSION = "0.6.0"
VERSION = "0.6.2"
LANG = "#{RUBY_ENGINE}2".freeze
PROTOCOL = 1
end
Expand Down
110 changes: 110 additions & 0 deletions spec/client_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,114 @@ class CustomError < StandardError; end
expect(nats.status).to eql(NATS::IO::DISCONNECTED)
end
end

context 'against a server with a custom INFO line' do
before(:all) do
# Start a fake tcp server
@fake_nats_server = TCPServer.new 4556
@fake_nats_server_th = Thread.new do

loop do
# Wait for a client to connect and linger
client = @fake_nats_server.accept
client.puts %Q(INFO {"version":"1.3.0 foo bar","max_payload": 1048576}\r\n)
client.puts "PONG\r\n"
end
end
end

after(:all) do
@fake_nats_server_th.exit
@fake_nats_server.close
end

it 'should be able to connect' do
msgs = []
errors = []
closes = 0
reconnects = 0
disconnects = []

nc = NATS::IO::Client.new
mon = Monitor.new
done = mon.new_cond

nc.on_error do |e|
errors << e
end

nc.on_close do
mon.synchronize { done.signal }
end

expect do
nc.connect({
:servers => ["nats://127.0.0.1:4556"],
:reconnect => false,
:connect_timeout => 1
})
end.to_not raise_error

nc.close
mon.synchronize { done.wait(3) }
puts errors
end
end

context 'against a server with a custom malformed INFO line' do
before(:all) do
# Start a fake tcp server
@fake_nats_server = TCPServer.new 4556
@fake_nats_server_th = Thread.new do

loop do
# Wait for a client to connect and linger
client = @fake_nats_server.accept
begin
client.puts %Q(INFO {foo)
ensure
client.close
end
end
end
end

after(:all) do
@fake_nats_server_th.exit
@fake_nats_server.close
end

it 'should fail to connect' do
msgs = []
errors = []
closes = 0
reconnects = 0
disconnects = []

nc = NATS::IO::Client.new
mon = Monitor.new
done = mon.new_cond

nc.on_error do |e|
errors << e
end

nc.on_close do
mon.synchronize { done.signal }
end

expect do
nc.connect({
:servers => ["nats://127.0.0.1:4556"],
:reconnect => false,
:connect_timeout => 1
})
end.to raise_error (NATS::IO::ConnectError)

nc.close
mon.synchronize { done.wait(3) }
expect(errors.count).to eql(1)
expect(errors.first).to be_a(NATS::IO::ConnectError)
end
end
end
12 changes: 6 additions & 6 deletions spec/client_reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@
# Wait for a client to connect
client = @fake_nats_server.accept
begin
client.puts "INFO {}"
client.puts "INFO {}\r\n"

# Read and ignore CONNECT command send by the client
connect_op = client.gets.chomp
connect_op = client.gets

# Reply to any pending pings client may have sent
sleep 0.1
Expand Down Expand Up @@ -455,10 +455,10 @@
# Wait for a client to connect
client = @fake_nats_server.accept
begin
client.puts "INFO {}"
client.puts "INFO {}\r\n"

# Read and ignore CONNECT command send by the client
connect_op = client.gets.chomp
connect_op = client.gets

# Reply to any pending pings client may have sent
sleep 0.1
Expand Down Expand Up @@ -542,10 +542,10 @@
# Wait for a client to connect
client = @fake_nats_server.accept
begin
client.puts "INFO {}"
client.puts "INFO {}\r\n"

# Read and ignore CONNECT command send by the client
connect_op = client.gets.chomp
connect_op = client.gets

# Reply to any pending pings client may have sent
sleep 0.5
Expand Down

0 comments on commit 8cb619e

Please sign in to comment.