From 50dd845ab9194da5f2cf6562ffcb69acacc43064 Mon Sep 17 00:00:00 2001 From: Kentaro Hayashi Date: Mon, 9 Nov 2026 15:43:56 +0900 Subject: [PATCH] test_out_http: use an unused port for each test case (#5618) **Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: The HTTP server thread was kept in a class variable, and it is shared with sub test cases. Each sub test case set it to nil at startup, so the server of the parent test case was never stopped and kept port 19880. The dynamic endpoint host validation tests used the same port, so their server could not bind it. Fixed ports can also conflict with other processes or with tests that run in parallel. Before: The server thread died with "Address already in use - bind(2) for 127.0.0.1:19880 (Errno::EADDRINUSE)". After: Each test case keeps its own server in a class instance variable, and uses a port from unused_port. **Docs Changes**: N/A **Release Note**: N/A Signed-off-by: Kentaro Hayashi Co-authored-by: Claude Signed-off-by: github-actions[bot] --- test/plugin/test_out_http.rb | 43 ++++++++++++------------------------ 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/test/plugin/test_out_http.rb b/test/plugin/test_out_http.rb index 10b63f4740..1c6b2b1322 100644 --- a/test/plugin/test_out_http.rb +++ b/test/plugin/test_out_http.rb @@ -23,22 +23,27 @@ class HTTPOutputTest < Test::Unit::TestCase DEFAULT_LOGGER = ::WEBrick::Log.new(::STDOUT, ::WEBrick::BasicLog::FATAL) class << self - # Use class variable to reduce server start/shutdown time + # Share one server in each test case to reduce server start/shutdown time. + # Don't use a class variable for it, because sub test cases share it + # and lose the server of the parent test case. + attr_accessor :http_server_thread, :server_port + def startup @@result = nil @@auth_handler = nil - @@http_server_thread = nil + @http_server_thread = nil + @server_port = unused_port(protocol: :tcp) end def shutdown - @@http_server_thread.kill - @@http_server_thread.join + @http_server_thread.kill + @http_server_thread.join rescue end end def server_port - 19880 + self.class.server_port end def base_endpoint @@ -130,7 +135,7 @@ def run_http_server FileUtils.rm_rf(TMP_DIR) @@result = Result.new(nil, nil, {}, nil) - @@http_server_thread ||= Thread.new do + self.class.http_server_thread ||= Thread.new do run_http_server end @@ -360,10 +365,6 @@ def test_write_with_disabled_unrecoverable FileUtils.rm_rf(TMP_DIR) end - def server_port - 19881 - end - def test_basic_auth d = create_driver(config + %[ @@ -416,10 +417,6 @@ def test_basic_auth_with_invalid_auth ) end - def server_port - 19883 - end - def test_aws_sigv4_sts_role_arn stub(Aws::AssumeRoleCredentials).new do |credentials_provider| stub(credentials_provider).credentials { @@ -498,10 +495,6 @@ def test_aws_sigv4_no_role end sub_test_case 'HTTPS' do - def server_port - 19882 - end - def server_config config = super # WEBrick supports self-generated self-signed certificate @@ -537,10 +530,6 @@ def test_write_with_https end sub_test_case 'GZIP' do - def server_port - 19882 - end - data(:json_array, [false, true]) data(:buffer_compress, ["text", "gzip"]) def test_write_with_gzip @@ -572,10 +561,6 @@ def test_write_with_gzip end sub_test_case 'connection_reuse' do - def server_port - 19883 - end - def test_connection_recreation d = create_driver(%[ endpoint http://127.0.0.1:#{server_port}/test @@ -589,9 +574,9 @@ def test_connection_recreation data = @@result.data # Restart server to simulate connection loss - @@http_server_thread.kill - @@http_server_thread.join - @@http_server_thread = Thread.new do + self.class.http_server_thread.kill + self.class.http_server_thread.join + self.class.http_server_thread = Thread.new do run_http_server end