Skip to content

Commit

Permalink
Add on_booted support for Rack::Handler::Puma
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Olichwirowicz authored and Marcin Olichwirowicz committed Feb 7, 2023
1 parent b4ad27f commit ca69608
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rack/handler/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def self.config(app, options = {})
end
end

conf = ::Puma::Configuration.new(options, default_options) do |user_config, file_config, default_config|
@events = options[:events] || ::Puma::Events.new

conf = ::Puma::Configuration.new(options, default_options.merge({events: @events})) do |user_config, file_config, default_config|
if options.delete(:Verbose)
require 'rack/common_logger'
app = Rack::CommonLogger.new(app, STDOUT)
Expand Down Expand Up @@ -65,7 +67,7 @@ def self.run(app, **options)

log_writer = options.delete(:Silent) ? ::Puma::LogWriter.strings : ::Puma::LogWriter.stdio

launcher = ::Puma::Launcher.new(conf, :log_writer => log_writer)
launcher = ::Puma::Launcher.new(conf, :log_writer => log_writer, events: @events)

yield launcher if block_given?
begin
Expand Down
33 changes: 33 additions & 0 deletions test/test_rack_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
if Rack::RELEASE < '3'

require "rack/handler/puma"
require "puma/events"

class TestHandlerGetStrSym < Minitest::Test
def test_handler
Expand All @@ -15,6 +16,38 @@ def test_handler
end
end

class TestOnBootedHandler < Minitest::Test
def app
Proc.new {|env| @input = env; [200, {}, ["hello world"]]}
end

def test_on_booted
on_booted = false
events = Puma::Events.new
events.on_booted do
on_booted = true
end

launcher = nil
thread = Thread.new do
Rack::Handler::Puma.run(app, events: events, Silent: true) do |l|
launcher = l
end
end

# Wait for launcher to boot
Timeout.timeout(10) do
sleep 0.5 until launcher
end
sleep 1.5 unless Puma::IS_MRI

launcher.stop
thread.join

assert_equal on_booted, true
end
end

class TestPathHandler < Minitest::Test
def app
Proc.new {|env| @input = env; [200, {}, ["hello world"]]}
Expand Down

0 comments on commit ca69608

Please sign in to comment.