RUBY-378: fixed documentation and tests for new ReplSetConnection seed format

Added helper method #build_seeds to rs_test_helper
Added new test for old connection
This commit is contained in:
Tyler Brock 2012-02-18 18:29:52 -05:00
parent b70c9ce152
commit 8db4eb771f
14 changed files with 72 additions and 92 deletions

View File

@ -31,8 +31,7 @@ module Mongo
# Connection#arbiters. This is useful if your application needs to connect manually to nodes other
# than the primary.
#
# @param [Array] args A list of host-port pairs to be used as seed nodes followed by a
# hash containing any options. See the examples below for exactly how to use the constructor.
# @param [Array] seeds "host:port" strings
#
# @option options [String] :rs_name (nil) The name of the replica set to connect to. You
# can use this option to verify that you're connecting to the right replica set.
@ -62,10 +61,10 @@ module Mongo
# between calls to check the replica set's state.
# @option opts [Boolean] :require_primary (true) If true, require a primary node for the connection
# to succeed. Otherwise, connection will succeed as long as there's at least one secondary node.
# Note: that the number of seed nodes does not have to be equal to the number of replica set members.
# The purpose of seed nodes is to permit the driver to find at least one replica set member even if a member is down.
#
# @example Connect to a replica set and provide two seed nodes. Note that the number of seed nodes does
# not have to be equal to the number of replica set members. The purpose of seed nodes is to permit
# the driver to find at least one replica set member even if a member is down.
# @example Connect to a replica set and provide two seed nodes.
# ReplSetConnection.new(['localhost:30000', 'localhost:30001'])
#
# @example Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named 'prod':

View File

@ -15,8 +15,7 @@ class AuthTest < Test::Unit::TestCase
end
def test_repl_set_auth
@conn = ReplSetConnection.new([@manager.host, @manager.ports[0]], [@manager.host, @manager.ports[1]],
[@manager.host, @manager.ports[2]], :name => @manager.name)
@conn = ReplSetConnection.new(build_seeds(3), :name => @manager.name)
# Add an admin user
@conn['admin'].add_user("me", "secret")

View File

@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
require 'logger'
$con = Mongo::ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read => :secondary, :refresh_mode => :sync, :refresh_interval => 30)
$con = Mongo::ReplSetConnection.new(['localhost:30000', 'localhost:30001'], :read => :secondary, :refresh_mode => :sync, :refresh_interval => 30)
$db = $con['foo']
class Load < Sinatra::Base

View File

@ -13,32 +13,30 @@ class BasicTest < Test::Unit::TestCase
end
def test_connect
@conn = ReplSetConnection.new([@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[2]], :name => @rs.name)
@conn = ReplSetConnection.new(build_seeds(3), :name => @rs.name)
assert @conn.connected?
assert_equal @rs.primary, @conn.primary
assert_equal @rs.secondaries.sort, @conn.secondaries.sort
assert_equal @rs.arbiters.sort, @conn.arbiters.sort
@conn = ReplSetConnection.new([@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[0]],
@conn = ReplSetConnection.new(["#{@rs.host}:#{@rs.ports[1]}","#{@rs.host}:#{@rs.ports[0]}"],
:name => @rs.name)
assert @conn.connected?
end
def test_cache_original_seed_nodes
@conn = ReplSetConnection.new([@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[2]], [@rs.host, 19356], :name => @rs.name)
seeds = build_seeds(3) << "#{@rs.host}:19356"
@conn = ReplSetConnection.new(seeds, :name => @rs.name)
assert @conn.connected?
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!"
end
def test_accessors
seeds = [[@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]]]
args = seeds << {:name => @rs.name}
@conn = ReplSetConnection.new(*args)
seeds = build_seeds(3)
args = {:name => @rs.name}
@conn = ReplSetConnection.new(seeds, args)
@major_version = @rs.version.first
assert_equal @conn.host, @rs.primary[0]
@ -59,10 +57,9 @@ class BasicTest < Test::Unit::TestCase
context "Socket pools" do
context "checking out writers" do
setup do
seeds = [[@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]]]
args = seeds << {:name => @rs.name}
@con = ReplSetConnection.new(*args)
seeds = build_seeds(3)
args = {:name => @rs.name}
@con = ReplSetConnection.new(seeds, args)
@coll = @con[MONGO_TEST_DB]['test-connection-exceptions']
end

View File

@ -21,8 +21,7 @@ class ConnectTest < Test::Unit::TestCase
def test_connect_bad_name
assert_raise_error(ReplicaSetConnectionError, "-wrong") do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]], :name => @rs.name + "-wrong")
@conn = ReplSetConnection.new(build_seeds(3), :name => @rs.name + "-wrong")
end
end
@ -32,14 +31,12 @@ class ConnectTest < Test::Unit::TestCase
# 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]])
@conn = ReplSetConnection.new build_seeds(3)
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]])
@conn = ReplSetConnection.new build_seeds(3)
end
end
@ -47,8 +44,7 @@ class ConnectTest < Test::Unit::TestCase
node = @rs.kill_secondary
rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]])
@conn = ReplSetConnection.new build_seeds(3)
end
assert @conn.connected?
end
@ -57,15 +53,13 @@ class ConnectTest < Test::Unit::TestCase
@rs.kill(@rs.get_node_from_port(@rs.ports[2]))
rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]])
@conn = ReplSetConnection.new build_seeds(3)
end
assert @conn.connected?
end
def test_connect_with_primary_stepped_down
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]])
@conn = ReplSetConnection.new build_seeds(3)
@conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
assert @conn[MONGO_TEST_DB]['bar'].find_one
@ -85,8 +79,7 @@ class ConnectTest < Test::Unit::TestCase
end
def test_save_with_primary_stepped_down
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]])
@conn = ReplSetConnection.new build_seeds(3)
primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
@ -110,7 +103,12 @@ class ConnectTest < Test::Unit::TestCase
end
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]}"])
@conn = ReplSetConnection.new build_seeds(3)
assert @conn.connected?
end
def test_connect_with_old_seed_format
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[2]])
assert @conn.connected?
end

View File

@ -5,9 +5,7 @@ class ReplicaSetCountTest < Test::Unit::TestCase
def setup
ensure_rs
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[2]],
:read => :secondary)
@conn = ReplSetConnection.new(build_seeds(3), :read => :secondary)
assert @conn.primary_pool
@primary = Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
@db = @conn.db(MONGO_TEST_DB)

View File

@ -5,8 +5,7 @@ class ReplicaSetInsertTest < Test::Unit::TestCase
def setup
ensure_rs
@conn = ReplSetConnection.new([TEST_HOST, @rs.ports[0]],
[TEST_HOST, @rs.ports[1]], [TEST_HOST, @rs.ports[2]])
@conn = ReplSetConnection.new build_seeds(3)
@db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets")
@coll = @db.collection("test-sets")

View File

@ -7,8 +7,7 @@ class ReplicaSetPooledInsertTest < Test::Unit::TestCase
def setup
ensure_rs
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]], :pool_size => 5, :timeout => 5, :refresh_mode => false)
@conn = ReplSetConnection.new(build_seeds(3), :pool_size => 5, :timeout => 5, :refresh_mode => false)
@db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets")
@coll = @db.collection("test-sets")

View File

@ -5,7 +5,7 @@ class ReplicaSetQueryTest < Test::Unit::TestCase
def setup
ensure_rs
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]])
@conn = ReplSetConnection.new build_seeds(1)
@db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets")
@coll = @db.collection("test-sets")

View File

@ -7,10 +7,15 @@ class ReadPreferenceTest < Test::Unit::TestCase
def setup
ensure_rs
log = Logger.new("test.log")
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
:read => :secondary, :pool_size => 50,
:refresh_mode => false, :refresh_interval => 5, :logger => log)
seeds = build_seeds(2)
args = {
:read => :secondary,
:pool_size => 50,
:refresh_mode => false,
:refresh_interval => 5,
:logger => log
}
@conn = ReplSetConnection.new(seeds, args)
@db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets")
end
@ -37,8 +42,7 @@ class ReadPreferenceTest < Test::Unit::TestCase
@rs.add_arbiter
@rs.remove_secondary_node
@conn = ReplSetConnection.new(["#{@rs.host}:#{@rs.ports[0]}","#{@rs.host}:#{@rs.ports[1]}"],
:read => :secondary_only)
@conn = ReplSetConnection.new(build_seeds(2), :read => :secondary_only)
@db = @conn.db(MONGO_TEST_DB)
@coll = @db.collection("test-sets")

View File

@ -17,17 +17,11 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
Benchmark.bm do |x|
x.report("Connect") do
10.times do
ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_mode => false)
ReplSetConnection.new(build_seeds(3), :refresh_mode => false)
end
end
@con = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_mode => false)
@con = ReplSetConnection.new(build_seeds(3), :refresh_mode => false)
x.report("manager") do
man = Mongo::PoolManager.new(@con, @con.seeds)
@ -43,10 +37,7 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
sleep(4)
rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_mode => false)
@conn = ReplSetConnection.new(build_seeds(3), :refresh_mode => false)
end
assert_equal [], @conn.secondaries
@ -75,11 +66,8 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
sleep(4)
rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_interval => 2,
:refresh_mode => :sync)
@conn = ReplSetConnection.new(build_seeds(3),
:refresh_interval => 2, :refresh_mode => :sync)
end
assert_equal [], @conn.secondaries
@ -101,11 +89,8 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
end
def test_automated_refresh_when_secondary_goes_down
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_interval => 2,
:refresh_mode => :sync)
@conn = ReplSetConnection.new(build_seeds(3),
:refresh_interval => 2, :refresh_mode => :sync)
num_secondaries = @conn.secondary_pools.length
old_refresh_version = @conn.refresh_version
@ -123,11 +108,8 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
end
def test_automated_refresh_with_removed_node
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_interval => 2,
:refresh_mode => :sync)
@conn = ReplSetConnection.new(build_seeds(3),
:refresh_interval => 2, :refresh_mode => :sync)
num_secondaries = @conn.secondary_pools.length
old_refresh_version = @conn.refresh_version
@ -145,19 +127,15 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
end
def test_adding_and_removing_nodes
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_interval => 2, :refresh_mode => :sync)
@conn = ReplSetConnection.new(build_seeds(3),
:refresh_interval => 2, :refresh_mode => :sync)
@rs.add_node
sleep(4)
@conn['foo']['bar'].find_one
@conn2 = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_interval => 2, :refresh_mode => :sync)
@conn2 = ReplSetConnection.new(build_seeds(3),
:refresh_interval => 2, :refresh_mode => :sync)
assert @conn2.secondaries.sort == @conn.secondaries.sort,
"Second connection secondaries not equal to first."

View File

@ -15,12 +15,13 @@ class ReplicaSetRefreshWithThreadsTest < Test::Unit::TestCase
end
def test_read_write_load_with_added_nodes
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]],
[@rs.host, @rs.ports[1]],
[@rs.host, @rs.ports[2]],
:refresh_interval => 5,
:refresh_mode => :sync,
:read => :secondary)
seeds = build_seeds(3)
args = {
:refresh_interval => 5,
:refresh_mode => :sync,
:read => :secondary
}
@conn = ReplSetConnection.new(seeds, args)
@duplicate = @conn[MONGO_TEST_DB]['duplicate']
@unique = @conn[MONGO_TEST_DB]['unique']
@duplicate.insert("test" => "insert")

View File

@ -5,7 +5,7 @@ class ReplicaSetAckTest < Test::Unit::TestCase
def setup
ensure_rs
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]])
@conn = ReplSetConnection.new(build_seeds(1))
@slave1 = Connection.new(@conn.secondary_pools[0].host,
@conn.secondary_pools[0].port, :slave_ok => true)

View File

@ -28,4 +28,12 @@ class Test::Unit::TestCase
retry
end
end
def build_seeds(num_hosts)
seeds = []
num_hosts.times do |n|
seeds << "#{@rs.host}:#{@rs.ports[n]}"
end
seeds
end
end