Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

[DNSResolver] Raise SocketError when there’s no connection. #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion lib/celluloid/io/dns_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Celluloid
module IO
# Asynchronous DNS resolver using Celluloid::IO::UDPSocket
class DNSResolver
class SocketNameResolutionError < SocketError; end

# Maximum UDP packet we'll accept
MAX_PACKET_SIZE = 512
DNS_PORT = 53
Expand All @@ -29,7 +31,9 @@ def initialize

# The non-blocking secret sauce is here, as this is actually a
# Celluloid::IO::UDPSocket
@socket = UDPSocket.new(@server.family)
unless @socket = UDPSocket.new(@server.family)
fail SocketNameResolutionError, "nodename nor servname provided, or not known"
end
end

def resolve(hostname)
Expand Down
5 changes: 5 additions & 0 deletions spec/celluloid/io/dns_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
expect(results).to be_an_instance_of(Resolv::IPv4)
end
end

it "raises SocketError if unable to connect to the nameserver" do
allow(Celluloid::IO::UDPSocket).to receive(:new).and_return(nil)
expect { Celluloid::IO::DNSResolver.new }.to raise_error(Celluloid::IO::DNSResolver::SocketNameResolutionError)
end
end
end