2016-11-22 10:05:49 -05:00
|
|
|
require "test_helper"
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
require "puma/cli"
|
|
|
|
require "puma/control_cli"
|
2011-12-07 17:48:41 -05:00
|
|
|
|
2012-11-29 15:05:29 -05:00
|
|
|
# These don't run on travis because they're too fragile
|
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
class TestIntegration < Minitest::Test
|
2011-12-07 16:43:16 -05:00
|
|
|
def setup
|
|
|
|
@state_path = "test/test_puma.state"
|
|
|
|
@bind_path = "test/test_server.sock"
|
|
|
|
@control_path = "test/test_control.sock"
|
2016-02-06 22:00:29 -05:00
|
|
|
@token = "xxyyzz"
|
2012-09-02 23:33:09 -04:00
|
|
|
@tcp_port = 9998
|
|
|
|
|
|
|
|
@server = nil
|
2012-09-06 01:09:42 -04:00
|
|
|
@script = nil
|
2013-07-05 19:54:15 -04:00
|
|
|
|
|
|
|
@wait, @ready = IO.pipe
|
|
|
|
|
|
|
|
@events = Puma::Events.strings
|
|
|
|
@events.on_booted { @ready << "!" }
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
File.unlink @state_path rescue nil
|
|
|
|
File.unlink @bind_path rescue nil
|
|
|
|
File.unlink @control_path rescue nil
|
2012-09-02 23:33:09 -04:00
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
@wait.close
|
|
|
|
@ready.close
|
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
if @server
|
|
|
|
Process.kill "INT", @server.pid
|
2013-06-19 19:16:15 -04:00
|
|
|
begin
|
|
|
|
Process.wait @server.pid
|
|
|
|
rescue Errno::ECHILD
|
|
|
|
end
|
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
@server.close
|
|
|
|
end
|
2012-09-06 01:09:42 -04:00
|
|
|
|
|
|
|
if @script
|
|
|
|
@script.close!
|
|
|
|
end
|
2012-09-02 23:33:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def server(opts)
|
|
|
|
core = "#{Gem.ruby} -rubygems -Ilib bin/puma"
|
|
|
|
cmd = "#{core} --restart-cmd '#{core}' -b tcp://127.0.0.1:#{@tcp_port} #{opts}"
|
2012-09-06 01:09:42 -04:00
|
|
|
tf = Tempfile.new "puma-test"
|
|
|
|
tf.puts "exec #{cmd}"
|
|
|
|
tf.close
|
|
|
|
|
|
|
|
@script = tf
|
|
|
|
|
|
|
|
@server = IO.popen("sh #{tf.path}", "r")
|
|
|
|
|
2016-04-07 13:46:16 -04:00
|
|
|
while (@server.gets) !~ /Ctrl-C/
|
2016-03-06 00:44:53 -05:00
|
|
|
# nothing
|
|
|
|
end
|
2012-09-02 23:33:09 -04:00
|
|
|
|
2012-09-03 12:18:46 -04:00
|
|
|
sleep 1
|
2012-09-06 01:09:42 -04:00
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
@server
|
|
|
|
end
|
|
|
|
|
|
|
|
def signal(which)
|
|
|
|
Process.kill which, @server.pid
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
def wait_booted
|
|
|
|
@wait.sysread 1
|
|
|
|
end
|
|
|
|
|
2011-12-07 16:43:16 -05:00
|
|
|
def test_stop_via_pumactl
|
2016-03-06 00:44:53 -05:00
|
|
|
if Puma.jruby? || Puma.windows?
|
2011-12-07 17:58:30 -05:00
|
|
|
assert true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.quiet
|
|
|
|
c.state_path @state_path
|
|
|
|
c.bind "unix://#{@bind_path}"
|
|
|
|
c.activate_control_app "unix://#{@control_path}", :auth_token => @token
|
|
|
|
c.rackup "test/hello.ru"
|
|
|
|
end
|
|
|
|
|
|
|
|
l = Puma::Launcher.new conf, :events => @events
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
t = Thread.new do
|
2016-02-06 22:00:29 -05:00
|
|
|
Thread.current.abort_on_exception = true
|
|
|
|
l.run
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
wait_booted
|
2011-12-07 16:53:36 -05:00
|
|
|
|
|
|
|
s = UNIXSocket.new @bind_path
|
|
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
sout = StringIO.new
|
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
ccli = Puma::ControlCLI.new %W!-S #{@state_path} stop!, sout
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
ccli.run
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
assert_kind_of Thread, t.join(1), "server didn't stop"
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
2012-09-02 23:33:09 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def test_phased_restart_via_pumactl
|
2016-11-22 10:05:49 -05:00
|
|
|
skip("Too finicky, fails 50% of the time on CI.")
|
|
|
|
|
2016-03-06 00:44:53 -05:00
|
|
|
if Puma.jruby? || Puma.windows?
|
2015-01-15 07:58:16 -05:00
|
|
|
assert true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.quiet
|
|
|
|
c.state_path @state_path
|
|
|
|
c.bind "unix://#{@bind_path}"
|
|
|
|
c.activate_control_app "unix://#{@control_path}", :auth_token => @token
|
|
|
|
c.workers 2
|
|
|
|
c.worker_shutdown_timeout 1
|
|
|
|
c.rackup "test/hello-stuck.ru"
|
|
|
|
end
|
|
|
|
|
|
|
|
l = Puma::Launcher.new conf, :events => @events
|
|
|
|
|
|
|
|
Thread.abort_on_exception = true
|
2015-01-15 07:58:16 -05:00
|
|
|
|
|
|
|
t = Thread.new do
|
2016-02-06 22:00:29 -05:00
|
|
|
Thread.current.abort_on_exception = true
|
|
|
|
l.run
|
2015-01-15 07:58:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
wait_booted
|
|
|
|
|
|
|
|
# Make both workers stuck
|
|
|
|
s1 = UNIXSocket.new @bind_path
|
|
|
|
s1 << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
sout = StringIO.new
|
|
|
|
|
|
|
|
# Phased restart
|
|
|
|
ccli = Puma::ControlCLI.new %W!-S #{@state_path} phased-restart!, sout
|
|
|
|
ccli.run
|
|
|
|
sleep 20
|
|
|
|
@events.stdout.rewind
|
|
|
|
log = @events.stdout.readlines.join("")
|
|
|
|
assert_match(/TERM sent/, log)
|
2016-02-19 20:05:30 -05:00
|
|
|
assert_match(/Worker \d \(pid: \d+\) booted, phase: 1/, log)
|
2015-01-15 07:58:16 -05:00
|
|
|
|
|
|
|
# Stop
|
|
|
|
ccli = Puma::ControlCLI.new %W!-S #{@state_path} stop!, sout
|
|
|
|
ccli.run
|
|
|
|
|
|
|
|
assert_kind_of Thread, t.join(5), "server didn't stop"
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def test_restart_closes_keepalive_sockets
|
2012-09-02 23:33:09 -04:00
|
|
|
server("-q test/hello.ru")
|
|
|
|
|
|
|
|
s = TCPSocket.new "localhost", @tcp_port
|
|
|
|
s << "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
true until s.gets == "\r\n"
|
|
|
|
|
|
|
|
s.readpartial(20)
|
|
|
|
signal :USR2
|
|
|
|
|
2012-09-06 01:09:42 -04:00
|
|
|
sleep 1
|
2012-09-03 12:23:42 -04:00
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
s.write "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
|
2016-04-07 13:46:16 -04:00
|
|
|
assert_raises Errno::ECONNRESET do
|
2012-09-02 23:33:09 -04:00
|
|
|
Timeout.timeout(2) do
|
2012-10-13 22:25:17 -04:00
|
|
|
raise Errno::ECONNRESET unless s.read(2)
|
2012-09-02 23:33:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-07 13:46:16 -04:00
|
|
|
while (@server.gets) !~ /Ctrl-C/
|
2016-03-06 00:44:53 -05:00
|
|
|
# nothing
|
|
|
|
end
|
|
|
|
|
|
|
|
sleep 5 if Puma.jruby?
|
|
|
|
|
|
|
|
s = TCPSocket.new "127.0.0.1", @tcp_port
|
2012-09-02 23:33:09 -04:00
|
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
|
|
end
|
2012-09-06 01:09:42 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def test_restart_closes_keepalive_sockets_workers
|
2016-03-06 00:44:53 -05:00
|
|
|
if Puma.jruby?
|
|
|
|
assert true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-09-10 11:35:50 -04:00
|
|
|
server("-q -w 2 test/hello.ru")
|
|
|
|
|
|
|
|
s = TCPSocket.new "localhost", @tcp_port
|
|
|
|
s << "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
true until s.gets == "\r\n"
|
|
|
|
|
|
|
|
s.readpartial(20)
|
|
|
|
signal :USR2
|
|
|
|
|
|
|
|
true while @server.gets =~ /Ctrl-C/
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
s.write "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
|
|
|
|
assert_raises Errno::ECONNRESET do
|
|
|
|
Timeout.timeout(2) do
|
2012-10-13 22:25:17 -04:00
|
|
|
raise Errno::ECONNRESET unless s.read(2)
|
2012-09-10 11:35:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
s = TCPSocket.new "localhost", @tcp_port
|
|
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
|
|
end
|
2016-03-06 00:44:53 -05:00
|
|
|
end
|