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
|
2012-01-30 00:05:17 +00:00
|
|
|
def setup
|
|
|
|
ensure_rs
|
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2010-12-14 22:38:52 +00:00
|
|
|
def teardown
|
2012-01-30 00:05:17 +00:00
|
|
|
@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
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = Connection.multi([[@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]]], :name => @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
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @rs.ports[2]], :name => @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
|
2012-01-30 00:05:17 +00:00
|
|
|
node = @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
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @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
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @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
|
2012-01-30 00:05:17 +00:00
|
|
|
node = @rs.kill_secondary
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2011-08-25 15:27:58 +00:00
|
|
|
rescue_connection_failure do
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @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
|
2012-01-30 00:05:17 +00:00
|
|
|
@rs.kill(@rs.get_node_from_port(@rs.ports[2]))
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2011-08-25 15:27:58 +00:00
|
|
|
rescue_connection_failure do
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @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
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @rs.ports[2]])
|
2011-12-05 21:16:06 +00:00
|
|
|
@conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
|
|
|
|
assert @conn[MONGO_TEST_DB]['bar'].find_one
|
|
|
|
|
|
|
|
primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
|
2012-01-26 17:07:32 +00:00
|
|
|
assert_raise Mongo::ConnectionFailure do
|
|
|
|
primary['admin'].command({:replSetStepDown => 60})
|
|
|
|
end
|
2011-12-05 21:16:06 +00:00
|
|
|
assert @conn.connected?
|
2012-01-26 17:07:32 +00:00
|
|
|
assert_raise Mongo::ConnectionFailure do
|
2011-12-05 21:16:06 +00:00
|
|
|
@conn[MONGO_TEST_DB]['bar'].find_one
|
|
|
|
end
|
|
|
|
assert !@conn.connected?
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
rescue_connection_failure do
|
2011-12-05 21:16:06 +00:00
|
|
|
@conn[MONGO_TEST_DB]['bar'].find_one
|
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
|
|
|
|
2011-12-16 15:44:21 +00:00
|
|
|
def test_save_with_primary_stepped_down
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
|
|
|
|
[@rs.host, @rs.ports[2]])
|
2011-12-16 15:44:21 +00:00
|
|
|
|
|
|
|
primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
|
|
|
|
|
|
|
|
# Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors
|
|
|
|
step_down_command = BSON::OrderedHash.new
|
|
|
|
step_down_command[:replSetStepDown] = 60
|
|
|
|
step_down_command[:force] = true
|
2012-01-26 17:07:32 +00:00
|
|
|
assert_raise Mongo::ConnectionFailure do
|
|
|
|
primary['admin'].command(step_down_command)
|
|
|
|
end
|
2011-12-16 15:44:21 +00:00
|
|
|
|
|
|
|
rescue_connection_failure do
|
|
|
|
@conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-03 15:17:36 +00:00
|
|
|
def test_connect_with_connection_string
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = Connection.from_uri("mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}")
|
2011-11-03 15:17:36 +00:00
|
|
|
assert @conn.is_a?(ReplSetConnection)
|
|
|
|
assert @conn.connected?
|
|
|
|
end
|
2012-02-16 15:59:09 +00:00
|
|
|
|
|
|
|
def test_connect_with_new_seed_format
|
|
|
|
@conn = ReplSetConnection.new(["#{@rs.host}:#{@rs.ports[0]}','#{@rs.host}:#{@rs.ports[1]}','#{@rs.host}:#{@rs.ports[2]}"])
|
|
|
|
assert @conn.connected?
|
|
|
|
end
|
2011-11-03 15:17:36 +00:00
|
|
|
|
|
|
|
def test_connect_with_full_connection_string
|
2012-01-30 00:05:17 +00:00
|
|
|
@conn = Connection.from_uri("mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name};safe=true;w=2;fsync=true;slaveok=true")
|
2011-11-03 15:17:36 +00:00
|
|
|
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
|