Skip to content

Commit

Permalink
[CI] - move test_control_gc_stats_* from test_cli.rb to test_integrat…
Browse files Browse the repository at this point in the history
…ion_pumactl.rb, misc (#3237)

* test_cli.rb - replace plain asserts

* Move test_control_gc_stats_* tests to test_integration_pumactl.rb, misc

* Update integration.rb
  • Loading branch information
MSP-Greg committed Sep 29, 2023
1 parent 02a7d01 commit 107f4c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 81 deletions.
4 changes: 2 additions & 2 deletions test/helpers/integration.rb
Expand Up @@ -333,9 +333,9 @@ def cli_pumactl(argv, unix: false, no_bind: nil)
end

r, w = IO.pipe
Thread.new { Puma::ControlCLI.new(arg, w, w).run }.join
w.close
@ios_to_close << r
Puma::ControlCLI.new(arg, w, w).run
w.close
r
end

Expand Down
70 changes: 1 addition & 69 deletions test/test_cli.rb
Expand Up @@ -280,72 +280,6 @@ def test_control_thread_backtraces
t.join if UNIX_SKT_EXIST
end

def control_gc_stats(uri, cntl)
cli = Puma::CLI.new ["-b", uri,
"--control-url", cntl,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events

t = Thread.new do
cli.run
end

wait_booted

s = yield
s << "GET /gc-stats HTTP/1.0\r\n\r\n"
body = s.read
s.close

lines = body.split("\r\n")
json_line = lines.detect { |l| l[0] == "{" }
pairs = json_line.scan(/\"[^\"]+\": [^,]+/)
gc_stats = {}
pairs.each do |p|
p =~ /\"([^\"]+)\": ([^,]+)/ || raise("Can't parse #{p.inspect}!")
gc_stats[$1] = $2
end
gc_count_before = gc_stats["count"].to_i

s = yield
s << "GET /gc HTTP/1.0\r\n\r\n"
body = s.read # Ignored
s.close

s = yield
s << "GET /gc-stats HTTP/1.0\r\n\r\n"
body = s.read
s.close

lines = body.split("\r\n")
json_line = lines.detect { |l| l[0] == "{" }
gc_stats = JSON.parse(json_line)
gc_count_after = gc_stats["count"].to_i

# Hitting the /gc route should increment the count by 1
assert(gc_count_before < gc_count_after, "make sure a gc has happened")

ensure
cli.launcher.stop if cli
t.join
end

def test_control_gc_stats_tcp
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 }
end

def test_control_gc_stats_unix
skip_unless :unix

uri = "unix://#{@tmp_path2}"
cntl = "unix://#{@tmp_path}"

control_gc_stats(uri, cntl) { UNIXSocket.new @tmp_path }
end

def test_tmp_control
skip_if :jruby, suffix: " - Unknown issue"
Expand All @@ -371,9 +305,7 @@ def test_tmp_control

url = data["control_url"]

m = %r!unix://(.*)!.match(url)

assert m, "'#{url}' is not a URL"
assert_operator url, :start_with?, "unix://", "'#{url}' is not a URL"
end

def test_state_file_callback_filtering
Expand Down
52 changes: 42 additions & 10 deletions test/test_integration_pumactl.rb
Expand Up @@ -102,9 +102,7 @@ def test_refork_cluster

start = Time.now

s = UNIXSocket.new @bind_path
@ios_to_close << s
s << "GET /sleep1 HTTP/1.0\r\n\r\n"
fast_connect("sleep1", unix: true)

# Get the PIDs of the phase 0 workers.
phase0_worker_pids = get_worker_pids 0, wrkrs
Expand Down Expand Up @@ -135,14 +133,11 @@ def test_prune_bundler_with_multiple_workers

cli_server "-q -C test/config/prune_bundler_with_multiple_workers.rb #{set_pumactl_args unix: true} -S #{@state_path}", unix: true

s = UNIXSocket.new @bind_path
@ios_to_close << s
s << "GET / HTTP/1.0\r\n\r\n"

body = s.read
socket = fast_connect(unix: true)
headers, body = read_response(socket)

assert_match "200 OK", body
assert_match "embedded app", body
assert_includes headers, "200 OK"
assert_includes body, "embedded app"

cli_pumactl "stop", unix: true

Expand Down Expand Up @@ -206,4 +201,41 @@ def test_require_dependencies
out = cli_pumactl_spawn "-S #{@state_path} status", no_bind: true
assert_includes out.read, "Puma is started"
end

def control_gc_stats(unix: false)
cli_server "-t1:1 -q test/rackup/hello.ru #{set_pumactl_args unix: unix} -S #{@state_path}"

key = Puma::IS_MRI || TRUFFLE_HEAD ? "count" : "used"

resp_io = cli_pumactl "gc-stats", unix: unix
before = JSON.parse resp_io.read.split("\n", 2).last
gc_before = before[key].to_i

2.times { fast_connect }

resp_io = cli_pumactl "gc", unix: unix
# below shows gc was called (200 reply)
assert_equal "Command gc sent success", resp_io.read.rstrip

resp_io = cli_pumactl "gc-stats", unix: unix
after = JSON.parse resp_io.read.split("\n", 2).last
gc_after = after[key].to_i

# Hitting the /gc route should increment the count by 1
if key == "count"
assert_operator gc_before, :<, gc_after, "make sure a gc has happened"
elsif !(Puma::IS_OSX && Puma::IS_JRUBY)
refute_equal gc_before, gc_after, "make sure a gc has happened"
end
end

def test_control_gc_stats_tcp
@control_tcp_port = UniquePort.call
control_gc_stats
end

def test_control_gc_stats_unix
skip_unless :unix
control_gc_stats unix: true
end
end

0 comments on commit 107f4c3

Please sign in to comment.