From aecb68b6a8ad40dfaa3d2f5a6476cbe93d6a66bd Mon Sep 17 00:00:00 2001 From: Diogo Vernier <10981831+diogovernier@users.noreply.github.com> Date: Thu, 9 Apr 2026 10:03:48 -0300 Subject: [PATCH] Use dynamic port allocation in Puma plugin tests The Puma plugin tests bind to a hardcoded port (9222). When a test teardown completes and the next test setup starts, the OS may still hold the port in TCP TIME_WAIT state, causing EADDRINUSE failures that cascade into nil process lookups and false test failures. Replace the hardcoded port with a dynamically allocated one via TCPServer, so each test gets a guaranteed available port. --- test/integration/puma/plugin_testing.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/integration/puma/plugin_testing.rb b/test/integration/puma/plugin_testing.rb index 14165c9b8..ccabd231b 100644 --- a/test/integration/puma/plugin_testing.rb +++ b/test/integration/puma/plugin_testing.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "test_helper" +require "socket" module PluginTesting extend ActiveSupport::Concern @@ -12,10 +13,12 @@ module PluginTesting setup do FileUtils.mkdir_p Rails.root.join("tmp", "pids") + @port = find_available_port + Dir.chdir("test/dummy") do cmd = %W[ bundle exec puma - -b tcp://127.0.0.1:9222 + -b tcp://127.0.0.1:#{@port} -C config/puma_#{solid_queue_mode}.rb -s config.ru @@ -57,4 +60,11 @@ module PluginTesting def solid_queue_mode raise NotImplementedError end + + def find_available_port + server = TCPServer.new("127.0.0.1", 0) + server.addr[1] + ensure + server&.close + end end