2011-10-07 17:55:09 +00:00
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
|
|
require './test/replica_sets/rs_test_helper'
|
|
|
|
|
2011-11-03 15:17:36 +00:00
|
|
|
class BasicTest < Test::Unit::TestCase
|
2012-01-30 00:05:17 +00:00
|
|
|
|
|
|
|
def setup
|
|
|
|
ensure_rs
|
|
|
|
end
|
2011-10-07 17:55:09 +00:00
|
|
|
|
|
|
|
def teardown
|
2012-01-30 00:05:17 +00:00
|
|
|
@rs.restart_killed_nodes
|
2011-10-07 17:55:09 +00:00
|
|
|
@conn.close if defined?(@conn) && @conn
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connect
|
2012-02-18 23:29:52 +00:00
|
|
|
@conn = ReplSetConnection.new(build_seeds(3), :name => @rs.name)
|
2011-10-07 17:55:09 +00:00
|
|
|
assert @conn.connected?
|
|
|
|
|
2012-01-30 00:05:17 +00:00
|
|
|
assert_equal @rs.primary, @conn.primary
|
|
|
|
assert_equal @rs.secondaries.sort, @conn.secondaries.sort
|
|
|
|
assert_equal @rs.arbiters.sort, @conn.arbiters.sort
|
2011-10-07 17:55:09 +00:00
|
|
|
|
2012-02-18 23:29:52 +00:00
|
|
|
@conn = ReplSetConnection.new(["#{@rs.host}:#{@rs.ports[1]}","#{@rs.host}:#{@rs.ports[0]}"],
|
2012-01-30 00:05:17 +00:00
|
|
|
:name => @rs.name)
|
2011-10-07 17:55:09 +00:00
|
|
|
assert @conn.connected?
|
|
|
|
end
|
|
|
|
|
2011-12-02 22:20:04 +00:00
|
|
|
def test_cache_original_seed_nodes
|
2012-02-18 23:29:52 +00:00
|
|
|
seeds = build_seeds(3) << "#{@rs.host}:19356"
|
|
|
|
@conn = ReplSetConnection.new(seeds, :name => @rs.name)
|
2011-12-02 22:20:04 +00:00
|
|
|
assert @conn.connected?
|
2012-01-30 00:05:17 +00:00
|
|
|
assert @conn.seeds.include?([@rs.host, 19356]), "Original seed nodes not cached!"
|
|
|
|
assert_equal [@rs.host, 19356], @conn.seeds.last, "Original seed nodes not cached!"
|
2011-12-02 22:20:04 +00:00
|
|
|
end
|
|
|
|
|
2011-10-07 17:55:09 +00:00
|
|
|
def test_accessors
|
2012-02-18 23:29:52 +00:00
|
|
|
seeds = build_seeds(3)
|
|
|
|
args = {:name => @rs.name}
|
|
|
|
@conn = ReplSetConnection.new(seeds, args)
|
2012-02-13 23:34:38 +00:00
|
|
|
@major_version = @rs.version.first
|
2011-10-07 17:55:09 +00:00
|
|
|
|
2012-01-30 00:05:17 +00:00
|
|
|
assert_equal @conn.host, @rs.primary[0]
|
|
|
|
assert_equal @conn.port, @rs.primary[1]
|
2011-10-07 17:55:09 +00:00
|
|
|
assert_equal @conn.host, @conn.primary_pool.host
|
|
|
|
assert_equal @conn.port, @conn.primary_pool.port
|
2011-11-03 15:17:36 +00:00
|
|
|
assert_equal @conn.nodes.sort, @conn.seeds.sort
|
2011-10-07 17:55:09 +00:00
|
|
|
assert_equal 2, @conn.secondaries.length
|
2011-11-03 15:17:36 +00:00
|
|
|
assert_equal 0, @conn.arbiters.length
|
2011-10-07 17:55:09 +00:00
|
|
|
assert_equal 2, @conn.secondary_pools.length
|
2012-01-30 00:05:17 +00:00
|
|
|
assert_equal @rs.name, @conn.replica_set_name
|
2011-10-07 17:55:09 +00:00
|
|
|
assert @conn.secondary_pools.include?(@conn.read_pool)
|
|
|
|
assert_equal 90, @conn.refresh_interval
|
2011-11-03 15:17:36 +00:00
|
|
|
assert_equal @conn.refresh_mode, false
|
2012-02-13 23:34:38 +00:00
|
|
|
assert_equal 5, @conn.tag_map.keys.length unless @major_version < 2
|
2011-10-07 17:55:09 +00:00
|
|
|
end
|
2011-12-02 20:37:05 +00:00
|
|
|
|
|
|
|
context "Socket pools" do
|
|
|
|
context "checking out writers" do
|
|
|
|
setup do
|
2012-02-18 23:29:52 +00:00
|
|
|
seeds = build_seeds(3)
|
|
|
|
args = {:name => @rs.name}
|
|
|
|
@con = ReplSetConnection.new(seeds, args)
|
2011-12-02 20:37:05 +00:00
|
|
|
@coll = @con[MONGO_TEST_DB]['test-connection-exceptions']
|
|
|
|
end
|
|
|
|
|
|
|
|
should "close the connection on send_message for major exceptions" do
|
|
|
|
@con.expects(:checkout_writer).raises(SystemStackError)
|
|
|
|
@con.expects(:close)
|
|
|
|
begin
|
|
|
|
@coll.insert({:foo => "bar"})
|
|
|
|
rescue SystemStackError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "close the connection on send_message_with_safe_check for major exceptions" do
|
|
|
|
@con.expects(:checkout_writer).raises(SystemStackError)
|
|
|
|
@con.expects(:close)
|
|
|
|
begin
|
|
|
|
@coll.insert({:foo => "bar"}, :safe => true)
|
|
|
|
rescue SystemStackError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "close the connection on receive_message for major exceptions" do
|
|
|
|
@con.expects(:checkout_writer).raises(SystemStackError)
|
|
|
|
@con.expects(:close)
|
|
|
|
begin
|
|
|
|
@coll.find.next
|
|
|
|
rescue SystemStackError
|
|
|
|
end
|
|
|
|
end
|
2012-02-19 00:24:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "checking out readers" do
|
|
|
|
setup do
|
|
|
|
seeds = build_seeds(3)
|
|
|
|
args = {:name => @rs.name}
|
|
|
|
@con = ReplSetConnection.new(seeds, args)
|
|
|
|
@coll = @con[MONGO_TEST_DB]['test-connection-exceptions']
|
|
|
|
end
|
|
|
|
|
2011-12-02 20:37:05 +00:00
|
|
|
should "close the connection on receive_message for major exceptions" do
|
|
|
|
@con.expects(:checkout_reader).raises(SystemStackError)
|
|
|
|
@con.expects(:close)
|
|
|
|
begin
|
|
|
|
@coll.find({}, :read => :secondary).next
|
|
|
|
rescue SystemStackError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-07 17:55:09 +00:00
|
|
|
end
|