Skip to content

Commit 7318941

Browse files
committed
Use port 0 for automatically assigned ports.
1 parent 1508345 commit 7318941

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

lib/async/http/endpoint.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ def endpoint
177177
@endpoint ||= build_endpoint
178178
end
179179

180+
def endpoint=(endpoint)
181+
@endpoint = build_endpoint(endpoint)
182+
end
183+
180184
def bind(*arguments, &block)
181185
endpoint.bind(*arguments, &block)
182186
end

spec/async/http/body_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
context 'with echo server' do
2222
let(:server) do
23-
Async::HTTP::Server.for(server_endpoint, protocol: described_class) do |request|
23+
Async::HTTP::Server.for(@bound_endpoint, protocol: described_class) do |request|
2424
input = request.body
2525
output = Async::HTTP::Body::Writable.new
2626

@@ -55,7 +55,7 @@
5555
let(:notification) {Async::Notification.new}
5656

5757
let(:server) do
58-
Async::HTTP::Server.for(server_endpoint, protocol: described_class) do |request|
58+
Async::HTTP::Server.for(@bound_endpoint, protocol: described_class) do |request|
5959
body = Async::HTTP::Body::Writable.new
6060

6161
Async::Task.current.async do |task|
@@ -103,8 +103,8 @@
103103
let(:client_context) {authority.client_context}
104104

105105
# Shared port for localhost network tests.
106-
let(:server_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9299", ssl_context: server_context, reuse_port: true)}
107-
let(:client_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9299", ssl_context: client_context, reuse_port: true)}
106+
let(:server_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:0", ssl_context: server_context, reuse_port: true)}
107+
let(:client_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:0", ssl_context: client_context, reuse_port: true)}
108108

109109
it_should_behave_like Async::HTTP::Body
110110
end

spec/async/http/protocol/shared_examples.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@
443443
end
444444

445445
context 'slow server' do
446-
let(:endpoint) {Async::HTTP::Endpoint.parse('http://127.0.0.1:9294', reuse_port: true, timeout: 0.1)}
446+
let(:endpoint) {Async::HTTP::Endpoint.parse('http://127.0.0.1:0', reuse_port: true, timeout: 0.1)}
447447

448448
let(:server) do
449449
Async::HTTP::Server.for(@bound_endpoint) do |request|

spec/async/http/server_context.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
include_context Async::RSpec::Reactor
1313

1414
let(:protocol) {described_class}
15-
let(:endpoint) {Async::HTTP::Endpoint.parse('http://127.0.0.1:9294', timeout: 0.8, reuse_port: true, protocol: protocol)}
15+
let(:endpoint) {Async::HTTP::Endpoint.parse('http://127.0.0.1:0', timeout: 0.8, reuse_port: true, protocol: protocol)}
1616

1717
let(:server_endpoint) {endpoint}
1818
let(:client_endpoint) {endpoint}
@@ -37,7 +37,16 @@
3737
server.run
3838
end
3939

40-
@client = Async::HTTP::Client.new(client_endpoint, protocol: endpoint.protocol, retries: retries)
40+
local_address_endpoint = @bound_endpoint.local_address_endpoint
41+
42+
if timeout = client_endpoint.timeout
43+
local_address_endpoint.each do |endpoint|
44+
endpoint.options = {timeout: timeout}
45+
end
46+
end
47+
48+
client_endpoint.endpoint = local_address_endpoint
49+
@client = Async::HTTP::Client.new(client_endpoint, protocol: client_endpoint.protocol, retries: retries)
4150
end
4251

4352
after do

0 commit comments

Comments
 (0)