From e10e520d47fa96cf549c7d544d6575baa8ed748a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 16 Feb 2011 01:31:41 +0000 Subject: middleware: add ipv6 address detection for Unicorn Since Unicorn and Rainbows! support IPv6 now, it makes sense to support the rfc2732-style addresses it returns. --- lib/raindrops/middleware.rb | 2 +- test/test_middleware_unicorn.rb | 46 ++++++++++++++++++++++++++++++ test/test_middleware_unicorn_ipv6.rb | 55 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 test/test_middleware_unicorn.rb create mode 100644 test/test_middleware_unicorn_ipv6.rb diff --git a/lib/raindrops/middleware.rb b/lib/raindrops/middleware.rb index 3507928..79496fc 100644 --- a/lib/raindrops/middleware.rb +++ b/lib/raindrops/middleware.rb @@ -22,7 +22,7 @@ class Raindrops::Middleware @tcp = @unix = nil if tmp - @tcp = tmp.grep(/\A[^:]+:\d+\z/) + @tcp = tmp.grep(/\A.+:\d+\z/) @unix = tmp.grep(%r{\A/}) @tcp = nil if @tcp.empty? @unix = nil if @unix.empty? diff --git a/test/test_middleware_unicorn.rb b/test/test_middleware_unicorn.rb new file mode 100644 index 0000000..cb73d3d --- /dev/null +++ b/test/test_middleware_unicorn.rb @@ -0,0 +1,46 @@ +# -*- encoding: binary -*- +require "test/unit" +require "raindrops" +require "rack" +require "rack/lobster" +require "open-uri" +begin + require "unicorn" +rescue => e + warn "W: #{e} skipping test since Unicorn was not found" +end +$stderr.sync = $stdout.sync = true + +class TestMiddlewareUnicorn < Test::Unit::TestCase + + def setup + @host = ENV["UNICORN_TEST_ADDR"] || "127.0.0.1" + sock = TCPServer.new @host, 0 + @port = sock.addr[1] + ENV["UNICORN_FD"] = sock.fileno.to_s + @host_with_port = "#@host:#@port" + @opts = { :listeners => [ @host_with_port ] } + @addr_regexp = Regexp.escape @host_with_port + end + + def test_auto_listener + @app = Rack::Builder.new do + use Raindrops::Middleware + run Rack::Lobster.new + end + @srv = fork { Unicorn.run(@app, @opts) } + + s = TCPSocket.new @host, @port + s.write "GET /_raindrops HTTP/1.0\r\n\r\n" + resp = s.read + head, body = resp.split /\r\n\r\n/, 2 + assert_match %r{^#@addr_regexp active: 1$}, body + assert_match %r{^#@addr_regexp queued: 0$}, body + end + + def teardown + Process.kill :QUIT, @srv + _, status = Process.waitpid2 @srv + assert status.success? + end +end if defined?(Unicorn) && RUBY_PLATFORM =~ /linux/ diff --git a/test/test_middleware_unicorn_ipv6.rb b/test/test_middleware_unicorn_ipv6.rb new file mode 100644 index 0000000..6f00cb1 --- /dev/null +++ b/test/test_middleware_unicorn_ipv6.rb @@ -0,0 +1,55 @@ +# -*- encoding: binary -*- +require "test/unit" +require "raindrops" +require "rack" +require "rack/lobster" +require "open-uri" +begin + require "unicorn" +rescue => e + warn "W: #{e} skipping test since Unicorn was not found" +end + +begin + tmp = TCPServer.new(ENV["TEST_HOST6"] || '::1', 0) + ipv6_enabled = true +rescue => e + warn "skipping IPv6 tests, host does not seem to be IPv6 enabled:" + warn " #{e.class}: #{e}" + ipv6_enabled = false +end +$stderr.sync = $stdout.sync = true + +class TestMiddlewareUnicornIPv6 < Test::Unit::TestCase + + def setup + @host = ENV["TEST_HOST6"] || "::1" + sock = TCPServer.new @host, 0 + @port = sock.addr[1] + ENV["UNICORN_FD"] = sock.fileno.to_s + @host_with_port = "[#@host]:#@port" + @opts = { :listeners => [ @host_with_port ] } + @addr_regexp = Regexp.escape @host_with_port + end + + def test_auto_listener + @app = Rack::Builder.new do + use Raindrops::Middleware + run Rack::Lobster.new + end + @srv = fork { Unicorn.run(@app, @opts) } + s = TCPSocket.new @host, @port + s.write "GET /_raindrops HTTP/1.0\r\n\r\n" + resp = s.read + head, body = resp.split /\r\n\r\n/, 2 + assert_match %r{^#@addr_regexp active: 1$}, body + assert_match %r{^#@addr_regexp queued: 0$}, body + end + + def teardown + Process.kill :QUIT, @srv + _, status = Process.waitpid2 @srv + assert status.success? + end +end if defined?(Unicorn) && RUBY_PLATFORM =~ /linux/ && ipv6_enabled + -- cgit v1.2.3-24-ge0c7