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

98 lines
3.4 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
2011-11-03 15:17:36 +00:00
include ReplicaSetTest
2010-12-14 22:38:52 +00:00
def teardown
2011-11-03 15:17:36 +00:00
self.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
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)
assert @conn.is_a?(ReplSetConnection)
assert @conn.connected?
end
def test_connect_bad_name
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")
end
end
def test_connect_with_primary_node_killed
2011-11-03 15:17:36 +00:00
node = self.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
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]])
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]])
end
end
def test_connect_with_secondary_node_killed
2011-11-03 15:17:36 +00:00
node = self.rs.kill_secondary
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]])
end
assert @conn.connected?
end
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]))
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]])
end
assert @conn.connected?
end
def test_connect_with_primary_stepped_down
2011-12-05 21:16:06 +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]])
@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)
primary['admin'].command({:replSetStepDown => 60})
assert @conn.connected?
assert_raise_error Mongo::ConnectionFailure, "not master" do
@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
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
end