Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests - clean up - three commits #1903

Merged
merged 3 commits into from Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 3 additions & 8 deletions test/helper.rb
Expand Up @@ -16,6 +16,7 @@
require "minitest/autorun"
require "minitest/pride"
require "minitest/proveit"
require_relative "helpers/apps"

$LOAD_PATH << File.expand_path("../../lib", __FILE__)
Thread.abort_on_exception = true
Expand Down Expand Up @@ -43,10 +44,10 @@ def hit(uris)

module UniquePort
@port = 3211
@mutex = Mutex.new

def self.call
@port += 1
@port
@mutex.synchronize { @port += 1 }
end
end

Expand All @@ -69,8 +70,6 @@ def run(*)

module TestSkips

@@next_port = 9000

# usage: skip NO_FORK_MSG unless HAS_FORK
# windows >= 2.6 fork is not defined, < 2.6 fork raises NotImplementedError
HAS_FORK = ::Process.respond_to? :fork
Expand Down Expand Up @@ -115,10 +114,6 @@ def skip_unless(eng, bt: caller)
end
skip skip_msg, bt if skip_msg
end

def next_port(incr = 1)
@@next_port += incr
end
end

Minitest::Test.include TestSkips
Expand Down
12 changes: 12 additions & 0 deletions test/helpers/apps.rb
@@ -0,0 +1,12 @@
module TestApps

# call with "GET /sleep<d> HTTP/1.1\r\n\r\n", where is the number of
# seconds to sleep
# same as rackup/sleep.ru
SLEEP = -> (env) do
dly = (env['REQUEST_PATH'][/\/sleep(\d+)/,1] || '0').to_i
sleep dly
[200, {"Content-Type" => "text/plain"}, ["Slept #{dly}"]]
end

end
4 changes: 0 additions & 4 deletions test/rackup/10seconds.ru

This file was deleted.

4 changes: 0 additions & 4 deletions test/rackup/1second.ru

This file was deleted.

3 changes: 0 additions & 3 deletions test/rackup/hello-delay.ru

This file was deleted.

3 changes: 0 additions & 3 deletions test/rackup/hello-map.ru

This file was deleted.

4 changes: 0 additions & 4 deletions test/rackup/hello-post.ru

This file was deleted.

1 change: 0 additions & 1 deletion test/rackup/hello-stuck-ci.ru

This file was deleted.

1 change: 0 additions & 1 deletion test/rackup/hello-stuck.ru

This file was deleted.

5 changes: 0 additions & 5 deletions test/rackup/hello-tcp.ru

This file was deleted.

6 changes: 0 additions & 6 deletions test/rackup/hijack.ru

This file was deleted.

5 changes: 0 additions & 5 deletions test/rackup/hijack2.ru

This file was deleted.

9 changes: 9 additions & 0 deletions test/rackup/sleep.ru
@@ -0,0 +1,9 @@
# call with "GET /sleep<d> HTTP/1.1\r\n\r\n", where <d> is the number of
# seconds to sleep
# same as TestApps::SLEEP

run lambda { |env|
dly = (env['REQUEST_PATH'][/\/sleep(\d+)/,1] || '0').to_i
sleep dly
[200, {"Content-Type" => "text/plain"}, ["Slept #{dly}"]]
}
4 changes: 0 additions & 4 deletions test/rackup/slow.ru

This file was deleted.

10 changes: 5 additions & 5 deletions test/test_cli.rb
Expand Up @@ -41,8 +41,8 @@ def test_pid_file
end

def test_control_for_tcp
tcp = next_port
cntl = next_port
tcp = UniquePort.call
cntl = UniquePort.call
url = "tcp://127.0.0.1:#{cntl}/"

cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:#{tcp}",
Expand Down Expand Up @@ -207,8 +207,8 @@ def control_gc_stats(uri, cntl)

def test_control_gc_stats_tcp
skip_on :jruby, suffix: " - Hitting /gc route does not increment count"
uri = "tcp://127.0.0.1:#{next_port}/"
cntl_port = next_port
uri = "tcp://127.0.0.1:#{UniquePort.call}/"
cntl_port = UniquePort.call
cntl = "tcp://127.0.0.1:#{cntl_port}/"

control_gc_stats(uri, cntl) { TCPSocket.new "127.0.0.1", cntl_port }
Expand Down Expand Up @@ -280,7 +280,7 @@ def test_log_formatter_custom_clustered
end

def test_state
url = "tcp://127.0.0.1:#{next_port}"
url = "tcp://127.0.0.1:#{UniquePort.call}"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
cli.launcher.write_state

Expand Down
34 changes: 17 additions & 17 deletions test/test_integration.rb
Expand Up @@ -12,9 +12,10 @@
class TestIntegration < Minitest::Test

def setup
@state_path = "test/test_puma.state"
@bind_path = "test/test_server.sock"
@control_path = "test/test_control.sock"
unique = UniquePort.call
@state_path = "test/test_#{unique}_puma.state"
@bind_path = "test/test_#{unique}_server.sock"
@control_path = "test/test_#{unique}_control.sock"
@token = "xxyyzz"

@server = nil
Expand Down Expand Up @@ -45,7 +46,7 @@ def teardown
end

def server_cmd(argv)
@tcp_port = next_port
@tcp_port = UniquePort.call
base = "#{Gem.ruby} -Ilib bin/puma"
base = "bundle exec #{base}" if defined?(Bundler)
"#{base} -b tcp://127.0.0.1:#{@tcp_port} #{argv}"
Expand Down Expand Up @@ -100,9 +101,9 @@ def restart_server(server, connection)
wait_for_server_to_boot(server)
end

def connect
def connect(path = nil)
s = TCPSocket.new "localhost", @tcp_port
s << "GET / HTTP/1.1\r\n\r\n"
s << "GET /#{path} HTTP/1.1\r\n\r\n"
true until s.gets == "\r\n"
s
end
Expand Down Expand Up @@ -158,17 +159,16 @@ def test_stop_via_pumactl
def test_phased_restart_via_pumactl
skip NO_FORK_MSG unless HAS_FORK

# hello-stuck-ci uses sleep 10, hello-stuck uses sleep 60
rackup = "test/rackup/hello-stuck#{ ENV['CI'] ? '-ci' : '' }.ru"
delay = 40

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 rackup
c.worker_shutdown_timeout 2
c.rackup "test/rackup/sleep.ru"
end

l = Puma::Launcher.new conf, :events => @events
Expand All @@ -181,7 +181,7 @@ def test_phased_restart_via_pumactl
wait_booted

s = UNIXSocket.new @bind_path
s << "GET / HTTP/1.0\r\n\r\n"
s << "GET /sleep#{delay} HTTP/1.0\r\n\r\n"

sout = StringIO.new
# Phased restart
Expand Down Expand Up @@ -241,15 +241,15 @@ def test_restart_closes_keepalive_sockets_workers

def test_sigterm_closes_listeners_on_forked_servers
skip NO_FORK_MSG unless HAS_FORK
pid = start_forked_server("-w 2 -q test/rackup/1second.ru")
pid = start_forked_server("-w 2 -q test/rackup/sleep.ru")
threads = []
initial_reply = nil
next_replies = []
condition_variable = ConditionVariable.new
mutex = Mutex.new

threads << Thread.new do
s = connect
s = connect "sleep1"
mutex.synchronize { condition_variable.broadcast }
initial_reply = read_body(s)
end
Expand Down Expand Up @@ -284,7 +284,7 @@ def test_sigterm_closes_listeners_on_forked_servers

threads.map(&:join)

assert_equal "Hello World", initial_reply
assert_equal "Slept 1", initial_reply

assert_includes next_replies, :connection_refused

Expand Down Expand Up @@ -351,9 +351,9 @@ def test_term_signal_suppress_in_clustered_mode
def test_not_accepts_new_connections_after_term_signal
skip_on :jruby, :windows

server('test/rackup/10seconds.ru')
server('test/rackup/sleep.ru')

_stdin, curl_stdout, _stderr, curl_wait_thread = Open3.popen3("curl 127.0.0.1:#{@tcp_port}")
_stdin, curl_stdout, _stderr, curl_wait_thread = Open3.popen3("curl http://127.0.0.1:#{@tcp_port}/sleep10")
sleep 1 # ensure curl send a request

Process.kill(:TERM, @server.pid)
Expand All @@ -368,7 +368,7 @@ def test_not_accepts_new_connections_after_term_signal
curl_wait_thread.join
rejected_curl_wait_thread.join

assert_match(/Hello World/, curl_stdout.read)
assert_match(/Slept 10/, curl_stdout.read)
assert_match(/Connection refused/, rejected_curl_stderr.read)

Process.wait(@server.pid)
Expand Down