mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
b638dd1948
* Bump minitest version. * Add basic test helper file. * Use minitest for web server tests. * Use Minitest for unix socket tests. * Use Minitest for ThreadPool tests. * Use Minitest for TCP-Rack tests * Use Minitest for TCPLogger tests. * Add missing helper to test helpers. * Use Minitest for Rack server tests. * Use Minitest for Rack handler tests. * Use Minitest for Puma::Server tests. * Use Minitest for Puma::Server with SSL tests. * Use Minitest for persisten connections tests. * Require puma in test_helper file. * Use minitest for Puma::NullIO tests. * Remove unnecessary requires on test files. * Use Minitest for MiniSSL tests. * Use Minitest for IOBuffer tests. * Require bundler/setup in Rakefile. * Use Minitest for HttpParser tests. * Use Minitest for Puma::Configuration tests. * Use Minitest for Puma::CLI tests. * Bump Minitest version for Ruby 2.1 Gemfile. * Use Minitest for integration tests. * Use Minitest for Puma::App::Status tests. * Remove test-unit from Gemfiles. * Add timeout helper to Minitest::Test. * Use Minitest for Puma::Binder tests. * Remove testhelp file. * Add missing require to Puma::Binder tests. * Prefer require instead of require_relative.
224 lines
4.6 KiB
Ruby
224 lines
4.6 KiB
Ruby
require "test_helper"
|
|
|
|
require "puma/cli"
|
|
require "puma/control_cli"
|
|
|
|
# These don't run on travis because they're too fragile
|
|
|
|
class TestIntegration < Minitest::Test
|
|
def setup
|
|
@state_path = "test/test_puma.state"
|
|
@bind_path = "test/test_server.sock"
|
|
@control_path = "test/test_control.sock"
|
|
@token = "xxyyzz"
|
|
@tcp_port = 9998
|
|
|
|
@server = nil
|
|
@script = nil
|
|
|
|
@wait, @ready = IO.pipe
|
|
|
|
@events = Puma::Events.strings
|
|
@events.on_booted { @ready << "!" }
|
|
end
|
|
|
|
def teardown
|
|
File.unlink @state_path rescue nil
|
|
File.unlink @bind_path rescue nil
|
|
File.unlink @control_path rescue nil
|
|
|
|
@wait.close
|
|
@ready.close
|
|
|
|
if @server
|
|
Process.kill "INT", @server.pid
|
|
begin
|
|
Process.wait @server.pid
|
|
rescue Errno::ECHILD
|
|
end
|
|
|
|
@server.close
|
|
end
|
|
|
|
if @script
|
|
@script.close!
|
|
end
|
|
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}"
|
|
tf = Tempfile.new "puma-test"
|
|
tf.puts "exec #{cmd}"
|
|
tf.close
|
|
|
|
@script = tf
|
|
|
|
@server = IO.popen("sh #{tf.path}", "r")
|
|
|
|
while (@server.gets) !~ /Ctrl-C/
|
|
# nothing
|
|
end
|
|
|
|
sleep 1
|
|
|
|
@server
|
|
end
|
|
|
|
def signal(which)
|
|
Process.kill which, @server.pid
|
|
end
|
|
|
|
def wait_booted
|
|
@wait.sysread 1
|
|
end
|
|
|
|
def test_stop_via_pumactl
|
|
if Puma.jruby? || Puma.windows?
|
|
assert true
|
|
return
|
|
end
|
|
|
|
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
|
|
|
|
t = Thread.new do
|
|
Thread.current.abort_on_exception = true
|
|
l.run
|
|
end
|
|
|
|
wait_booted
|
|
|
|
s = UNIXSocket.new @bind_path
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
|
|
sout = StringIO.new
|
|
|
|
ccli = Puma::ControlCLI.new %W!-S #{@state_path} stop!, sout
|
|
|
|
ccli.run
|
|
|
|
assert_kind_of Thread, t.join(1), "server didn't stop"
|
|
end
|
|
|
|
def test_phased_restart_via_pumactl
|
|
skip("Too finicky, fails 50% of the time on CI.")
|
|
|
|
if Puma.jruby? || Puma.windows?
|
|
assert true
|
|
return
|
|
end
|
|
|
|
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
|
|
|
|
t = Thread.new do
|
|
Thread.current.abort_on_exception = true
|
|
l.run
|
|
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)
|
|
assert_match(/Worker \d \(pid: \d+\) booted, phase: 1/, log)
|
|
|
|
# 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
|
|
|
|
def test_restart_closes_keepalive_sockets
|
|
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
|
|
|
|
sleep 1
|
|
|
|
s.write "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
assert_raises Errno::ECONNRESET do
|
|
Timeout.timeout(2) do
|
|
raise Errno::ECONNRESET unless s.read(2)
|
|
end
|
|
end
|
|
|
|
while (@server.gets) !~ /Ctrl-C/
|
|
# nothing
|
|
end
|
|
|
|
sleep 5 if Puma.jruby?
|
|
|
|
s = TCPSocket.new "127.0.0.1", @tcp_port
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
end
|
|
|
|
def test_restart_closes_keepalive_sockets_workers
|
|
if Puma.jruby?
|
|
assert true
|
|
return
|
|
end
|
|
|
|
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
|
|
raise Errno::ECONNRESET unless s.read(2)
|
|
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
|
|
end
|