From 8db4eb771ff0bf1be955b72723fbf30133fcebdd Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Sat, 18 Feb 2012 18:29:52 -0500 Subject: [PATCH] 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 --- lib/mongo/repl_set_connection.rb | 9 ++-- test/auxillary/repl_set_auth_test.rb | 3 +- test/load/thin/load.rb | 2 +- test/replica_sets/basic_test.rb | 23 ++++----- test/replica_sets/connect_test.rb | 28 +++++------ test/replica_sets/count_test.rb | 4 +- test/replica_sets/insert_test.rb | 3 +- test/replica_sets/pooled_insert_test.rb | 3 +- test/replica_sets/query_test.rb | 2 +- test/replica_sets/read_preference_test.rb | 16 ++++--- test/replica_sets/refresh_test.rb | 48 +++++-------------- .../replica_sets/refresh_with_threads_test.rb | 13 ++--- test/replica_sets/replication_ack_test.rb | 2 +- test/replica_sets/rs_test_helper.rb | 8 ++++ 14 files changed, 72 insertions(+), 92 deletions(-) diff --git a/lib/mongo/repl_set_connection.rb b/lib/mongo/repl_set_connection.rb index 5591024..d8d45f1 100644 --- a/lib/mongo/repl_set_connection.rb +++ b/lib/mongo/repl_set_connection.rb @@ -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': diff --git a/test/auxillary/repl_set_auth_test.rb b/test/auxillary/repl_set_auth_test.rb index 03af340..afa3984 100644 --- a/test/auxillary/repl_set_auth_test.rb +++ b/test/auxillary/repl_set_auth_test.rb @@ -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") diff --git a/test/load/thin/load.rb b/test/load/thin/load.rb index 9d981f6..f97ee83 100644 --- a/test/load/thin/load.rb +++ b/test/load/thin/load.rb @@ -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 diff --git a/test/replica_sets/basic_test.rb b/test/replica_sets/basic_test.rb index 1e9e909..10d63ac 100644 --- a/test/replica_sets/basic_test.rb +++ b/test/replica_sets/basic_test.rb @@ -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 diff --git a/test/replica_sets/connect_test.rb b/test/replica_sets/connect_test.rb index cf41aa2..efdf6c4 100644 --- a/test/replica_sets/connect_test.rb +++ b/test/replica_sets/connect_test.rb @@ -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 diff --git a/test/replica_sets/count_test.rb b/test/replica_sets/count_test.rb index cf810b9..868e197 100644 --- a/test/replica_sets/count_test.rb +++ b/test/replica_sets/count_test.rb @@ -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) diff --git a/test/replica_sets/insert_test.rb b/test/replica_sets/insert_test.rb index 38926ff..3d977e9 100644 --- a/test/replica_sets/insert_test.rb +++ b/test/replica_sets/insert_test.rb @@ -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") diff --git a/test/replica_sets/pooled_insert_test.rb b/test/replica_sets/pooled_insert_test.rb index 58a8272..85dc2c5 100644 --- a/test/replica_sets/pooled_insert_test.rb +++ b/test/replica_sets/pooled_insert_test.rb @@ -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") diff --git a/test/replica_sets/query_test.rb b/test/replica_sets/query_test.rb index 3e3988b..22953ad 100644 --- a/test/replica_sets/query_test.rb +++ b/test/replica_sets/query_test.rb @@ -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") diff --git a/test/replica_sets/read_preference_test.rb b/test/replica_sets/read_preference_test.rb index bff4874..34026ec 100644 --- a/test/replica_sets/read_preference_test.rb +++ b/test/replica_sets/read_preference_test.rb @@ -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") diff --git a/test/replica_sets/refresh_test.rb b/test/replica_sets/refresh_test.rb index 0359387..574b7ad 100644 --- a/test/replica_sets/refresh_test.rb +++ b/test/replica_sets/refresh_test.rb @@ -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." diff --git a/test/replica_sets/refresh_with_threads_test.rb b/test/replica_sets/refresh_with_threads_test.rb index 7c106dc..458bb06 100644 --- a/test/replica_sets/refresh_with_threads_test.rb +++ b/test/replica_sets/refresh_with_threads_test.rb @@ -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") diff --git a/test/replica_sets/replication_ack_test.rb b/test/replica_sets/replication_ack_test.rb index 0feca2c..9cd367c 100644 --- a/test/replica_sets/replication_ack_test.rb +++ b/test/replica_sets/replication_ack_test.rb @@ -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) diff --git a/test/replica_sets/rs_test_helper.rb b/test/replica_sets/rs_test_helper.rb index ad390af..219c47c 100644 --- a/test/replica_sets/rs_test_helper.rb +++ b/test/replica_sets/rs_test_helper.rb @@ -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