RUBY-406 added ReplSetConnection read preference :secondary_only

This commit is contained in:
Tyler Brock 2012-02-16 12:59:52 -05:00
parent 78fc1debbe
commit 542b8f46da
4 changed files with 18 additions and 8 deletions

View File

@ -224,7 +224,6 @@ module Mongo
@manager = background_manager
old_manager.close(:soft => true)
@refresh_version += 1
return true
end
@ -324,11 +323,9 @@ module Mongo
else
connect
end
begin
socket = get_socket_from_pool(self.read_pool)
if !socket
if !socket && @read != :secondary_only
connect
socket = get_socket_from_pool(self.primary_pool)
end

View File

@ -232,7 +232,7 @@ module Mongo
# If more than one node is available, use the ping
# time to figure out which nodes to choose from.
def set_read_pool
if @secondary_pools.empty?
if @secondary_pools.empty? && @connection.read_preference != :secondary_only
@read_pool = @primary_pool
elsif @secondary_pools.size == 1
@read_pool = @secondary_pools[0]

View File

@ -59,11 +59,11 @@ module Mongo
end
def validate_read_preference(value)
if [:primary, :secondary, nil].include?(value)
if [:primary, :secondary, :secondary_only, nil].include?(value)
return true
else
raise MongoArgumentError, "#{value} is not a valid read preference. " +
"Please specify either :primary or :secondary."
"Please specify either :primary or :secondary or :secondary_only."
end
end

View File

@ -13,7 +13,6 @@ class ReadPreferenceTest < Test::Unit::TestCase
:refresh_mode => false, :refresh_interval => 5, :logger => log)
@db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets")
col = @db['mongo-test']
end
def teardown
@ -34,6 +33,20 @@ class ReadPreferenceTest < Test::Unit::TestCase
"Primary port and read port at the same!"
end
def test_read_secondary_only
@conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], :read => :secondary_only)
assert_equal @conn.read_preference, :secondary_only
@db = @conn.db(MONGO_TEST_DB)
@coll = @db.collection("test-sets")
@coll.save({:a => 20})
@rs.kill_all_secondaries
assert_raise ConnectionFailure do
@coll.find_one
end
end
def test_query_secondaries
@secondary = Connection.new(@rs.host, @conn.read_pool.port, :slave_ok => true)
@coll = @db.collection("test-sets", :safe => {:w => 3, :wtimeout => 20000})