Skip to content

Commit

Permalink
tests - clean up - three commits (#1903)
Browse files Browse the repository at this point in the history
* tests - cleanup rackup folder, add sleep.ru, add helpers/app.rb

* tests - change nextport to UniquePort.call, remove nextport, mutex

clean up, mutex removes port issues with parallel testing

* test_integration.rb - make UNIXSocket file names unique
  • Loading branch information
MSP-Greg authored and nateberkopec committed Sep 5, 2019
1 parent b4ff8e1 commit 9efac9e
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 70 deletions.
11 changes: 3 additions & 8 deletions test/helper.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class TestIntegration < Minitest::Test
HOST = '127.0.0.1'

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 @@ -46,7 +47,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 @@ -101,9 +102,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 @@ -159,17 +160,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 @@ -182,7 +182,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 @@ -242,15 +242,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 @@ -285,7 +285,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 @@ -352,9 +352,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 @@ -369,7 +369,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

0 comments on commit 9efac9e

Please sign in to comment.