Skip to content

Commit 07e0704

Browse files
authored
chore: Allow OS to assign port for unit tests (#336)
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.
1 parent 6308826 commit 07e0704

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
build-linux:
1414
uses: ./.github/workflows/build-gem.yml
1515
strategy:
16+
fail-fast: false
1617
matrix:
1718
version: ["3.2", "jruby-9.4"]
1819
with:

spec/http_util.rb

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,25 @@
77
class StubHTTPServer
88
attr_reader :requests, :port
99

10-
@@next_port = 50000
11-
1210
def initialize(enable_compression: false)
13-
@port = StubHTTPServer.next_port
1411
@enable_compression = enable_compression
15-
begin
16-
base_opts = {
17-
BindAddress: '127.0.0.1',
18-
Port: @port,
19-
AccessLog: [],
20-
Logger: NullLogger.new,
21-
RequestCallback: method(:record_request),
22-
}
23-
@server = create_server(@port, base_opts)
24-
rescue Errno::EADDRINUSE
25-
@port = StubHTTPServer.next_port
26-
retry
27-
end
12+
base_opts = {
13+
BindAddress: '127.0.0.1',
14+
Port: 0, # Let OS assign an available port
15+
AccessLog: [],
16+
Logger: NullLogger.new,
17+
RequestCallback: method(:record_request),
18+
}
19+
@server = create_server(base_opts)
2820
@requests = []
2921
@requests_queue = Queue.new
3022
end
3123

32-
def self.next_port
33-
p = @@next_port
34-
@@next_port = (p + 1 < 60000) ? p + 1 : 50000
35-
p
36-
end
37-
38-
def create_server(port, base_opts)
39-
WEBrick::HTTPServer.new(base_opts)
24+
def create_server(base_opts)
25+
server = WEBrick::HTTPServer.new(base_opts)
26+
# Get the actual port assigned by the OS
27+
@port = server.config[:Port]
28+
server
4029
end
4130

4231
def start

0 commit comments

Comments
 (0)