Skip to content

Commit

Permalink
add test for systemd plugin and tweak message fetch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
QWYNG committed Oct 29, 2022
1 parent 06e14cf commit e98e6d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
31 changes: 11 additions & 20 deletions lib/puma/plugin/systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ def start(launcher)
end

def status
common = "workers: #{running}/#{max_threads} threads, #{pool_capacity} available, #{backlog} backlog"
if clustered?
"Puma #{Puma::Const::VERSION} cluster: #{booted_workers}/#{workers} #{common}"
messages = Array.new(workers) do |i|
common_message(stats[:worker_status][i][:last_status])
end.join(',')

"Puma #{Puma::Const::VERSION}: cluster: #{booted_workers}/#{workers}, worker_status: [#{messages}]"
else
"Puma #{Puma::Const::VERSION}: #{common}"
"Puma #{Puma::Const::VERSION}: worker: #{common_message(stats)}"
end
end

Expand All @@ -75,30 +78,18 @@ def stats
end

def clustered?
stats.has_key?('workers')
stats.has_key?(:workers)
end

def workers
stats.fetch('workers', 1)
stats.fetch(:workers, 1)
end

def booted_workers
stats.fetch('booted_workers', 1)
end

def running
stats['running']
end

def backlog
stats['backlog']
end

def pool_capacity
stats['pool_capacity']
stats.fetch(:booted_workers, 1)
end

def max_threads
stats['max_threads']
def common_message(stats)
"{ #{stats[:running]}/#{stats[:max_threads]} threads, #{stats[:pool_capacity]} available, #{stats[:backlog]} backlog }"
end
end
29 changes: 25 additions & 4 deletions test/test_integration_systemd.rb → test/test_plugin_systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

require 'sd_notify'

class TestIntegrationSystemd < TestIntegration
class TestPluginSystemd < TestIntegration
def setup
skip "Skipped because Systemd support is linux-only" if windows? || osx?
#skip "Skipped because Systemd support is linux-only" if windows? || osx?
skip_unless :unix
skip_unless_signal_exist? :TERM
skip_if :jruby
Expand Down Expand Up @@ -57,6 +57,27 @@ def test_systemd_watchdog
assert_match(socket_message, "STOPPING=1")
end

def test_systemd_notify
cli_server "test/rackup/hello.ru"
assert_equal(socket_message, "READY=1")

assert_equal(socket_message(70),
"STATUS=Puma #{Puma::Const::VERSION}: worker: { 0/5 threads, 5 available, 0 backlog }")

stop_server
assert_match(socket_message, "STOPPING=1")
end

def test_systemd_cluster_notify
cli_server "-w 2 -q test/rackup/hello.ru"
assert_equal(socket_message, "READY=1")
assert_equal(socket_message(130),
"STATUS=Puma #{Puma::Const::VERSION}: cluster: 2/2, worker_status: [{ 0/5 threads, 5 available, 0 backlog },{ 0/5 threads, 5 available, 0 backlog }]")

stop_server
assert_match(socket_message, "STOPPING=1")
end

private

def assert_restarts_with_systemd(signal, workers: 2)
Expand All @@ -77,7 +98,7 @@ def assert_restarts_with_systemd(signal, workers: 2)
assert_equal socket_message, 'STOPPING=1'
end

def socket_message
@socket.recvfrom(15)[0]
def socket_message(len = 15)
@socket.recvfrom(len)[0]
end
end

0 comments on commit e98e6d7

Please sign in to comment.