Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Test for graceful close on shared bound endpoints. #80

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions lib/async/io/shared_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,9 @@ def bind
task = Async::Task.current

@wrappers.each do |server|
server = server.dup

task.async do |task|
task.annotate "binding to #{server.inspect}"

begin
yield server, task
ensure
server.close
end
yield server, task
end
end
end
Expand All @@ -109,9 +102,9 @@ def connect
end
end

def accept(backlog = nil, &block)
def accept(backlog = nil, **options, &block)
bind do |server|
server.accept_each(&block)
server.accept_each(**options, &block)
end
end

Expand Down
37 changes: 36 additions & 1 deletion spec/async/io/shared_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
include_context Async::RSpec::Reactor

describe '#bound' do
let(:endpoint) {Async::IO::Endpoint.udp("localhost", 5123, timeout: 10)}
let(:endpoint) {Async::IO::Endpoint.tcp("localhost", 5123, timeout: 10)}

it "can bind to shared endpoint" do
bound_endpoint = described_class.bound(endpoint)
Expand All @@ -33,6 +33,41 @@

bound_endpoint.close
end

it "can close a bound endpoint to terminate accept loop" do
bound_endpoint = described_class.bound(endpoint)
expect(bound_endpoint.wrappers).to_not be_empty

server_task = Async do
bound_endpoint.accept do |io|
io.close
end
end

connect = proc do
endpoint.connect do |io|
io.write "Hello World"
io.close
end
end

connect.call

wrapper = bound_endpoint.wrappers.first
expect(wrapper).to be_a Async::IO::Socket

bound_endpoint.close
expect(wrapper).to be_closed

expect do
begin
# Either ECONNRESET or ECONNREFUSED can be raised here.
connect.call
rescue Errno::ECONNRESET
raise Errno::ECONNREFUSED
end
end.to raise_error(Errno::ECONNREFUSED)
end
end

describe '#connected' do
Expand Down