Skip to content

Commit

Permalink
Refactor binds + test
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed Sep 25, 2019
1 parent 3823644 commit eabef54
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
37 changes: 24 additions & 13 deletions lib/puma/binder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ def parse(binds, logger)
bak = params.fetch('backlog', 1024).to_i

io = add_tcp_listener uri.host, uri.port, opt, bak

@ios.each do |i|
next unless TCPServer === i
addr = if i.local_address.ipv6?
"[#{i.local_address.ip_unpack[0]}]:#{i.local_address.ip_unpack[1]}"
else
i.local_address.ip_unpack.join(':')
end

logger.log "* Listening on tcp://#{addr}"
end
end

@listeners << [str, io] if io
Expand Down Expand Up @@ -148,7 +137,6 @@ def parse(binds, logger)
end

io = add_unix_listener path, umask, mode, backlog
logger.log "* Listening on #{str}"
end

@listeners << [str, io]
Expand Down Expand Up @@ -221,7 +209,6 @@ def parse(binds, logger)
logger.log "* Activated #{str}"
else
io = add_ssl_listener uri.host, uri.port, ctx
logger.log "* Listening on #{str}"
end

@listeners << [str, io] if io
Expand All @@ -230,6 +217,18 @@ def parse(binds, logger)
end
end

@ios.each do |i|
protocol, addrinfo = case i
when TCPServer
["tcp", i.local_address]
when UNIXServer
["unix", i.local_address]
when Puma::MiniSSL::Server
["ssl", i.to_io.local_address]
end
logger.log "* Listening on #{protocol}://#{addrinfo_to_uri(addrinfo)}"
end

# If we inherited fds but didn't use them (because of a
# configuration change), then be sure to close them.
@inherited_fds.each do |str, fd|
Expand Down Expand Up @@ -436,5 +435,17 @@ def redirects_for_restart
end
redirects
end

private

def addrinfo_to_uri(addrinfo)
if addrinfo.ipv6?
"[#{addrinfo.ip_address}]:#{addrinfo.ip_port}"
elsif addrinfo.ipv4?
addrinfo.ip_unpack.join(':')
elsif addrinfo.unix?
addrinfo.unix_path
end
end
end
end
34 changes: 14 additions & 20 deletions test/test_binder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,34 @@ def test_logs_all_localhost_bindings
end

def test_allows_both_unix_and_tcp
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST

path_unix = "test/#{name}_server.sock"
uri_unix = "unix://#{path_unix}"
uri_tcp = "tcp://127.0.0.1:#{UniquePort.call}"

@binder.parse([uri_unix, uri_tcp], @events)

stdout = @events.stdout.string

assert stdout.include?(uri_unix), "\n#{stdout}\n"
assert stdout.include?(uri_tcp) , "\n#{stdout}\n"

ensure
if UNIX_SKT_EXIST
@binder.close
File.unlink(path_unix) if File.exist? path_unix
end
assert_parsing_logs_uri [:unix, :tcp]
end

def test_allows_both_tcp_and_unix
assert_parsing_logs_uri [:tcp, :unix]
end

private

def assert_parsing_logs_uri(order = [:unix, :tcp])
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST

path_unix = "test/#{name}_server.sock"
uri_unix = "unix://#{path_unix}"
uri_tcp = "tcp://127.0.0.1:#{UniquePort.call}"

@binder.parse([uri_tcp, uri_unix], @events)
if order == [:unix, :tcp]
@binder.parse([uri_tcp, uri_unix], @events)
elsif order == [:tcp, :unix]
@binder.parse([uri_unix, uri_tcp], @events)
else
raise ArgumentError
end

stdout = @events.stdout.string

assert stdout.include?(uri_unix), "\n#{stdout}\n"
assert stdout.include?(uri_tcp) , "\n#{stdout}\n"

ensure
if UNIX_SKT_EXIST
@binder.close
Expand Down

0 comments on commit eabef54

Please sign in to comment.