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
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
# NOTE: This test expects a replica set of three nodes to be running on RS.host,
|
|
|
|
# on ports TEST_PORT, RS.ports[1], and TEST + 2.
|
2010-10-21 18:33:48 +00:00
|
|
|
class ConnectTest < Test::Unit::TestCase
|
|
|
|
include Mongo
|
|
|
|
|
2010-12-14 22:38:52 +00:00
|
|
|
def teardown
|
|
|
|
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
|
|
|
|
|
2010-12-15 19:16:05 +00:00
|
|
|
def test_connect_with_deprecated_multi
|
|
|
|
@conn = Connection.multi([[RS.host, RS.ports[0]], [RS.host, RS.ports[1]]], :name => RS.name)
|
|
|
|
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-08-25 15:27:58 +00:00
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
2010-12-14 18:14:45 +00:00
|
|
|
[RS.host, RS.ports[2]], :rs_name => RS.name + "-wrong")
|
2010-11-03 19:12:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-25 15:27:58 +00:00
|
|
|
# def test_connect_timeout
|
|
|
|
# passed = false
|
|
|
|
# timeout = 3
|
|
|
|
# begin
|
|
|
|
# t0 = Time.now
|
|
|
|
# @conn = ReplSetConnection.new(['192.169.169.1', 27017], :connect_timeout => timeout)
|
|
|
|
# rescue OperationTimeout
|
|
|
|
# passed = true
|
|
|
|
# t1 = Time.now
|
|
|
|
# end
|
|
|
|
|
|
|
|
# assert passed
|
|
|
|
# assert t1 - t0 < timeout + 1
|
|
|
|
# end
|
2011-06-15 18:20:11 +00:00
|
|
|
|
2010-10-21 18:33:48 +00:00
|
|
|
def test_connect
|
2011-04-12 19:01:40 +00:00
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
|
2010-12-14 18:14:45 +00:00
|
|
|
[RS.host, RS.ports[2]], :name => RS.name)
|
|
|
|
assert @conn.connected?
|
|
|
|
|
|
|
|
assert_equal RS.primary, @conn.primary
|
2010-12-14 20:47:18 +00:00
|
|
|
assert_equal RS.secondaries.sort, @conn.secondaries.sort
|
|
|
|
assert_equal RS.arbiters.sort, @conn.arbiters.sort
|
2011-04-13 18:39:52 +00:00
|
|
|
|
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
|
|
|
|
:name => RS.name)
|
|
|
|
assert @conn.connected?
|
2010-12-14 18:14:45 +00:00
|
|
|
end
|
|
|
|
|
2011-03-23 20:34:42 +00:00
|
|
|
def test_host_port_accessors
|
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
|
|
|
[RS.host, RS.ports[2]], :name => RS.name)
|
|
|
|
|
|
|
|
assert_equal @conn.host, RS.primary[0]
|
|
|
|
assert_equal @conn.port, RS.primary[1]
|
|
|
|
end
|
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
def test_connect_with_primary_node_killed
|
|
|
|
node = 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([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
|
|
|
[RS.host, RS.ports[2]])
|
|
|
|
end
|
|
|
|
|
|
|
|
# This allows the secondary to come up as a primary
|
|
|
|
rescue_connection_failure do
|
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
|
|
|
[RS.host, RS.ports[2]])
|
|
|
|
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
|
|
|
|
node = RS.kill_secondary
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2011-08-25 15:27:58 +00:00
|
|
|
rescue_connection_failure do
|
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
|
|
|
[RS.host, RS.ports[2]])
|
|
|
|
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
|
|
|
|
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
|
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
|
|
|
[RS.host, RS.ports[2]])
|
|
|
|
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
|
|
|
|
RS.step_down_primary
|
2010-10-21 18:33:48 +00:00
|
|
|
|
2010-12-14 18:14:45 +00:00
|
|
|
rescue_connection_failure do
|
|
|
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
|
|
|
[RS.host, RS.ports[2]])
|
|
|
|
end
|
2010-10-21 18:33:48 +00:00
|
|
|
assert @conn.connected?
|
|
|
|
end
|
2010-12-14 18:14:45 +00:00
|
|
|
|
2010-10-21 18:33:48 +00:00
|
|
|
end
|