Skip to content
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
10 changes: 8 additions & 2 deletions lib/mongo/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def socket(socket_timeout, opts = {})
# (multiple identical items in the returned array). It does not make
# sense to try to connect to the same address more than once, thus
# eliminate duplicates here.
infos = ::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM)
infos = getaddrinfo(host, family)
results = infos.map do |info|
[info[4], info[3]]
end.uniq
Expand Down Expand Up @@ -276,6 +276,12 @@ def to_s

private

# This is a simple wrapper around Socket.getaddrinfo added to
# make testing easier.
def getaddrinfo(host, family)
::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM)
end

def parse_host_port
address = seed.downcase
case address
Expand Down Expand Up @@ -305,7 +311,7 @@ def map_exceptions(csot)
else
raise e
end
rescue IOError, SystemCallError => e
rescue IOError, SystemCallError, ::SocketError => e
raise Error::SocketError, "#{e.class}: #{e} (for #{self})"
rescue OpenSSL::SSL::SSLError => e
raise Error::SocketError, "#{e.class}: #{e} (for #{self})"
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def map_exceptions
yield
rescue Errno::ETIMEDOUT => e
raise Error::SocketTimeoutError, "#{e.class}: #{e} (for #{human_address})"
rescue IOError, SystemCallError => e
rescue IOError, SystemCallError, ::SocketError => e
raise Error::SocketError, "#{e.class}: #{e} (for #{human_address})"
rescue OpenSSL::SSL::SSLError => e
raise Error::SocketError, "#{e.class}: #{e} (for #{human_address})"
Expand Down
2 changes: 1 addition & 1 deletion spec/lite_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module Mrss
require 'mrss/session_registry'
require 'support/local_resource_registry'

if SpecConfig.instance.mri? && !SpecConfig.instance.windows?
if SpecConfig.instance.mri? && (SpecConfig.instance.linux? || SpecConfig.instance.macos?)
require 'timeout_interrupt'
else
require 'timeout'
Expand Down
11 changes: 11 additions & 0 deletions spec/mongo/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@
end
end
end
context 'when a SocketError occurs' do
before do
allow(address).to receive(:getaddrinfo).and_raise(::SocketError)
end

it 'raises a Mongo::Error::SocketError' do
expect {
address.socket(0.0)
}.to raise_error(Mongo::Error::SocketError)
end
end
end

describe '#to_s' do
Expand Down
2 changes: 1 addition & 1 deletion spec/mongo/server/monitor/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
expect(Socket).to receive(:getaddrinfo).and_raise(SocketError.new('Test exception'))
lambda do
connection.connect!
end.should raise_error(SocketError, 'Test exception')
end.should raise_error(Mongo::Error::SocketError, /SocketError: Test exception/)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/mongo/socket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
end
end.to raise_error(Mongo::Error::SocketError, 'OpenSSL::SSL::SSLError: Test error (for fake-address)')
end

it 'maps SocketError and preserves message' do
expect do
socket.send(:map_exceptions) do
raise SocketError.new('Test error')
end
end.to raise_error(Mongo::Error::SocketError, 'SocketError: Test error (for fake-address)')
end
end

describe '#read' do
Expand Down
Loading