1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/test/helper.rb

132 lines
3.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2011-09-23 23:46:33 -04:00
# Copyright (c) 2011 Evan Phoenix
# Copyright (c) 2005 Zed A. Shaw
if %w(2.2.7 2.2.8 2.2.9 2.2.10 2.3.4 2.4.1).include? RUBY_VERSION
2017-11-20 08:24:02 -05:00
begin
require 'stopgap_13632'
rescue LoadError
puts "For test stability, you must install the stopgap_13632 gem."
exit(1)
2017-11-20 08:24:02 -05:00
end
end
require "net/http"
require "timeout"
require "minitest/autorun"
require "minitest/pride"
require "minitest/proveit"
Implement user_supplied_options behavior. When options are passed to the Puma rack handler it is unknown if the options were set via a framework as a default or via a user. Puma currently has 3 different sources of configuration, the user via command line, the config files, and defaults. Rails 5.1+ will record the values actually specified by the user versus the values specified by the frameworks. It passes these values to the Rack handler and now it's up to Puma to do something with that information. When only framework defaults are passed it will set ``` options[:user_supplied_options] = [] ``` When one or more options are specified by the user such as `:Port` then those keys will be in the array. In that example it will look like this ``` options[:user_supplied_options] = [:Port] ``` This change is 100% backwards compatible. If the framework is older and does not pass this information then the `user_supplied_options` will not be set, in that case we assume all values are user supplied. Internally we accomplish this separation by replacing `LeveledOptions` which was a generic way of specifying options with different priorities with a more explicit `UserFileDefaultOptions` this assumes only 3 levels of options and it will use them in the order supplied (user config wins over file based config wins over defaults). Now instead of using 1 dsl to set all values, we use 3. A user dsl, a file dsl and a Configuration.new` will return all 3 DSLs to the block. It's up to the person using the block to use the correct dsl corresponding to the source of data they are getting.
2017-03-03 17:04:56 -05:00
$LOAD_PATH << File.expand_path("../../lib", __FILE__)
Thread.abort_on_exception = true
Implement user_supplied_options behavior. When options are passed to the Puma rack handler it is unknown if the options were set via a framework as a default or via a user. Puma currently has 3 different sources of configuration, the user via command line, the config files, and defaults. Rails 5.1+ will record the values actually specified by the user versus the values specified by the frameworks. It passes these values to the Rack handler and now it's up to Puma to do something with that information. When only framework defaults are passed it will set ``` options[:user_supplied_options] = [] ``` When one or more options are specified by the user such as `:Port` then those keys will be in the array. In that example it will look like this ``` options[:user_supplied_options] = [:Port] ``` This change is 100% backwards compatible. If the framework is older and does not pass this information then the `user_supplied_options` will not be set, in that case we assume all values are user supplied. Internally we accomplish this separation by replacing `LeveledOptions` which was a generic way of specifying options with different priorities with a more explicit `UserFileDefaultOptions` this assumes only 3 levels of options and it will use them in the order supplied (user config wins over file based config wins over defaults). Now instead of using 1 dsl to set all values, we use 3. A user dsl, a file dsl and a Configuration.new` will return all 3 DSLs to the block. It's up to the person using the block to use the correct dsl corresponding to the source of data they are getting.
2017-03-03 17:04:56 -05:00
require "puma"
require "puma/events"
require "puma/detect"
# Either takes a string to do a get request against, or a tuple of [URI, HTTP] where
# HTTP is some kind of Net::HTTP request object (POST, HEAD, etc.)
def hit(uris)
uris.map do |u|
response =
if u.kind_of? String
Net::HTTP.get(URI.parse(u))
else
url = URI.parse(u[0])
Net::HTTP.new(url.host, url.port).start {|h| h.request(u[1]) }
end
assert response, "Didn't get a response: #{u}"
response
end
end
module UniquePort
@port = 3211
def self.call
@port += 1
@port
end
end
module TimeoutEveryTestCase
# our own subclass so we never confused different timeouts
class TestTookTooLong < Timeout::Error
end
def run(*)
::Timeout.timeout(Puma.jruby? ? 120 : 60, TestTookTooLong) { super }
end
end
if ENV['CI']
Minitest::Test.prepend TimeoutEveryTestCase
require 'minitest/retry'
Minitest::Retry.use!
end
module TestSkips
@@next_port = 9000
# usage: skip NO_FORK_MSG unless HAS_FORK
# windows >= 2.6 fork is not defined, < 2.6 fork raises NotImplementedError
HAS_FORK = ::Process.respond_to? :fork
NO_FORK_MSG = "Kernel.fork isn't available on the #{RUBY_PLATFORM} platform"
# socket is required by puma
# usage: skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
UNIX_SKT_EXIST = Object.const_defined? :UNIXSocket
UNIX_SKT_MSG = "UnixSockets aren't available on the #{RUBY_PLATFORM} platform"
# usage: skip_unless_signal_exist? :USR2
def skip_unless_signal_exist?(sig, bt: caller)
signal = sig.to_s
unless Signal.list.key? signal
skip "Signal #{signal} isn't available on the #{RUBY_PLATFORM} platform", bt
end
end
# called with one or more params, like skip_on :jruby, :windows
# optional suffix kwarg is appended to the skip message
# optional suffix bt should generally not used
def skip_on(*engs, suffix: '', bt: caller)
skip_msg = false
engs.each do |eng|
skip_msg = case eng
when :jruby then "Skipped on JRuby#{suffix}" if Puma.jruby?
when :windows then "Skipped on Windows#{suffix}" if Puma.windows?
when :appveyor then "Skipped on Appveyor#{suffix}" if ENV["APPVEYOR"]
when :ci then "Skipped on ENV['CI']#{suffix}" if ENV["CI"]
else false
end
skip skip_msg, bt if skip_msg
end
end
# called with only one param
def skip_unless(eng, bt: caller)
skip_msg = case eng
when :jruby then "Skip unless JRuby" unless Puma.jruby?
when :windows then "Skip unless Windows" unless Puma.windows?
else false
end
skip skip_msg, bt if skip_msg
end
def next_port(incr = 1)
@@next_port += incr
end
end
Minitest::Test.include TestSkips
class Minitest::Test
def self.run(reporter, options = {}) # :nodoc:
prove_it!
super
end
end