Skip to content

Commit ce9a704

Browse files
Zenspider zenspider/minitest cleanup (#3604)
* Clean up minitest usage: use a subclass of Minitest::Test, hook run_one_method not run, etc This changes the test class in every file, so it looks ugly, but it should make things cleaner in the long run. test_helper.rb could use some more cleanup. It is still including into Minitest::Test when it could just PUT everything in TimeoutTestCase now. * Mark the one as allowed --------- Co-authored-by: Ryan Davis <[email protected]>
1 parent 9058575 commit ce9a704

38 files changed

+87
-109
lines changed

.github/workflows/tests.yml

-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ jobs:
3030
PUMA_TEST_DEBUG: true
3131
TESTOPTS: -v
3232
PUMA_NO_RUBOCOP: true
33-
TERM: BugMinitest
3433

3534
runs-on: ${{ matrix.os }}
3635
if: |
@@ -142,7 +141,6 @@ jobs:
142141
PUMA_TEST_DEBUG: true
143142
TESTOPTS: -v
144143
PUMA_NO_RUBOCOP: true
145-
TERM: BugMinitest
146144

147145
runs-on: ${{ matrix.os }}
148146
if: |

.rubocop.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require:
22
- rubocop-performance
3+
- ./cops/tests_must_timeout.rb
34

45
AllCops:
56
DisabledByDefault: true
@@ -82,3 +83,6 @@ Style/TrailingCommaInArguments:
8283

8384
Style/WhileUntilModifier:
8485
Enabled: true
86+
87+
Puma/TestsMustTimeout:
88+
Enabled: true

cops/tests_must_timeout.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'rubocop'
2+
3+
module RuboCop
4+
module Cop
5+
module Puma
6+
class TestsMustTimeout < Base
7+
extend AutoCorrector
8+
9+
MSG = 'Inherit from TimeoutTestCase instead of Minitest::Test'
10+
11+
def_node_matcher :inherits_from_minitest_test?, <<~PATTERN
12+
(class _ (const (const nil? :Minitest) :Test) ...)
13+
PATTERN
14+
15+
def on_class(node)
16+
return unless inherits_from_minitest_test?(node)
17+
18+
add_offense(node.children[1]) do |corrector|
19+
corrector.replace(node.children[1], 'TimeoutTestCase')
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end

test/helper.rb

+10-55
Original file line numberDiff line numberDiff line change
@@ -87,68 +87,23 @@ def self.call(host = '127.0.0.1')
8787

8888
require "timeout"
8989

90-
if Minitest::VERSION < '5.25'
91-
module TimeoutEveryTestCase
92-
# our own subclass so we never confuse different timeouts
93-
class TestTookTooLong < Timeout::Error
94-
end
95-
96-
def run
97-
with_info_handler do
98-
time_it do
99-
capture_exceptions do
100-
::Timeout.timeout($test_case_timeout, TestTookTooLong) do
101-
before_setup; setup; after_setup
102-
self.send self.name
103-
end
104-
end
105-
106-
capture_exceptions do
107-
::Timeout.timeout($test_case_timeout, TestTookTooLong) do
108-
Minitest::Test::TEARDOWN_METHODS.each { |hook| self.send hook }
109-
end
110-
end
111-
if respond_to? :clean_tmp_paths
112-
clean_tmp_paths
113-
end
114-
end
115-
end
90+
class TimeoutTestCase < Minitest::Test # rubocop:disable Puma/TestsMustTimeout
91+
# our own subclass so we never confuse different timeouts
92+
class TestTookTooLong < Timeout::Error
93+
end
11694

117-
Minitest::Result.from self # per contract
95+
def self.run_one_method(klass, method_name, reporter)
96+
::Timeout.timeout($test_case_timeout, TestTookTooLong) do
97+
super
11898
end
11999
end
120-
else
121-
module TimeoutEveryTestCase
122-
# our own subclass so we never confuse different timeouts
123-
class TestTookTooLong < Timeout::Error
124-
end
125-
126-
def run
127-
time_it do
128-
capture_exceptions do
129-
::Timeout.timeout($test_case_timeout, TestTookTooLong) do
130-
Minitest::Test::SETUP_METHODS.each { |hook| self.send hook }
131-
self.send self.name
132-
end
133-
end
134100

135-
capture_exceptions do
136-
::Timeout.timeout($test_case_timeout, TestTookTooLong) do
137-
Minitest::Test::TEARDOWN_METHODS.each { |hook| self.send hook }
138-
end
139-
end
140-
if respond_to? :clean_tmp_paths
141-
clean_tmp_paths
142-
end
143-
end
144-
145-
Minitest::Result.from self # per contract
146-
end
101+
def teardown
102+
super
103+
clean_tmp_paths if respond_to? :clean_tmp_paths
147104
end
148105
end
149106

150-
Minitest::Test.prepend TimeoutEveryTestCase
151-
152107
if ENV['CI']
153108
require 'minitest/retry'
154109

test/helpers/integration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Only single mode tests go here. Cluster and pumactl tests
99
# have their own files, use those instead
10-
class TestIntegration < Minitest::Test
10+
class TestIntegration < TimeoutTestCase
1111
include TmpPath
1212
HOST = "127.0.0.1"
1313
TOKEN = "xxyyzz"

test/test_app_status.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
require "puma/app/status"
66
require "rack"
77

8-
class TestAppStatus < Minitest::Test
9-
8+
class TestAppStatus < TimeoutTestCase
109
class FakeServer
1110
def initialize
1211
@status = :running

test/test_binder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
require "puma/events"
99
require "puma/configuration"
1010

11-
class TestBinderBase < Minitest::Test
11+
class TestBinderBase < TimeoutTestCase
1212
include SSLHelper if ::Puma::HAS_SSL
1313
include TmpPath
1414

test/test_bundle_pruner.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'puma/events'
44

5-
class TestBundlePruner < Minitest::Test
5+
class TestBundlePruner < TimeoutTestCase
66

77
PUMA_VERS = "puma-#{Puma::Const::PUMA_VERSION}"
88

test/test_busy_worker.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
require_relative "helper"
22
require_relative "helpers/test_puma/puma_socket"
33

4-
class TestBusyWorker < Minitest::Test
5-
4+
class TestBusyWorker < TimeoutTestCase
65
include ::TestPuma::PumaSocket
76

87
def setup

test/test_cli.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require "json"
88
require "psych"
99

10-
class TestCLI < Minitest::Test
10+
class TestCLI < TimeoutTestCase
1111
include SSLHelper if ::Puma::HAS_SSL
1212
include TmpPath
1313
include TestPuma::PumaSocket

test/test_config.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require 'puma/log_writer'
77
require 'rack'
88

9-
class TestConfigFile < Minitest::Test
9+
class TestConfigFile < TimeoutTestCase
1010
parallelize_me!
1111

1212
def test_default_max_threads
@@ -633,7 +633,7 @@ def assert_warning_for_hooks_defined_in_single_mode(hook_name)
633633
end
634634

635635
# contains tests that cannot run parallel
636-
class TestConfigFileSingle < Minitest::Test
636+
class TestConfigFileSingle < TimeoutTestCase
637637
def test_custom_logger_from_DSL
638638
conf = Puma::Configuration.new { |c| c.load 'test/config/custom_logger.rb' }
639639

@@ -645,7 +645,7 @@ def test_custom_logger_from_DSL
645645
end
646646

647647
# Thread unsafe modification of ENV
648-
class TestEnvModifificationConfig < Minitest::Test
648+
class TestEnvModifificationConfig < TimeoutTestCase
649649
def test_double_bind_port
650650
port = (rand(10_000) + 30_000).to_s
651651
env = { "PORT" => port }
@@ -659,7 +659,7 @@ def test_double_bind_port
659659
end
660660
end
661661

662-
class TestConfigEnvVariables < Minitest::Test
662+
class TestConfigEnvVariables < TimeoutTestCase
663663
def test_config_loads_correct_min_threads
664664
assert_equal 0, Puma::Configuration.new.options.default_options[:min_threads]
665665

@@ -719,7 +719,7 @@ def test_config_preloads_app_if_using_workers
719719
end
720720
end
721721

722-
class TestConfigFileWithFakeEnv < Minitest::Test
722+
class TestConfigFileWithFakeEnv < TimeoutTestCase
723723
def setup
724724
FileUtils.mkpath("config/puma")
725725
File.write("config/puma/fake-env.rb", "")

test/test_error_logger.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'puma/error_logger'
22
require_relative "helper"
33

4-
class TestErrorLogger < Minitest::Test
4+
class TestErrorLogger < TimeoutTestCase
55
Req = Struct.new(:env, :body)
66

77
def test_stdio

test/test_events.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'puma/events'
22
require_relative "helper"
33

4-
class TestEvents < Minitest::Test
4+
class TestEvents < TimeoutTestCase
55
def test_register_callback_with_block
66
res = false
77

test/test_example_cert_expiration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#
88
# These tests will start to fail 1 month before the certs expire
99
#
10-
class TestExampleCertExpiration < Minitest::Test
10+
class TestExampleCertExpiration < TimeoutTestCase
1111
EXAMPLES_DIR = File.expand_path '../examples/puma', __dir__
1212
EXPIRE_THRESHOLD = Time.now.utc - (60 * 60 * 24 * 30) # 30 days
1313

test/test_http10.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "puma/puma_http11"
44

5-
class Http10ParserTest < Minitest::Test
5+
class Http10ParserTest < TimeoutTestCase
66
def test_parse_simple
77
parser = Puma::HttpParser.new
88
req = {}

test/test_iobuffer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "puma/io_buffer"
44

5-
class TestIOBuffer < Minitest::Test
5+
class TestIOBuffer < TimeoutTestCase
66
attr_accessor :iobuf
77
def setup
88
self.iobuf = Puma::IOBuffer.new

test/test_json_serialization.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require "json"
33
require "puma/json_serialization"
44

5-
class TestJSONSerialization < Minitest::Test
5+
class TestJSONSerialization < TimeoutTestCase
66
parallelize_me! unless JRUBY_HEAD
77

88
def test_json_generates_string_for_hash_with_string_keys

test/test_launcher.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
require 'puma/log_writer'
66

77
# Intermittent failures & errors when run parallel in GHA, local use may run fine.
8-
9-
10-
class TestLauncher < Minitest::Test
8+
class TestLauncher < TimeoutTestCase
119
include TmpPath
1210

1311
def test_prints_thread_traces

test/test_log_writer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'puma/log_writer'
33
require_relative "helper"
44

5-
class TestLogWriter < Minitest::Test
5+
class TestLogWriter < TimeoutTestCase
66
def test_null
77
log_writer = Puma::LogWriter.null
88

test/test_minissl.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "puma/minissl" if ::Puma::HAS_SSL
44

5-
class TestMiniSSL < Minitest::Test
5+
class TestMiniSSL < TimeoutTestCase
66

77
if Puma.jruby?
88
def test_raises_with_invalid_keystore_file

test/test_normalize.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require "puma/request"
66

7-
class TestNormalize < Minitest::Test
7+
class TestNormalize < TimeoutTestCase
88
parallelize_me!
99

1010
include Puma::Request

test/test_null_io.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require "puma/null_io"
66

7-
class TestNullIO < Minitest::Test
7+
class TestNullIO < TimeoutTestCase
88
parallelize_me!
99

1010
attr_accessor :nio

test/test_out_of_band_server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require_relative "helper"
22

3-
class TestOutOfBandServer < Minitest::Test
3+
class TestOutOfBandServer < TimeoutTestCase
44
parallelize_me!
55

66
def setup

test/test_persistent.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_relative "helper"
44
require_relative "helpers/test_puma/puma_socket"
55

6-
class TestPersistent < Minitest::Test
6+
class TestPersistent < TimeoutTestCase
77
parallelize_me!
88

99
include ::TestPuma::PumaSocket

test/test_puma_localhost_authority.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
require "openssl" unless Object.const_defined? :OpenSSL
1212
end
1313

14-
class TestPumaLocalhostAuthority < Minitest::Test
14+
class TestPumaLocalhostAuthority < TimeoutTestCase
1515
include TestPuma
1616
include TestPuma::PumaSocket
1717

@@ -45,7 +45,7 @@ def test_localhost_authority_file_generated
4545

4646
end if ::Puma::HAS_SSL && !Puma::IS_JRUBY
4747

48-
class TestPumaSSLLocalhostAuthority < Minitest::Test
48+
class TestPumaSSLLocalhostAuthority < TimeoutTestCase
4949
include TestPuma
5050
include TestPuma::PumaSocket
5151

test/test_puma_server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def backtrace; nil; end
1010
def message; "no backtrace error"; end
1111
end
1212

13-
class TestPumaServer < Minitest::Test
13+
class TestPumaServer < TimeoutTestCase
1414
parallelize_me!
1515

1616
include TestPuma

test/test_puma_server_current.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def call(env)
1313
end
1414
end
1515

16-
class PumaServerCurrentTest < Minitest::Test
16+
class PumaServerCurrentTest < TimeoutTestCase
1717
parallelize_me!
1818

1919
def setup

test/test_puma_server_hijack.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# The sleep statements may not be needed for local CI, but are needed
1515
# for use with GitHub Actions...
1616

17-
class TestPumaServerHijack < Minitest::Test
17+
class TestPumaServerHijack < TimeoutTestCase
1818
parallelize_me!
1919

2020
def setup

0 commit comments

Comments
 (0)