1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

test_cli.rb - test_control_gc_stats => tcp and unix

split test_control_gc_stats into separate tcp & unix test, add test_control_gc_stats as shared code
This commit is contained in:
MSP-Greg 2019-02-20 14:29:52 -06:00
parent e04c030ac7
commit 550cd9730b

View file

@ -40,8 +40,11 @@ class TestCLI < Minitest::Test
end end
def test_control_for_tcp def test_control_for_tcp
url = "tcp://127.0.0.1:9877/" tcp = next_port
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:9876", cntl = next_port
url = "tcp://127.0.0.1:#{cntl}/"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:#{tcp}",
"--control", url, "--control", url,
"--control-token", "", "--control-token", "",
"test/rackup/lobster.ru"], @events "test/rackup/lobster.ru"], @events
@ -53,18 +56,22 @@ class TestCLI < Minitest::Test
wait_booted wait_booted
s = TCPSocket.new "127.0.0.1", 9877 s = TCPSocket.new "127.0.0.1", cntl
s << "GET /stats HTTP/1.0\r\n\r\n" s << "GET /stats HTTP/1.0\r\n\r\n"
body = s.read body = s.read
s.close
assert_equal '{ "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }', body.split(/\r?\n/).last assert_equal '{ "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }', body.split(/\r?\n/).last
assert_equal '{ "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }', Puma.stats assert_equal '{ "backlog": 0, "running": 0, "pool_capacity": 16, "max_threads": 16 }', Puma.stats
ensure
cli.launcher.stop cli.launcher.stop
t.join t.join
end end
def test_control_clustered def test_control_clustered
skip_on :jruby, :windows, suffix: " - Puma::Binder::UNIXServer is not defined" skip NO_FORK_MSG unless HAS_FORK
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
url = "unix://#{@tmp_path}" url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}", cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
@ -102,7 +109,7 @@ class TestCLI < Minitest::Test
end end
def test_control def test_control
skip_on :jruby, :windows, suffix: " - Puma::Binder::UNIXServer is not defined" skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
url = "unix://#{@tmp_path}" url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}", cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
@ -126,7 +133,7 @@ class TestCLI < Minitest::Test
end end
def test_control_stop def test_control_stop
skip_on :jruby, :windows, suffix: " - Puma::Binder::UNIXServer is not defined" skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
url = "unix://#{@tmp_path}" url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}", cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
@ -148,21 +155,20 @@ class TestCLI < Minitest::Test
t.join t.join
end end
def test_control_gc_stats def control_gc_stats(uri, cntl)
skip_on :jruby, :windows, suffix: " - Puma::Binder::UNIXServer is not defined" cli = Puma::CLI.new ["-b", uri,
url = "unix://#{@tmp_path}" "--control", cntl,
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control", url,
"--control-token", "", "--control-token", "",
"test/rackup/lobster.ru"], @events "test/rackup/lobster.ru"], @events
t = Thread.new { cli.run } t = Thread.new do
t.abort_on_exception = true Thread.current.abort_on_exception = true
cli.run
end
wait_booted wait_booted
s = UNIXSocket.new @tmp_path s = yield
s << "GET /gc-stats HTTP/1.0\r\n\r\n" s << "GET /gc-stats HTTP/1.0\r\n\r\n"
body = s.read body = s.read
s.close s.close
@ -177,15 +183,16 @@ class TestCLI < Minitest::Test
end end
gc_count_before = gc_stats["count"].to_i gc_count_before = gc_stats["count"].to_i
s = UNIXSocket.new @tmp_path s = yield
s << "GET /gc HTTP/1.0\r\n\r\n" s << "GET /gc HTTP/1.0\r\n\r\n"
body = s.read # Ignored body = s.read # Ignored
s.close s.close
s = UNIXSocket.new @tmp_path s = yield
s << "GET /gc-stats HTTP/1.0\r\n\r\n" s << "GET /gc-stats HTTP/1.0\r\n\r\n"
body = s.read body = s.read
s.close s.close
lines = body.split("\r\n") lines = body.split("\r\n")
json_line = lines.detect { |l| l[0] == "{" } json_line = lines.detect { |l| l[0] == "{" }
pairs = json_line.scan(/\"[^\"]+\": [^,]+/) pairs = json_line.scan(/\"[^\"]+\": [^,]+/)
@ -199,13 +206,33 @@ class TestCLI < Minitest::Test
# Hitting the /gc route should increment the count by 1 # Hitting the /gc route should increment the count by 1
assert(gc_count_before < gc_count_after, "make sure a gc has happened") assert(gc_count_before < gc_count_after, "make sure a gc has happened")
cli.launcher.stop ensure
cli.launcher.stop if cli
t.join t.join
end end
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
cntl = "tcp://127.0.0.1:#{cntl_port}/"
control_gc_stats(uri, cntl) { TCPSocket.new "127.0.0.1", cntl_port }
end
def test_control_gc_stats_unix
skip_on :jruby, suffix: " - Hitting /gc route does not increment count"
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
uri = "unix://#{@tmp_path2}"
cntl = "unix://#{@tmp_path}"
control_gc_stats(uri, cntl) { UNIXSocket.new @tmp_path }
end
def test_tmp_control def test_tmp_control
skip_on :jruby skip_on :jruby, suffix: " - Unknown issue"
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"] cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"]
cli.launcher.write_state cli.launcher.write_state
@ -221,7 +248,7 @@ class TestCLI < Minitest::Test
end end
def test_state_file_callback_filtering def test_state_file_callback_filtering
skip_on :jruby, :windows, suffix: " - worker mode not supported" skip NO_FORK_MSG unless HAS_FORK
cli = Puma::CLI.new [ "--config", "test/config/state_file_testing_config.rb", cli = Puma::CLI.new [ "--config", "test/config/state_file_testing_config.rb",
"--state", @tmp_path ] "--state", @tmp_path ]
cli.launcher.write_state cli.launcher.write_state
@ -233,7 +260,7 @@ class TestCLI < Minitest::Test
end end
def test_state def test_state
url = "tcp://127.0.0.1:8232" url = "tcp://127.0.0.1:#{next_port}"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url] cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
cli.launcher.write_state cli.launcher.write_state