From b1a8f9c6ef21ffbbcd64f2d85b2480afc2b1e77f Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 10 Oct 2025 10:25:22 -0400 Subject: [PATCH] chore: Allow OS to assign port for unit tests Previously we started looking for testing ports starting at 5000. If the port was in use, we would incrementally search upwards until we found a port we could use. A recent change in the GH Windows image resulted in a permission error being raised before we could find a port to use: ``` Failure/Error: WEBrick::HTTPServer.new(base_opts) Errno::EACCES: Permission denied - bind(2) for 127.0.0.1:50018 ``` To address this, we are modifying the tests to set a port of 0, which will allow the OS to assign an available port for us. This removes the need for this silly retry logic and hard coded port numbers. --- .github/workflows/ci.yml | 1 + spec/http_util.rb | 37 +++++++++++++------------------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 83090cb2..56e68395 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,7 @@ jobs: build-linux: uses: ./.github/workflows/build-gem.yml strategy: + fail-fast: false matrix: version: ["3.2", "jruby-9.4"] with: diff --git a/spec/http_util.rb b/spec/http_util.rb index a52e4bb6..1bd34073 100644 --- a/spec/http_util.rb +++ b/spec/http_util.rb @@ -7,36 +7,25 @@ class StubHTTPServer attr_reader :requests, :port - @@next_port = 50000 - def initialize(enable_compression: false) - @port = StubHTTPServer.next_port @enable_compression = enable_compression - begin - base_opts = { - BindAddress: '127.0.0.1', - Port: @port, - AccessLog: [], - Logger: NullLogger.new, - RequestCallback: method(:record_request), - } - @server = create_server(@port, base_opts) - rescue Errno::EADDRINUSE - @port = StubHTTPServer.next_port - retry - end + base_opts = { + BindAddress: '127.0.0.1', + Port: 0, # Let OS assign an available port + AccessLog: [], + Logger: NullLogger.new, + RequestCallback: method(:record_request), + } + @server = create_server(base_opts) @requests = [] @requests_queue = Queue.new end - def self.next_port - p = @@next_port - @@next_port = (p + 1 < 60000) ? p + 1 : 50000 - p - end - - def create_server(port, base_opts) - WEBrick::HTTPServer.new(base_opts) + def create_server(base_opts) + server = WEBrick::HTTPServer.new(base_opts) + # Get the actual port assigned by the OS + @port = server.config[:Port] + server end def start