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

Commit 1aa768f

Browse files
committed
Test for graceful close on shared bound endpoints.
1 parent 262e295 commit 1aa768f

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

lib/async/io/shared_endpoint.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,9 @@ def bind
7777
task = Async::Task.current
7878

7979
@wrappers.each do |server|
80-
server = server.dup
81-
8280
task.async do |task|
8381
task.annotate "binding to #{server.inspect}"
84-
85-
begin
86-
yield server, task
87-
ensure
88-
server.close
89-
end
82+
yield server, task
9083
end
9184
end
9285
end
@@ -109,9 +102,9 @@ def connect
109102
end
110103
end
111104

112-
def accept(backlog = nil, &block)
105+
def accept(backlog = nil, **options, &block)
113106
bind do |server|
114-
server.accept_each(&block)
107+
server.accept_each(**options, &block)
115108
end
116109
end
117110

spec/async/io/shared_endpoint_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
include_context Async::RSpec::Reactor
1111

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

1515
it "can bind to shared endpoint" do
1616
bound_endpoint = described_class.bound(endpoint)
@@ -33,6 +33,36 @@
3333

3434
bound_endpoint.close
3535
end
36+
37+
it "can close a bound endpoint to terminate accept loop" do
38+
bound_endpoint = described_class.bound(endpoint)
39+
expect(bound_endpoint.wrappers).to_not be_empty
40+
41+
server_task = Async do
42+
bound_endpoint.accept do |io|
43+
io.close
44+
end
45+
end
46+
47+
connect = proc do
48+
endpoint.connect do |io|
49+
io.write "Hello World"
50+
io.close
51+
end
52+
end
53+
54+
connect.call
55+
56+
wrapper = bound_endpoint.wrappers.first
57+
expect(wrapper).to be_a Async::IO::Socket
58+
59+
bound_endpoint.close
60+
expect(wrapper).to be_closed
61+
62+
expect do
63+
connect.call
64+
end.to raise_error(Errno::ECONNRESET)
65+
end
3666
end
3767

3868
describe '#connected' do

0 commit comments

Comments
 (0)