2019-07-27 12:47:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-12 15:16:55 -04:00
|
|
|
require_relative "helper"
|
2019-10-18 01:49:05 -04:00
|
|
|
require_relative "helpers/config_file"
|
2011-12-07 16:42:53 -05:00
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
require "puma/configuration"
|
2020-03-10 13:41:17 -04:00
|
|
|
require 'puma/events'
|
2011-12-07 16:42:53 -05:00
|
|
|
|
2020-04-19 21:48:20 -04:00
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
class TestConfigFile < TestConfigFileBase
|
|
|
|
parallelize_me!
|
2017-10-16 18:22:14 -04:00
|
|
|
|
2020-03-01 20:12:36 -05:00
|
|
|
def test_default_max_threads
|
|
|
|
max_threads = 16
|
|
|
|
max_threads = 5 if RUBY_ENGINE.nil? || RUBY_ENGINE == 'ruby'
|
|
|
|
assert_equal max_threads, Puma::Configuration.new.default_max_threads
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-04-29 14:51:25 -04:00
|
|
|
def test_app_from_rackup
|
2016-02-06 22:00:29 -05:00
|
|
|
conf = Puma::Configuration.new do |c|
|
2017-04-11 17:08:18 -04:00
|
|
|
c.rackup "test/rackup/hello-bind.ru"
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-04-29 14:51:25 -04:00
|
|
|
conf.load
|
|
|
|
|
2020-03-08 23:08:06 -04:00
|
|
|
# suppress deprecation warning of Rack (>= 2.2.0)
|
|
|
|
# > Parsing options from the first comment line is deprecated!\n
|
|
|
|
assert_output(nil, nil) do
|
|
|
|
conf.app
|
|
|
|
end
|
2015-04-29 14:51:25 -04:00
|
|
|
|
|
|
|
assert_equal ["tcp://127.0.0.1:9292"], conf.options[:binds]
|
|
|
|
end
|
|
|
|
|
2011-12-07 16:42:53 -05:00
|
|
|
def test_app_from_app_DSL
|
2016-02-06 22:00:29 -05:00
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.load "test/config/app.rb"
|
|
|
|
end
|
2011-12-07 16:42:53 -05:00
|
|
|
conf.load
|
|
|
|
|
|
|
|
app = conf.app
|
|
|
|
|
2013-02-06 17:44:51 -05:00
|
|
|
assert_equal [200, {}, ["embedded app"]], app.call({})
|
2011-12-07 16:42:53 -05:00
|
|
|
end
|
2014-02-17 12:07:17 -05:00
|
|
|
|
2018-12-18 19:13:59 -05:00
|
|
|
def test_ssl_configuration_from_DSL
|
|
|
|
conf = Puma::Configuration.new do |config|
|
|
|
|
config.load "test/config/ssl_config.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
bind_configuration = conf.options.file_options[:binds].first
|
|
|
|
app = conf.app
|
|
|
|
|
|
|
|
assert bind_configuration =~ %r{ca=.*ca.crt}
|
|
|
|
assert bind_configuration =~ /verify_mode=peer/
|
|
|
|
|
|
|
|
assert_equal [200, {}, ["embedded app"]], app.call({})
|
|
|
|
end
|
|
|
|
|
2018-11-12 11:53:06 -05:00
|
|
|
def test_ssl_bind
|
|
|
|
skip_on :jruby
|
|
|
|
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.ssl_bind "0.0.0.0", "9292", {
|
|
|
|
cert: "/path/to/cert",
|
|
|
|
key: "/path/to/key",
|
|
|
|
verify_mode: "the_verify_mode",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
conf.load
|
|
|
|
|
2019-08-03 15:56:30 -04:00
|
|
|
ssl_binding = "ssl://0.0.0.0:9292?cert=/path/to/cert&key=/path/to/key&verify_mode=the_verify_mode&no_tlsv1=false&no_tlsv1_1=false"
|
2018-11-12 11:53:06 -05:00
|
|
|
assert_equal [ssl_binding], conf.options[:binds]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ssl_bind_with_cipher_filter
|
|
|
|
skip_on :jruby
|
|
|
|
|
|
|
|
cipher_filter = "!aNULL:AES+SHA"
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.ssl_bind "0.0.0.0", "9292", {
|
|
|
|
cert: "cert",
|
|
|
|
key: "key",
|
|
|
|
ssl_cipher_filter: cipher_filter,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
ssl_binding = conf.options[:binds].first
|
|
|
|
assert ssl_binding.include?("&ssl_cipher_filter=#{cipher_filter}")
|
|
|
|
end
|
|
|
|
|
2019-10-04 13:30:10 -04:00
|
|
|
def test_ssl_bind_with_ca
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.ssl_bind "0.0.0.0", "9292", {
|
|
|
|
cert: "/path/to/cert",
|
|
|
|
ca: "/path/to/ca",
|
|
|
|
key: "/path/to/key",
|
|
|
|
verify_mode: :peer,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
ssl_binding = conf.options[:binds].first
|
|
|
|
assert_match "ca=/path/to/ca", ssl_binding
|
|
|
|
assert_match "verify_mode=peer", ssl_binding
|
|
|
|
end
|
|
|
|
|
2018-03-19 17:05:15 -04:00
|
|
|
def test_lowlevel_error_handler_DSL
|
2016-02-06 22:00:29 -05:00
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.load "test/config/app.rb"
|
|
|
|
end
|
2014-02-17 12:07:17 -05:00
|
|
|
conf.load
|
|
|
|
|
|
|
|
app = conf.options[:lowlevel_error_handler]
|
|
|
|
|
|
|
|
assert_equal [200, {}, ["error page"]], app.call({})
|
|
|
|
end
|
2015-02-16 18:44:43 -05:00
|
|
|
|
2016-03-26 10:48:36 -04:00
|
|
|
def test_allow_users_to_override_default_options
|
|
|
|
conf = Puma::Configuration.new(restart_cmd: 'bin/rails server')
|
|
|
|
|
|
|
|
assert_equal 'bin/rails server', conf.options[:restart_cmd]
|
|
|
|
end
|
|
|
|
|
2016-11-20 14:10:34 -05:00
|
|
|
def test_overwrite_options
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.workers 3
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
assert_equal conf.options[:workers], 3
|
|
|
|
conf.options[:workers] += 1
|
|
|
|
assert_equal conf.options[:workers], 4
|
|
|
|
end
|
|
|
|
|
2017-03-06 13:28:44 -05:00
|
|
|
def test_explicit_config_files
|
|
|
|
conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do |c|
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
assert_match(/:3000$/, conf.options[:binds].first)
|
|
|
|
end
|
|
|
|
|
2017-01-30 23:29:19 -05:00
|
|
|
def test_parameters_overwrite_files
|
|
|
|
conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do |c|
|
|
|
|
c.port 3030
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
assert_match(/:3030$/, conf.options[:binds].first)
|
|
|
|
assert_equal 3, conf.options[:min_threads]
|
|
|
|
assert_equal 5, conf.options[:max_threads]
|
|
|
|
end
|
|
|
|
|
2017-10-16 18:22:14 -04:00
|
|
|
def test_config_files_default
|
|
|
|
conf = Puma::Configuration.new do
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [nil], conf.config_files
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_files_with_dash
|
|
|
|
conf = Puma::Configuration.new(config_files: ['-']) do
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [], conf.config_files
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_files_with_existing_path
|
|
|
|
conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ['test/config/settings.rb'], conf.config_files
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_files_with_non_existing_path
|
|
|
|
conf = Puma::Configuration.new(config_files: ['test/config/typo/settings.rb']) do
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ['test/config/typo/settings.rb'], conf.config_files
|
|
|
|
end
|
|
|
|
|
2018-03-19 17:05:15 -04:00
|
|
|
def test_config_files_with_integer_convert
|
|
|
|
conf = Puma::Configuration.new(config_files: ['test/config/with_integer_convert.rb']) do
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
assert_equal 6, conf.options[:persistent_timeout]
|
|
|
|
assert_equal 3, conf.options[:first_data_timeout]
|
|
|
|
assert_equal 2, conf.options[:workers]
|
|
|
|
assert_equal 4, conf.options[:min_threads]
|
|
|
|
assert_equal 8, conf.options[:max_threads]
|
|
|
|
assert_equal 90, conf.options[:worker_timeout]
|
|
|
|
assert_equal 120, conf.options[:worker_boot_timeout]
|
|
|
|
assert_equal 150, conf.options[:worker_shutdown_timeout]
|
|
|
|
end
|
|
|
|
|
2019-02-21 15:31:35 -05:00
|
|
|
def test_config_raise_exception_on_sigterm
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.raise_exception_on_sigterm false
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
assert_equal conf.options[:raise_exception_on_sigterm], false
|
|
|
|
conf.options[:raise_exception_on_sigterm] = true
|
|
|
|
assert_equal conf.options[:raise_exception_on_sigterm], true
|
|
|
|
end
|
2020-03-10 13:41:17 -04:00
|
|
|
|
|
|
|
def test_run_hooks_on_restart_hook
|
|
|
|
assert_run_hooks :on_restart
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_hooks_before_worker_fork
|
|
|
|
assert_run_hooks :before_worker_fork, configured_with: :on_worker_fork
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_hooks_after_worker_fork
|
|
|
|
assert_run_hooks :after_worker_fork
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_hooks_before_worker_boot
|
|
|
|
assert_run_hooks :before_worker_boot, configured_with: :on_worker_boot
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_hooks_before_worker_shutdown
|
|
|
|
assert_run_hooks :before_worker_shutdown, configured_with: :on_worker_shutdown
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_hooks_before_fork
|
|
|
|
assert_run_hooks :before_fork
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_hooks_and_exception
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.on_restart do |a|
|
|
|
|
raise RuntimeError, 'Error from hook'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
events = Puma::Events.strings
|
|
|
|
|
|
|
|
conf.run_hooks :on_restart, 'ARG', events
|
|
|
|
expected = /WARNING hook on_restart failed with exception \(RuntimeError\) Error from hook/
|
|
|
|
assert_match expected, events.stdout.string
|
|
|
|
end
|
|
|
|
|
2020-04-21 19:33:13 -04:00
|
|
|
def test_config_does_not_load_workers_by_default
|
|
|
|
assert_equal 0, Puma::Configuration.new.options.default_options[:workers]
|
|
|
|
end
|
|
|
|
|
2020-03-10 13:41:17 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def assert_run_hooks(hook_name, options = {})
|
|
|
|
configured_with = options[:configured_with] || hook_name
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
conf = Puma::Configuration.new do |c|
|
|
|
|
c.send(configured_with) do |a|
|
|
|
|
messages << "#{hook_name} is called with #{a}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
conf.load
|
|
|
|
|
|
|
|
conf.run_hooks hook_name, 'ARG', Puma::Events.strings
|
|
|
|
assert_equal messages, ["#{hook_name} is called with ARG"]
|
|
|
|
end
|
2019-07-27 12:47:19 -04:00
|
|
|
end
|
2019-02-21 15:31:35 -05:00
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
# Thread unsafe modification of ENV
|
|
|
|
class TestEnvModifificationConfig < TestConfigFileBase
|
|
|
|
def test_double_bind_port
|
|
|
|
port = (rand(10_000) + 30_000).to_s
|
|
|
|
with_env("PORT" => port) do
|
|
|
|
conf = Puma::Configuration.new do |user_config, file_config, default_config|
|
|
|
|
user_config.bind "tcp://#{Puma::Configuration::DefaultTCPHost}:#{port}"
|
|
|
|
file_config.load "test/config/app.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
conf.load
|
|
|
|
assert_equal ["tcp://0.0.0.0:#{port}"], conf.options[:binds]
|
|
|
|
end
|
2017-10-16 18:22:14 -04:00
|
|
|
end
|
2019-07-27 12:47:19 -04:00
|
|
|
end
|
2017-10-16 18:22:14 -04:00
|
|
|
|
2020-04-19 21:48:20 -04:00
|
|
|
class TestConfigEnvVariables < TestConfigFileBase
|
|
|
|
def test_config_loads_correct_min_threads
|
2020-04-21 19:33:13 -04:00
|
|
|
assert_equal 0, Puma::Configuration.new.options.default_options[:min_threads]
|
2020-04-19 21:48:20 -04:00
|
|
|
|
|
|
|
with_env("MIN_THREADS" => "7") do
|
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal 7, conf.options.default_options[:min_threads]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_env("PUMA_MIN_THREADS" => "8") do
|
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal 8, conf.options.default_options[:min_threads]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_loads_correct_max_threads
|
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal conf.default_max_threads, conf.options.default_options[:max_threads]
|
|
|
|
|
|
|
|
with_env("MAX_THREADS" => "7") do
|
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal 7, conf.options.default_options[:max_threads]
|
|
|
|
end
|
|
|
|
|
2020-04-20 02:22:44 -04:00
|
|
|
with_env("PUMA_MAX_THREADS" => "8") do
|
2020-04-19 21:48:20 -04:00
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal 8, conf.options.default_options[:max_threads]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_loads_workers_from_env
|
|
|
|
with_env("WEB_CONCURRENCY" => "9") do
|
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal 9, conf.options.default_options[:workers]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_does_not_preload_app_if_using_workers
|
|
|
|
with_env("WEB_CONCURRENCY" => "0") do
|
|
|
|
conf = Puma::Configuration.new
|
|
|
|
assert_equal false, conf.options.default_options[:preload_app]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_preloads_app_if_using_workers
|
|
|
|
with_env("WEB_CONCURRENCY" => "2") do
|
2020-04-21 19:33:13 -04:00
|
|
|
preload = Puma.forkable?
|
2020-04-19 21:48:20 -04:00
|
|
|
conf = Puma::Configuration.new
|
2020-04-20 03:03:39 -04:00
|
|
|
assert_equal preload, conf.options.default_options[:preload_app]
|
2020-04-19 21:48:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
class TestConfigFileWithFakeEnv < TestConfigFileBase
|
|
|
|
def setup
|
|
|
|
FileUtils.mkpath("config/puma")
|
|
|
|
File.write("config/puma/fake-env.rb", "")
|
|
|
|
end
|
2015-02-16 18:44:43 -05:00
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
def test_config_files_with_rack_env
|
|
|
|
with_env('RACK_ENV' => 'fake-env') do
|
|
|
|
conf = Puma::Configuration.new do
|
2015-02-16 18:44:43 -05:00
|
|
|
end
|
2019-07-27 12:47:19 -04:00
|
|
|
|
|
|
|
assert_equal ['config/puma/fake-env.rb'], conf.config_files
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-11 00:10:02 -05:00
|
|
|
def test_config_files_with_rails_env
|
|
|
|
with_env('RAILS_ENV' => 'fake-env', 'RACK_ENV' => nil) do
|
|
|
|
conf = Puma::Configuration.new do
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ['config/puma/fake-env.rb'], conf.config_files
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
def test_config_files_with_specified_environment
|
|
|
|
conf = Puma::Configuration.new do
|
2015-02-16 18:44:43 -05:00
|
|
|
end
|
2019-07-27 12:47:19 -04:00
|
|
|
|
|
|
|
conf.options[:environment] = 'fake-env'
|
|
|
|
|
|
|
|
assert_equal ['config/puma/fake-env.rb'], conf.config_files
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
FileUtils.rm_r("config/puma")
|
|
|
|
end
|
2011-12-07 16:42:53 -05:00
|
|
|
end
|