From cb3eb19406d0c7d8769d91366fe44b9f2546020f Mon Sep 17 00:00:00 2001 From: Markus Date: Mon, 6 Sep 2021 18:37:11 +0200 Subject: [PATCH] allow multiple after_worker_fork hooks (#2690) * allow multiple after_worker_fork hooks fixes what seems to be a simple typo, now it behaves like expected * update tests for multiple hook calls thanks to MSP-Greg in https://github.com/puma/puma/pull/2690#issuecomment-913708587 --- lib/puma/dsl.rb | 2 +- test/test_config.rb | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/puma/dsl.rb b/lib/puma/dsl.rb index 36f0441056..3c38cd782c 100644 --- a/lib/puma/dsl.rb +++ b/lib/puma/dsl.rb @@ -585,7 +585,7 @@ def on_worker_fork(&block) # end def after_worker_fork(&block) @options[:after_worker_fork] ||= [] - @options[:after_worker_fork] = block + @options[:after_worker_fork] << block end alias_method :after_worker_boot, :after_worker_fork diff --git a/test/test_config.rb b/test/test_config.rb index d4e2443661..2052cc7079 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -363,16 +363,31 @@ def test_silence_single_worker_warning_overwrite def assert_run_hooks(hook_name, options = {}) configured_with = options[:configured_with] || hook_name + # test single, not an array + messages = [] + conf = Puma::Configuration.new + conf.options[hook_name] = -> (a) { + messages << "#{hook_name} is called with #{a}" + } + + conf.run_hooks hook_name, 'ARG', Puma::Events.strings + assert_equal messages, ["#{hook_name} is called with ARG"] + + # test multiple messages = [] conf = Puma::Configuration.new do |c| c.send(configured_with) do |a| - messages << "#{hook_name} is called with #{a}" + messages << "#{hook_name} is called with #{a} one time" + end + + c.send(configured_with) do |a| + messages << "#{hook_name} is called with #{a} a second time" end end conf.load conf.run_hooks hook_name, 'ARG', Puma::Events.strings - assert_equal messages, ["#{hook_name} is called with ARG"] + assert_equal messages, ["#{hook_name} is called with ARG one time", "#{hook_name} is called with ARG a second time"] end end