Skip to content

Commit

Permalink
Centralise usage of listen to wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 24, 2024
1 parent 7d4004a commit 749a42a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
gem "bake-test-external"

gem "sus-fixtures-openssl"
gem "sus-fixtures-async"
end
11 changes: 2 additions & 9 deletions lib/io/endpoint/bound_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ class BoundEndpoint < Generic
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false)
sockets = endpoint.bind

sockets.each do |server|
# This is somewhat optional. We want to have a generic interface as much as possible so that users of this interface can just call it without knowing a lot of internal details. Therefore, we ignore errors here if it's because the underlying socket does not support the operation.
begin
server.listen(backlog)
rescue Errno::EOPNOTSUPP
# Ignore.
end

server.close_on_exec = close_on_exec
sockets.each do |socket|
socket.close_on_exec = close_on_exec
end

return self.new(endpoint, sockets, **endpoint.options)
Expand Down
7 changes: 6 additions & 1 deletion lib/io/endpoint/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ def bind(local_address, protocol: 0, reuse_address: true, reuse_port: nil, linge
socket.bind(local_address.to_sockaddr)

if backlog
socket.listen(backlog)
begin
# Generally speaking, bind/listen is a common pattern, but it's not applicable to all socket types. We ignore the error if it's not supported as the alternative is exposing this upstream, which seems less desirable than handling it here. In other words, `bind` in this context means "prepare it to accept connections", whatever that means for the given socket type.
socket.listen(backlog)
rescue Errno::EOPNOTSUPP
# Ignore.
end
end
rescue
socket&.close
Expand Down
4 changes: 0 additions & 4 deletions test/io/endpoint/bound_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
sockets = internal_endpoint.bind
server = sockets.first

server.listen(1)

thread = Thread.new do
peer, address = server.accept
peer.close
Expand All @@ -52,8 +50,6 @@
sockets = internal_endpoint.bind
server = sockets.first

server.listen(1)

thread = Thread.new do
Thread.current.report_on_exception = false

Expand Down
24 changes: 23 additions & 1 deletion test/io/endpoint/unix_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'io/endpoint/unix_endpoint'
require 'with_temporary_directory'
require 'sus/fixtures/async/reactor_context'

describe IO::Endpoint::UNIXEndpoint do
include WithTemporaryDirectory
Expand Down Expand Up @@ -54,12 +55,33 @@
end

describe IO::Endpoint do
let(:endpoint) {subject.unix("/tmp/test.ipc")}
let(:endpoint) {subject.unix("/tmp/test.ipc", Socket::SOCK_DGRAM)}

with '.unix' do
it "can construct endpoint from path" do
expect(endpoint).to be_a(IO::Endpoint::UNIXEndpoint)
expect(endpoint).to have_attributes(path: be == "/tmp/test.ipc")
end

with "a simple UDP server" do
include Sus::Fixtures::Async::ReactorContext

it "can send and receive UDP messages" do
server_task = Async do
endpoint.bind do |server|
expect(server).to be_a(Socket)
packet, address = server.recvfrom(512)

expect(packet).to be == "Hello World!"
end
end

endpoint.connect do |peer|
peer.sendmsg("Hello World!")
end

server_task.wait
end
end
end
end

0 comments on commit 749a42a

Please sign in to comment.