diff --git a/lib/celluloid/io/dns_resolver.rb b/lib/celluloid/io/dns_resolver.rb index 7def5b5..e3043bb 100644 --- a/lib/celluloid/io/dns_resolver.rb +++ b/lib/celluloid/io/dns_resolver.rb @@ -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 @@ -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) diff --git a/spec/celluloid/io/dns_resolver_spec.rb b/spec/celluloid/io/dns_resolver_spec.rb index ab1d4f4..b7c5ce8 100644 --- a/spec/celluloid/io/dns_resolver_spec.rb +++ b/spec/celluloid/io/dns_resolver_spec.rb @@ -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