1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/test/test_integration_systemd.rb
Ewoud Kohl van Wijngaarden 288a4cf756
Add systemd notify and watchdog support (#2438)
* Adds systemd notification support

* Improve systemd notification support

This takes the work by @acmh and improves on it. This is done by
squashing all commits and rebasing it. Then the following changes were
made:

* Dropped SD_NOTIFY env var. There is aleady the NOTIFY_SOCKET env var
  presented by systemd and is redundant.
* Move code is pushed in Puma::Systemd
* on_reload now emits RELOADING=1 notification to systemd
* Drop lower bound check on usec. Systemd can only be configured in
  seconds and it's hard to misconfigure. The actual code should be safe.
* Clean up integration tests and skip on JRuby

Co-authored-by: Artur Montenegro <artur.montenegro@tempest.com.br>
2020-10-26 16:02:31 -06:00

62 lines
1.4 KiB
Ruby

require_relative "helper"
require_relative "helpers/integration"
require 'sd_notify'
class TestIntegrationSystemd < TestIntegration
def setup
skip "Skipped because Systemd support is linux-only" if windows? || osx?
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless_signal_exist? :TERM
skip_on :jruby
super
::Dir::Tmpname.create("puma_socket") do |sockaddr|
@sockaddr = sockaddr
@socket = Socket.new(:UNIX, :DGRAM, 0)
socket_ai = Addrinfo.unix(sockaddr)
@socket.bind(socket_ai)
ENV["NOTIFY_SOCKET"] = sockaddr
end
end
def teardown
return if skipped?
@socket.close if @socket
File.unlink(@sockaddr) if @sockaddr
@socket = nil
@sockaddr = nil
ENV["NOTIFY_SOCKET"] = nil
ENV["WATCHDOG_USEC"] = nil
end
def socket_message
@socket.recvfrom(15)[0]
end
def test_systemd_integration
cli_server "test/rackup/hello.ru"
assert_equal(socket_message, "READY=1")
connection = connect
restart_server connection
assert_equal(socket_message, "RELOADING=1")
assert_equal(socket_message, "READY=1")
stop_server
assert_equal(socket_message, "STOPPING=1")
end
def test_systemd_watchdog
ENV["WATCHDOG_USEC"] = "1_000_000"
cli_server "test/rackup/hello.ru"
assert_equal(socket_message, "READY=1")
assert_equal(socket_message, "WATCHDOG=1")
stop_server
assert_match(socket_message, "STOPPING=1")
end
end