mongo-ruby-driver/test/replica_sets/connect_test.rb

163 lines
4.8 KiB
Ruby
Raw Normal View History

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require './test/replica_sets/rs_test_helper'
class ConnectTest < Test::Unit::TestCase
def setup
ensure_rs
end
2010-12-14 22:38:52 +00:00
def teardown
@rs.restart_killed_nodes
@conn.close if defined?(@conn) && @conn
2010-12-14 22:38:52 +00:00
end
# TODO: test connect timeout.
def test_connect_with_deprecated_multi
2012-03-16 20:17:33 +00:00
silently do
@conn = Connection.multi([[@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]]], :name => @rs.name)
end
assert @conn.is_a?(ReplSetConnection)
assert @conn.connected?
end
def test_connect_bad_name
assert_raise_error(ReplicaSetConnectionError, "-wrong") do
@conn = ReplSetConnection.new(build_seeds(3), :name => @rs.name + "-wrong")
end
end
def test_connect_with_primary_node_killed
2012-04-04 17:45:19 +00:00
@rs.kill_primary
# 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
@conn = ReplSetConnection.new build_seeds(3)
end
# This allows the secondary to come up as a primary
rescue_connection_failure do
@conn = ReplSetConnection.new build_seeds(3)
end
end
def test_connect_with_secondary_node_killed
2012-04-04 17:45:19 +00:00
@rs.kill_secondary
rescue_connection_failure do
@conn = ReplSetConnection.new build_seeds(3)
end
assert @conn.connected?
end
def test_connect_with_third_node_killed
@rs.kill(@rs.get_node_from_port(@rs.ports[2]))
rescue_connection_failure do
@conn = ReplSetConnection.new build_seeds(3)
end
assert @conn.connected?
end
def test_connect_with_primary_stepped_down
@conn = ReplSetConnection.new build_seeds(3)
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?
rescue_connection_failure do
2011-12-05 21:16:06 +00:00
@conn[MONGO_TEST_DB]['bar'].find_one
end
end
def test_save_with_primary_stepped_down
@conn = ReplSetConnection.new build_seeds(3)
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
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-04-04 20:44:01 +00:00
silently do
@conn = Connection.from_uri("mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}")
end
2011-11-03 15:17:36 +00:00
assert @conn.is_a?(ReplSetConnection)
assert @conn.connected?
end
2012-05-18 16:17:21 +00:00
def test_connect_with_connection_string_in_env_var
begin
old_mongodb_uri = ENV['MONGODB_URI']
ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}"
silently do
@conn = ReplSetConnection.new
end
assert @conn.is_a?(ReplSetConnection)
assert @conn.connected?
ensure
ENV['MONGODB_URI'] = old_mongodb_uri
end
end
2012-02-16 15:59:09 +00:00
def test_connect_with_new_seed_format
@conn = ReplSetConnection.new build_seeds(3)
assert @conn.connected?
end
def test_connect_with_old_seed_format
2012-04-04 19:48:32 +00:00
silently do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[2]])
end
2012-02-16 15:59:09 +00:00
assert @conn.connected?
end
2011-11-03 15:17:36 +00:00
def test_connect_with_full_connection_string
2012-04-04 20:44:01 +00:00
silently do
@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")
end
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
2012-05-18 16:17:21 +00:00
def test_connect_with_full_connection_string_in_env_var
begin
old_mongodb_uri = ENV['MONGODB_URI']
ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name};safe=true;w=2;fsync=true;slaveok=true"
silently do
@conn = ReplSetConnection.new
end
assert @conn.is_a?(ReplSetConnection)
assert @conn.connected?
assert_equal 2, @conn.safe[:w]
assert @conn.safe[:fsync]
assert @conn.read_pool
ensure
ENV['MONGODB_URI'] = old_mongodb_uri
end
end
end