Browse files

Merge pull request #370 from mongodb/refactor-socket-factory

Refactor socket creation.
  • Loading branch information...
2 parents 38d0974 + 45bbf48 commit c200faf81f927f63097461d5f15204f71d716dab @durran durran committed Mar 2, 2014
View
7 lib/mongo/pool.rb
@@ -165,12 +165,7 @@ def create_pool(server)
size: server.options[:pool_size],
timeout: server.options[:pool_timeout]
) do
- Connection.new(
- server.address.ip,
- server.address.port,
- server.options[:timeout],
- server.options
- )
+ Connection.new(server.address, server.options[:timeout], server.options)
end
end
View
19 lib/mongo/pool/connection.rb
@@ -25,11 +25,8 @@ class Connection
# @since 3.0.0
TIMEOUT = 5
- # @return [ String ] host The host to connect to.
- attr_reader :host
-
- # @return [ Integer ] port The port to connect on.
- attr_reader :port
+ # @return [ Mongo::Server::Address ] address The address to connect to.
+ attr_reader :address
# @return [ Float ] timeout The connection timeout.
attr_reader :timeout
@@ -46,7 +43,7 @@ class Connection
#
# @since 3.0.0
def connect!
- @socket = Socket.create(host, port, timeout, ssl_opts) unless socket
+ @socket = address.socket(timeout, ssl_opts) unless socket
socket.connect! and true
end
@@ -72,17 +69,15 @@ def disconnect!
# Initialize a new socket connection from the client to the server.
#
# @example Create the connection.
- # Connection.new('127.0.0.1', 27017, 10)
+ # Connection.new(address, 10)
#
- # @param [ String ] host The host to connect to.
- # @param [ Integer ] port The port to connect to.
+ # @param [ Mongo::Server::Address ] address The address to connect to.
# @param [ Float ] timeout The connection timeout.
# @param [ Hash ] options The connection options.
#
# @since 3.0.0
- def initialize(host, port, timeout = nil, options = {})
- @host = host
- @port = port
+ def initialize(address, timeout = nil, options = {})
+ @address = address
@timeout = timeout || TIMEOUT
@ssl_opts = options.reject { |k, v| !k.to_s.start_with?('ssl') }
@socket = nil
View
40 lib/mongo/pool/socket.rb
@@ -16,43 +16,3 @@
require 'mongo/pool/socket/tcp'
require 'mongo/pool/socket/ssl'
require 'mongo/pool/socket/unix'
-
-module Mongo
- class Pool
- module Socket
-
- class << self
-
- # Factory method to create a specific type of socket based on the
- # provided options.
- #
- # @example Create a TCP socket.
- # Socket.create('127.0.0.1', 27017, 5)
- #
- # @example Create an SSL socket.
- # Socket.create('127.0.0.1', 27017, 5, :ssl => true)
- #
- # @example Create a Unix socket.
- # Socket.create('/path/to/socket.sock', nil, 5)
- #
- # @param [ String ] host The host to connect to.
- # @param [ String, nil ] port The port to connect to.
- # @param [ Integer ] timeout The connection timeout.
- # @param [ Hash ] options The ssl options.
- #
- # @return [ Socket ] The socket.
- #
- # @since 3.0.0
- def create(host, port, timeout, options = {})
- if !options.empty?
- Socket::SSL.new(host, port, timeout, options)
- elsif port.nil?
- Socket::Unix.new(host, timeout)
- else
- Socket::TCP.new(host, port, timeout)
- end
- end
- end
- end
- end
-end
View
84 lib/mongo/pool/socket/connectable.rb
@@ -24,9 +24,30 @@ module Socket
module Connectable
include ::Socket::Constants
+ # Error message for timeouts on socket calls.
+ #
+ # @since 3.0.0
+ TIMEOUT_ERROR = 'Socket request timed out'.freeze
+
+ # The pack directive for timeouts.
+ #
+ # @since 3.0.0
+ TIMEOUT_PACK = 'l_2'.freeze
+
+ # Error message for SSL related exceptions.
+ #
+ # @since 3.0.0
+ SSL_ERROR = 'SSL handshake failed. MongoDB may not be configured with SSL support.'.freeze
+
+ # @return [ Integer ] family The socket family (IPv4, IPv6, Unix).
+ attr_reader :family
+
# @return [ String ] host The host to connect to.
attr_reader :host
+ # @return [ Integer ] port The port to connect to.
+ attr_reader :port
+
# @return [ Float ] timeout The connection timeout.
attr_reader :timeout
@@ -72,7 +93,7 @@ def close
#
# @since 3.0.0
def read(length)
- data = handle_socket_error { @socket.read(length) }
+ data = handle_errors { @socket.read(length) }
unless data
raise SocketError, "Attempted to read #{length} bytes from the socket but got none."
end
@@ -91,7 +112,7 @@ def read(length)
#
# @since 3.0.0
def gets(*args)
- handle_socket_error { @socket.gets(*args) }
+ handle_errors { @socket.gets(*args) }
end
# Read a single byte from the socket.
@@ -103,7 +124,7 @@ def gets(*args)
#
# @since 3.0.0
def readbyte
- handle_socket_error { @socket.readbyte }
+ handle_errors { @socket.readbyte }
end
# Writes data to the socket instance.
@@ -117,45 +138,46 @@ def readbyte
#
# @since 3.0.0
def write(*args)
- handle_socket_error { @socket.write(*args) }
+ handle_errors { @socket.write(*args) }
end
private
- def handle_connect
- error = nil
- addr_info = ::Socket.getaddrinfo(host, nil, AF_UNSPEC, SOCK_STREAM)
- addr_info.each do |info|
- begin
- sock = create_socket(info[4])
- socket_addr = ::Socket.pack_sockaddr_in(port, info[3])
- sock.connect(socket_addr)
- return sock
- rescue IOError, SystemCallError => e
- error = e
- end
- end
- raise error
- end
-
- def create_socket(family)
+ def default_socket
sock = ::Socket.new(family, SOCK_STREAM, 0)
- sock.set_encoding('binary') if sock.respond_to?(:set_encoding)
- sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) if family != AF_UNIX
- timeout_value = [timeout, 0].pack('l_2')
- sock.setsockopt(SOL_SOCKET, SO_RCVTIMEO, timeout_value)
- sock.setsockopt(SOL_SOCKET, SO_SNDTIMEO, timeout_value)
+ sock.set_encoding(BSON::BINARY)
+ sock.setsockopt(SOL_SOCKET, SO_RCVTIMEO, encoded_timeout)
+ sock.setsockopt(SOL_SOCKET, SO_SNDTIMEO, encoded_timeout)
sock
end
- def handle_socket_error
+ def encoded_timeout
+ @encoded_timeout ||= [ timeout, 0 ].pack(TIMEOUT_PACK)
+ end
+
+ def handle_errors
yield
rescue Errno::ETIMEDOUT
- raise Mongo::SocketTimeoutError, 'Socket request timed out.'
- rescue IOError, SystemCallError
- raise Mongo::SocketError, 'A socket error occurred.'
+ raise Mongo::SocketTimeoutError, TIMEOUT_ERROR
+ rescue IOError, SystemCallError => e
+ raise Mongo::SocketError, e.message
rescue OpenSSL::SSL::SSLError
- raise Mongo::SocketError, 'SSL handshake failed. MongoDB may not be configured with SSL support.'
+ raise Mongo::SocketError, SSL_ERROR
+ end
+
+ def initialize!
+ Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
+ @socket = initialize_socket
+ yield if block_given?
+ self
+ end
+ end
+
+ def initialize_socket
+ sock = default_socket
+ sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
+ sock.connect(::Socket.pack_sockaddr_in(port, host))
+ sock
end
end
end
View
20 lib/mongo/pool/socket/ssl.rb
@@ -26,9 +26,6 @@ class SSL
# @return [ OpenSSL::SSL::SSLContext ] context The SSL context.
attr_reader :context
- # @return [ Integer ] port The port to connect to.
- attr_reader :port
-
# Establishes the socket connection and performs
# optional SSL valiation.
#
@@ -41,9 +38,7 @@ class SSL
#
# @since 3.0.0
def connect!
- Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
- @socket = handle_connect
-
+ initialize! do
# Apply ssl wrapper and perform handshake.
ssl_socket = OpenSSL::SSL::SSLSocket.new(@socket, context)
ssl_socket.sync_close = true
@@ -55,7 +50,6 @@ def connect!
raise Mongo::SocketError, 'SSL handshake failed due to a hostname mismatch.'
end
end
- self
end
end
@@ -66,10 +60,11 @@ def connect!
# SSL.new('127.0.0.1', 30, 27017)
# SSL.new('127.0.0.1', 30, 27017)
#
- # @param host [ String ] The hostname or IP address.
- # @param port [ Integer ] The port number.
- # @param timeout [ Integer ] The socket timeout value.
- # @param opts [ Hash ] Optional settings and configuration values.
+ # @param [ String ] host The hostname or IP address.
+ # @param [ Integer ] port The port number.
+ # @param [ Float ] timeout The socket timeout value.
+ # @param [ Integer ] family The socket family.
+ # @param [ Hash ] opts Optional settings and configuration values.
#
# @option opts [ true, false ] :connect (true) If true calls connect
# before returning the object instance.
@@ -86,10 +81,11 @@ def connect!
# from the other end of the socket connection. Implies :ssl_verify.
#
# @since 3.0.0
- def initialize(host, port, timeout, opts = {})
+ def initialize(host, port, timeout, family, opts = {})
@host = host
@port = port
@timeout = timeout
+ @family = family
@context = Context.create(opts)
end
View
18 lib/mongo/pool/socket/tcp.rb
@@ -22,9 +22,6 @@ module Socket
class TCP
include Socket::Connectable
- # @return [ Integer ] port The port to connect to.
- attr_reader :port
-
# Establishes a socket connection.
#
# @example Connect the socket.
@@ -37,10 +34,7 @@ class TCP
#
# @since 3.0.0
def connect!
- Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
- @socket = handle_connect
- self
- end
+ initialize!
end
# Initializes a new TCP socket.
@@ -49,15 +43,17 @@ def connect!
# TCP.new('::1', 27017, 30)
# TCP.new('127.0.0.1', 27017, 30)
#
- # @param host [ String ] The hostname or IP address.
- # @param port [ Integer ] The port number.
- # @param timeout [ Integer ] The socket timeout value.
+ # @param [ String ] host The hostname or IP address.
+ # @param [ Integer ] port The port number.
+ # @param [ Float ] timeout The socket timeout value.
+ # @param [ Integer ] family The socket family.
#
# @since 3.0.0
- def initialize(host, port, timeout)
+ def initialize(host, port, timeout, family)
@host = host
@port = port
@timeout = timeout
+ @family = family
end
end
end
View
27 lib/mongo/pool/socket/unix.rb
@@ -31,30 +31,31 @@ class Unix
#
# @since 3.0.0
def connect!
- Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
- begin
- @socket = create_socket(AF_UNIX)
- @socket.connect(host)
- self
- rescue IOError, SystemCallError => e
- @socket.close if @socket
- raise e
- end
- end
+ initialize!
end
# Initializes a new Unix socket.
#
# @example Create the Unix socket.
# Unix.new('/path/to/socket.sock', 30)
#
- # @param path [ String ] The path to the unix socket.
- # @param timeout [ Integer ] The socket timeout value.
+ # @param [ String ] path The path to the unix socket.
+ # @param [ Float ] timeout The socket timeout value.
+ # @param [ Integer ] family The socket family.
#
# @since 3.0.0
- def initialize(path, timeout)
+ def initialize(path, timeout, family)
@host = path
@timeout = timeout
+ @family = family
+ end
+
+ private
+
+ def initialize_socket
+ sock = default_socket
+ sock.connect(host)
+ sock
end
end
end
View
10 lib/mongo/server.rb
@@ -49,6 +49,16 @@ def ==(other)
address == other.address
end
+ # Instantiate a new server object. Will start the background refresh and
+ # subscribe to the appropriate events.
+ #
+ # @example Initialize the server.
+ # Mongo::Server.new('127.0.0.1:27017')
+ #
+ # @param [ String ] address The host:port address to connect to.
+ # @param [ Hash ] options The server options.
+ #
+ # @since 3.0.0
def initialize(address, options = {})
@address = Address.new(address)
@options = options
View
6 lib/mongo/server/address.rb
@@ -15,7 +15,7 @@
require 'mongo/server/address/resolvable'
require 'mongo/server/address/ipv4'
require 'mongo/server/address/ipv6'
-require 'mongo/server/address/sock'
+require 'mongo/server/address/unix'
require 'forwardable'
module Mongo
@@ -31,7 +31,7 @@ class Address
# Delegate the ip, host, and port methods to the resolver.
#
# @since 3.0.0
- def_delegators :@resolver, :ip, :host, :port
+ def_delegators :@resolver, :ip, :host, :port, :socket
# @return [ Integer ] port The port to the connect to.
attr_reader :resolver
@@ -92,7 +92,7 @@ def hash
# @since 3.0.0
def initialize(address, options = {})
case address
- when Sock::MATCH then @resolver = Sock.new(address)
+ when Unix::MATCH then @resolver = Unix.new(address)
when IPv6::MATCH then @resolver = IPv6.new(address)
else @resolver = IPv4.new(address)
end
View
19 lib/mongo/server/address/ipv4.rb
@@ -55,6 +55,25 @@ def initialize(address)
def pattern
Resolv::IPv4::Regex
end
+
+ # Get a socket for the provided address type, given the options.
+ #
+ # @example Get an IPv4 socket.
+ # ipv4.socket(5, :ssl => true)
+ #
+ # @param [ Float ] timeout The socket timeout.
+ # @param [ Hash ] ssl_opts SSL options.
+ #
+ # @return [ Pool::Socket::SSL, Pool::Socket::TCP ] The socket.
+ #
+ # @since 3.0.0
+ def socket(timeout, ssl_opts = {})
+ unless ssl_opts.empty?
+ Pool::Socket::SSL.new(ip, port, timeout, Socket::PF_INET, ssl_opts)
+ else
+ Pool::Socket::TCP.new(ip, port, timeout, Socket::PF_INET)
+ end
+ end
end
end
end
View
19 lib/mongo/server/address/ipv6.rb
@@ -55,6 +55,25 @@ def initialize(address)
def pattern
Resolv::IPv6::Regex
end
+
+ # Get a socket for the provided address type, given the options.
+ #
+ # @example Get an IPv6 socket.
+ # ipv4.socket(5, :ssl => true)
+ #
+ # @param [ Float ] timeout The socket timeout.
+ # @param [ Hash ] ssl_opts SSL options.
+ #
+ # @return [ Pool::Socket::SSL, Pool::Socket::TCP ] The socket.
+ #
+ # @since 3.0.0
+ def socket(timeout, ssl_opts = {})
+ unless ssl_opts.empty?
+ Pool::Socket::SSL.new(ip, port, timeout, Socket::PF_INET6, ssl_opts)
+ else
+ Pool::Socket::TCP.new(ip, port, timeout, Socket::PF_INET6)
+ end
+ end
end
end
end
View
17 lib/mongo/server/address/sock.rb → lib/mongo/server/address/unix.rb
@@ -19,7 +19,7 @@ class Address
# Sets up socket addresses.
#
# @since 3.0.0
- class Sock
+ class Unix
include Resolvable
# The regular expression to use to match a socket path.
@@ -38,6 +38,21 @@ class Sock
def initialize(address)
@host = address
end
+
+ # Get a socket for the provided address type, given the options.
+ #
+ # @example Get a Unix socket.
+ # ipv4.socket(5)
+ #
+ # @param [ Float ] timeout The socket timeout.
+ # @param [ Hash ] ssl_opts SSL options - ignored.
+ #
+ # @return [ Pool::Socket::Unix ] The socket.
+ #
+ # @since 3.0.0
+ def socket(timeout, ssl_opts = {})
+ Pool::Socket::Unix.new(host, timeout, Socket::AF_UNIX)
+ end
end
end
end
View
28 spec/mongo/pool/connection_spec.rb
@@ -2,10 +2,14 @@
describe Mongo::Pool::Connection do
+ let(:address) do
+ Mongo::Server::Address.new('127.0.0.1:27017')
+ end
+
describe '#connect!' do
let(:connection) do
- described_class.new('127.0.0.1', 27017)
+ described_class.new(address)
end
context 'when no socket exists' do
@@ -53,7 +57,7 @@
context 'when a socket is not connected' do
let(:connection) do
- described_class.new('127.0.0.1', 27017)
+ described_class.new(address)
end
it 'does not raise an error' do
@@ -64,7 +68,7 @@
context 'when a socket is connected' do
let(:connection) do
- described_class.new('127.0.0.1', 27017)
+ described_class.new(address)
end
before do
@@ -83,15 +87,11 @@
context 'when host and port are provided' do
let(:connection) do
- described_class.new('127.0.0.1', 27017)
- end
-
- it 'sets the host' do
- expect(connection.host).to eq('127.0.0.1')
+ described_class.new(address)
end
- it 'sets the port' do
- expect(connection.port).to eq(27017)
+ it 'sets the address' do
+ expect(connection.address).to eq(address)
end
it 'sets the socket to nil' do
@@ -106,7 +106,7 @@
context 'when timeout options are provided' do
let(:connection) do
- described_class.new('127.0.0.1', 27017, 10)
+ described_class.new(address, 10)
end
it 'sets the timeout' do
@@ -117,7 +117,7 @@
context 'when ssl options are provided' do
let(:connection) do
- described_class.new('127.0.0.1', 27017, nil, :ssl => true)
+ described_class.new(address, nil, :ssl => true)
end
it 'sets the ssl options' do
@@ -129,7 +129,7 @@
describe '#read' do
let(:connection) do
- described_class.new('127.0.0.1', 27017, 5)
+ described_class.new(address, 5)
end
let(:documents) do
@@ -170,7 +170,7 @@
describe '#write' do
let(:connection) do
- described_class.new('127.0.0.1', 27017, 5)
+ described_class.new(address, 5)
end
let(:documents) do
View
12 spec/mongo/pool/socket/ssl_spec.rb
@@ -13,7 +13,7 @@
end
before do
- expect(socket).to receive(:handle_connect).and_return(sock)
+ expect(socket).to receive(:initialize_socket).and_return(sock)
expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(sock, socket.context).and_return(wrapper)
expect(wrapper).to receive(:sync_close=).with(true)
expect(wrapper).to receive(:connect)
@@ -26,7 +26,7 @@
context 'when verifying the certificate' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 5, :ssl_verify => true)
+ described_class.new('127.0.0.1', 27017, 5, Socket::PF_INET, :ssl_verify => true)
end
before do
@@ -42,7 +42,7 @@
context 'when not verifying the certificate' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 5, :ssl => true)
+ described_class.new('127.0.0.1', 27017, 5, Socket::PF_INET, :ssl => true)
end
it 'connects the socket using ssl' do
@@ -54,7 +54,7 @@
describe '#initialize' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 10)
+ described_class.new('127.0.0.1', 27017, 10, Socket::PF_INET)
end
it 'sets the host' do
@@ -79,7 +79,7 @@
context 'when the verify mode is not nil' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 10, :ssl_verify => true)
+ described_class.new('127.0.0.1', 27017, 10, Socket::PF_INET, :ssl_verify => true)
end
it 'returns true' do
@@ -90,7 +90,7 @@
context 'when the verify mode is nil' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 10)
+ described_class.new('127.0.0.1', 27017, 10, Socket::PF_INET)
end
it 'returns false' do
View
4 spec/mongo/pool/socket/tcp_spec.rb
@@ -5,7 +5,7 @@
describe '#connect' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 10)
+ described_class.new('127.0.0.1', 27017, 10, Socket::PF_INET)
end
let(:connected) do
@@ -24,7 +24,7 @@
describe '#initialize' do
let(:socket) do
- described_class.new('127.0.0.1', 27017, 10)
+ described_class.new('127.0.0.1', 27017, 10, Socket::PF_INET)
end
it 'sets the host' do
View
11 spec/mongo/pool/socket/unix_spec.rb
@@ -5,7 +5,7 @@
describe '#connect!' do
let(:socket) do
- described_class.new('/path/to/socket.sock', 10)
+ described_class.new('/path/to/socket.sock', 10, Socket::AF_UNIX)
end
let(:sock) do
@@ -15,9 +15,7 @@
context 'when an error occurs when connecting' do
before do
- expect(socket).to receive(:create_socket).with(Socket::Constants::AF_UNIX).and_return(sock)
- expect(sock).to receive(:connect).with('/path/to/socket.sock').and_raise(IOError)
- expect(sock).to receive(:close)
+ expect(socket).to receive(:initialize_socket).and_raise(IOError)
end
it 're-raises the exception' do
@@ -30,8 +28,7 @@
context 'when no error occurs connecting' do
before do
- expect(socket).to receive(:create_socket).with(Socket::Constants::AF_UNIX).and_return(sock)
- expect(sock).to receive(:connect)
+ expect(socket).to receive(:initialize_socket).and_return(sock)
end
it 'connects and returns the socket' do
@@ -43,7 +40,7 @@
describe '#initialize' do
let(:socket) do
- described_class.new('/path/to/socket.sock', 10)
+ described_class.new('/path/to/socket.sock', 10, Socket::AF_UNIX)
end
it 'sets the host' do
View
40 spec/mongo/pool/socket_spec.rb
@@ -1,40 +0,0 @@
-require 'spec_helper'
-
-describe Mongo::Pool::Socket do
-
- describe '.create' do
-
- context 'when the port is nil' do
-
- let(:socket) do
- described_class.create('/path/to/socket.sock', nil, 5)
- end
-
- it 'creates a unix socket' do
- expect(socket).to be_a(Mongo::Pool::Socket::Unix)
- end
- end
-
- context 'when ssl options exist' do
-
- let(:socket) do
- described_class.create('127.0.0.1', 27017, 5, :ssl => true)
- end
-
- it 'creates an ssl socket' do
- expect(socket).to be_a(Mongo::Pool::Socket::SSL)
- end
- end
-
- context 'when a port and no ssl options exist' do
-
- let(:socket) do
- described_class.create('127.0.0.1', 27017, 5)
- end
-
- it 'creates a tcp socket' do
- expect(socket).to be_a(Mongo::Pool::Socket::TCP)
- end
- end
- end
-end
View
6 spec/mongo/pool_spec.rb
@@ -68,7 +68,7 @@
end
it 'returns a new connection' do
- expect(connection.host).to eq(server.address.ip)
+ expect(connection.address).to eq(server.address)
end
it 'puts the new connection on the thread local stack' do
@@ -87,7 +87,7 @@
end
it 'returns the threads connection' do
- expect(pool.checkout.host).to eq(server.address.ip)
+ expect(pool.checkout.address).to eq(server.address)
end
it 'keeps the connection on the thread local stack' do
@@ -106,7 +106,7 @@
end
it 'returns a new connection' do
- expect(pool.checkout.host).to eq(server.address.ip)
+ expect(pool.checkout.address).to eq(server.address)
end
it 'does not return the same connection instance' do
View
37 spec/mongo/server/address/ipv4_spec.rb
@@ -42,4 +42,41 @@
end
end
end
+
+ describe '#socket' do
+
+ let(:resolver) do
+ described_class.new('127.0.0.1')
+ end
+
+ context 'when ssl options are provided' do
+
+ let(:socket) do
+ resolver.socket(5, :ssl => true)
+ end
+
+ it 'returns an ssl socket' do
+ expect(socket).to be_a(Mongo::Pool::Socket::SSL)
+ end
+
+ it 'sets the family as ipv4' do
+ expect(socket.family).to eq(Socket::PF_INET)
+ end
+ end
+
+ context 'when ssl options are not provided' do
+
+ let(:socket) do
+ resolver.socket(5)
+ end
+
+ it 'returns a tcp socket' do
+ expect(socket).to be_a(Mongo::Pool::Socket::TCP)
+ end
+
+ it 'sets the family a ipv4' do
+ expect(socket.family).to eq(Socket::PF_INET)
+ end
+ end
+ end
end
View
37 spec/mongo/server/address/ipv6_spec.rb
@@ -42,4 +42,41 @@
end
end
end
+
+ describe '#socket' do
+
+ let(:resolver) do
+ described_class.new('[::1]')
+ end
+
+ context 'when ssl options are provided' do
+
+ let(:socket) do
+ resolver.socket(5, :ssl => true)
+ end
+
+ it 'returns an ssl socket' do
+ expect(socket).to be_a(Mongo::Pool::Socket::SSL)
+ end
+
+ it 'sets the family as ipv6' do
+ expect(socket.family).to eq(Socket::PF_INET6)
+ end
+ end
+
+ context 'when ssl options are not provided' do
+
+ let(:socket) do
+ resolver.socket(5)
+ end
+
+ it 'returns a tcp socket' do
+ expect(socket).to be_a(Mongo::Pool::Socket::TCP)
+ end
+
+ it 'sets the family a ipv6' do
+ expect(socket.family).to eq(Socket::PF_INET6)
+ end
+ end
+ end
end
View
17 spec/mongo/server/address/sock_spec.rb → spec/mongo/server/address/unix_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe Mongo::Server::Address::Sock do
+describe Mongo::Server::Address::Unix do
describe '#initialize' do
@@ -20,4 +20,19 @@
expect(resolver.host).to eq('/path/to/socket.sock')
end
end
+
+ describe '#socket' do
+
+ let(:resolver) do
+ described_class.new('/path/to/socket.sock')
+ end
+
+ let(:socket) do
+ resolver.socket(5)
+ end
+
+ it 'returns a unix socket' do
+ expect(socket).to be_a(Mongo::Pool::Socket::Unix)
+ end
+ end
end

0 comments on commit c200faf

Please sign in to comment.