|
7 | 7 | class StubHTTPServer |
8 | 8 | attr_reader :requests, :port |
9 | 9 |
|
10 | | - @@next_port = 50000 |
11 | | - |
12 | 10 | def initialize(enable_compression: false) |
13 | | - @port = StubHTTPServer.next_port |
14 | 11 | @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) |
28 | 20 | @requests = [] |
29 | 21 | @requests_queue = Queue.new |
30 | 22 | end |
31 | 23 |
|
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 |
40 | 29 | end |
41 | 30 |
|
42 | 31 | def start |
|
0 commit comments