2010-10-21 18:33:48 +00:00
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2010-12-14 18:14:45 +00:00
|
|
|
require './test/replica_sets/rs_test_helper'
|
2010-10-21 18:33:48 +00:00
|
|
|
|
|
|
|
class ConnectTest < Test::Unit::TestCase
|
2011-11-03 15:17:36 +00:00
|
|
|
include ReplicaSetTest
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2010-12-14 22:38:52 +00:00
|
|
|
def teardown
|
2011-11-03 15:17:36 +00:00
|
|
|
self.rs.restart_killed_nodes
|
2011-08-25 15:27:58 +00:00
|
|
|
@conn.close if defined?(@conn) && @conn
|
2010-12-14 22:38:52 +00:00
|
|
|
end
|
|
|
|
|
2011-10-07 17:55:09 +00:00
|
|
|
# TODO: test connect timeout.
|
|
|
|
|
2010-12-15 19:16:05 +00:00
|
|
|
def test_connect_with_deprecated_multi
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = Connection.multi([[self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]]], :name => self.rs.name)
|
2010-12-15 19:16:05 +00:00
|
|
|
assert @conn.is_a?(ReplSetConnection)
|
2010-12-30 20:40:50 +00:00
|
|
|
assert @conn.connected?
|
2010-12-15 19:16:05 +00:00
|
|
|
end
|
|
|
|
|
2010-11-03 19:12:15 +00:00
|
|
|
def test_connect_bad_name
|
2010-12-14 18:14:45 +00:00
|
|
|
assert_raise_error(ReplicaSetConnectionError, "-wrong") do
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]],
|
|
|
|
[self.rs.host, self.rs.ports[2]], :name => self.rs.name + "-wrong")
|
2010-11-03 19:12:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
def test_connect_with_primary_node_killed
|
2011-11-03 15:17:36 +00:00
|
|
|
node = self.rs.kill_primary
|
2010-12-14 18:14:45 +00:00
|
|
|
|
|
|
|
# Becuase we're killing the primary and trying to connect right away,
|
|
|
|
# this is going to fail right away.
|
|
|
|
assert_raise_error(ConnectionFailure, "Failed to connect to primary node") do
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]],
|
|
|
|
[self.rs.host, self.rs.ports[2]])
|
2010-12-14 18:14:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# This allows the secondary to come up as a primary
|
|
|
|
rescue_connection_failure do
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]],
|
|
|
|
[self.rs.host, self.rs.ports[2]])
|
2010-12-14 18:14:45 +00:00
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
end
|
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
def test_connect_with_secondary_node_killed
|
2011-11-03 15:17:36 +00:00
|
|
|
node = self.rs.kill_secondary
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2011-08-25 15:27:58 +00:00
|
|
|
rescue_connection_failure do
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]],
|
|
|
|
[self.rs.host, self.rs.ports[2]])
|
2011-08-25 15:27:58 +00:00
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
assert @conn.connected?
|
|
|
|
end
|
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
def test_connect_with_third_node_killed
|
2011-11-03 15:17:36 +00:00
|
|
|
self.rs.kill(self.rs.get_node_from_port(self.rs.ports[2]))
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2011-08-25 15:27:58 +00:00
|
|
|
rescue_connection_failure do
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]],
|
|
|
|
[self.rs.host, self.rs.ports[2]])
|
2011-08-25 15:27:58 +00:00
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
assert @conn.connected?
|
|
|
|
end
|
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
def test_connect_with_primary_stepped_down
|
2011-11-03 15:17:36 +00:00
|
|
|
self.rs.step_down_primary
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
rescue_connection_failure do
|
2011-11-03 15:17:36 +00:00
|
|
|
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]],
|
|
|
|
[self.rs.host, self.rs.ports[2]])
|
2010-12-14 18:14:45 +00:00
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
assert @conn.connected?
|
|
|
|
end
|
2010-12-14 18:14:45 +00:00
|
|
|
|
2011-11-03 15:17:36 +00:00
|
|
|
def test_connect_with_connection_string
|
|
|
|
@conn = Connection.from_uri("mongodb://#{self.rs.host}:#{self.rs.ports[0]},#{self.rs.host}:#{self.rs.ports[1]}?replicaset=#{self.rs.name}")
|
|
|
|
assert @conn.is_a?(ReplSetConnection)
|
|
|
|
assert @conn.connected?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connect_with_full_connection_string
|
|
|
|
@conn = Connection.from_uri("mongodb://#{self.rs.host}:#{self.rs.ports[0]},#{self.rs.host}:#{self.rs.ports[1]}?replicaset=#{self.rs.name};safe=true;w=2;fsync=true;slaveok=true")
|
|
|
|
assert @conn.is_a?(ReplSetConnection)
|
|
|
|
assert @conn.connected?
|
|
|
|
assert_equal 2, @conn.safe[:w]
|
|
|
|
assert @conn.safe[:fsync]
|
|
|
|
assert @conn.read_pool
|
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
end
|