Skip to content

Commit

Permalink
- spec: add connect_timeout to server check
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Jan 8, 2025
1 parent 10b2f1c commit f6f66de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 10 additions & 1 deletion spec/support/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@

Resolv::DefaultResolver.replace_resolvers([hosts_resolver, dns_resolver])

# resolve-replace doesn't support modern Ruby TCPSocket signature:
# https://github.com/ruby/resolv-replace/issues/2
class TCPSocket
def initialize(host, serv, *rest, **kwargs)
rest[0] = IPSocket.getaddress(rest[0]) if rest[0]
original_resolv_initialize(IPSocket.getaddress(host), serv, *rest, **kwargs)
end
end

# Patch Socket to rely on Resolve when getting address info
# Inspired by the original resolve-replace.rb: https://github.com/ruby/resolv-replace/blob/master/lib/resolv-replace.rb
require "socket"
Expand All @@ -32,7 +41,7 @@ def getaddrinfo(host, *args)
begin
return original_getaddrinfo(Resolv.getaddress(host).to_s, *args)
rescue Resolv::ResolvError
raise SocketError, "Hostname not known: #{host}"
original_getaddrinfo(host, *args)
end
end
end
16 changes: 10 additions & 6 deletions spec/support/nats_server_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "socket"

class NatsServerControl
BIN_PATH = File.expand_path(File.join(__dir__, "../../scripts/nats-server"))

Expand Down Expand Up @@ -92,17 +94,19 @@ def kill_server
def wait_for_server(uri, max_wait = 5) # :nodoc:
start = Time.now
while (Time.now - start < max_wait) # Wait max_wait seconds max
break if server_running?(uri)
return if server_running?(uri)
sleep(0.1)
end

raise "NATS Server did not start in #{max_wait} seconds"
end

def server_running?(uri) # :nodoc:
require 'socket'
s = TCPSocket.new(uri.host, uri.port)
s.close
return true
s = TCPSocket.new(uri.host, uri.port, nil, nil, connect_timeout: 0.5)
true
rescue
return false
false
ensure
s&.close
end
end

0 comments on commit f6f66de

Please sign in to comment.