From 272d4835a7a4f25d30622e5ea8ed8e136da297ac Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 23 Mar 2011 16:34:42 -0400 Subject: [PATCH] RUBY-252 RUBY-237 re-add Connection#host and Connection#port --- lib/mongo/connection.rb | 14 ++++++++++++++ lib/mongo/repl_set_connection.rb | 14 ++++++++++++++ test/connection_test.rb | 5 +++++ test/replica_sets/connect_test.rb | 8 ++++++++ 4 files changed, 41 insertions(+) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 5615252..39d483f 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -157,6 +157,20 @@ module Mongo end end + # The host name used for this connection. + # + # @return [String] + def host + @primary_pool.host + end + + # The port used for this connection. + # + # @return [Integer] + def port + @primary_pool.port + end + # Fsync, then lock the mongod process against writes. Use this to get # the datafiles in a state safe for snapshotting, backing up, etc. # diff --git a/lib/mongo/repl_set_connection.rb b/lib/mongo/repl_set_connection.rb index fe1a40d..3f03fdd 100644 --- a/lib/mongo/repl_set_connection.rb +++ b/lib/mongo/repl_set_connection.rb @@ -135,6 +135,20 @@ module Mongo @nodes_to_try.length > 0 end + # The replica set primary's host name. + # + # @return [String] + def host + super + end + + # The replica set primary's port. + # + # @return [Integer] + def port + super + end + # Determine whether we're reading from a primary node. If false, # this connection connects to a secondary node and @read_secondaries is true. # diff --git a/test/connection_test.rb b/test/connection_test.rb index ec6f341..733b291 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -22,6 +22,11 @@ class TestConnection < Test::Unit::TestCase end end + def test_host_port_accessors + assert_equal @conn.host, TEST_HOST + assert_equal @conn.port, TEST_PORT + end + def test_server_info server_info = @conn.server_info assert server_info.keys.include?("version") diff --git a/test/replica_sets/connect_test.rb b/test/replica_sets/connect_test.rb index 691b556..678f07d 100644 --- a/test/replica_sets/connect_test.rb +++ b/test/replica_sets/connect_test.rb @@ -39,6 +39,14 @@ class ConnectTest < Test::Unit::TestCase assert_equal RS.arbiters.sort, @conn.arbiters.sort end + 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 + def test_connect_with_primary_node_killed node = RS.kill_primary