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 # Connection#arbiters. This is useful if your application needs to connect manually to nodes other
# than the primary. # than the primary.
# #
# @param [Array] args A list of host-port pairs to be used as seed nodes followed by a # @param [Array] seeds "host:port" strings
# hash containing any options. See the examples below for exactly how to use the constructor.
# #
# @option options [String] :rs_name (nil) The name of the replica set to connect to. You # @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. # 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. # between calls to check the replica set's state.
# @option opts [Boolean] :require_primary (true) If true, require a primary node for the connection # @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. # 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 # @example Connect to a replica set and provide two seed nodes.
# 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.
# ReplSetConnection.new(['localhost:30000', 'localhost:30001']) # 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': # @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 end
def test_repl_set_auth def test_repl_set_auth
@conn = ReplSetConnection.new([@manager.host, @manager.ports[0]], [@manager.host, @manager.ports[1]], @conn = ReplSetConnection.new(build_seeds(3), :name => @manager.name)
[@manager.host, @manager.ports[2]], :name => @manager.name)
# Add an admin user # Add an admin user
@conn['admin'].add_user("me", "secret") @conn['admin'].add_user("me", "secret")

View File

@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo') require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
require 'logger' 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'] $db = $con['foo']
class Load < Sinatra::Base class Load < Sinatra::Base

View File

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

View File

@ -21,8 +21,7 @@ class ConnectTest < Test::Unit::TestCase
def test_connect_bad_name def test_connect_bad_name
assert_raise_error(ReplicaSetConnectionError, "-wrong") do assert_raise_error(ReplicaSetConnectionError, "-wrong") do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new(build_seeds(3), :name => @rs.name + "-wrong")
[@rs.host, @rs.ports[2]], :name => @rs.name + "-wrong")
end end
end end
@ -32,14 +31,12 @@ class ConnectTest < Test::Unit::TestCase
# Becuase we're killing the primary and trying to connect right away, # Becuase we're killing the primary and trying to connect right away,
# this is going to fail right away. # this is going to fail right away.
assert_raise_error(ConnectionFailure, "Failed to connect to primary node") do assert_raise_error(ConnectionFailure, "Failed to connect to primary node") do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new build_seeds(3)
[@rs.host, @rs.ports[2]])
end end
# This allows the secondary to come up as a primary # This allows the secondary to come up as a primary
rescue_connection_failure do rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new build_seeds(3)
[@rs.host, @rs.ports[2]])
end end
end end
@ -47,8 +44,7 @@ class ConnectTest < Test::Unit::TestCase
node = @rs.kill_secondary node = @rs.kill_secondary
rescue_connection_failure do rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new build_seeds(3)
[@rs.host, @rs.ports[2]])
end end
assert @conn.connected? assert @conn.connected?
end end
@ -57,15 +53,13 @@ class ConnectTest < Test::Unit::TestCase
@rs.kill(@rs.get_node_from_port(@rs.ports[2])) @rs.kill(@rs.get_node_from_port(@rs.ports[2]))
rescue_connection_failure do rescue_connection_failure do
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new build_seeds(3)
[@rs.host, @rs.ports[2]])
end end
assert @conn.connected? assert @conn.connected?
end end
def test_connect_with_primary_stepped_down def test_connect_with_primary_stepped_down
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new build_seeds(3)
[@rs.host, @rs.ports[2]])
@conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}}) @conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
assert @conn[MONGO_TEST_DB]['bar'].find_one assert @conn[MONGO_TEST_DB]['bar'].find_one
@ -85,8 +79,7 @@ class ConnectTest < Test::Unit::TestCase
end end
def test_save_with_primary_stepped_down def test_save_with_primary_stepped_down
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], @conn = ReplSetConnection.new build_seeds(3)
[@rs.host, @rs.ports[2]])
primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port) primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
@ -110,7 +103,12 @@ class ConnectTest < Test::Unit::TestCase
end end
def test_connect_with_new_seed_format 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? assert @conn.connected?
end end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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