2011-08-29 21:49:58 +00:00
|
|
|
require './test/test_helper'
|
|
|
|
|
|
|
|
class ReadTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
context "Read mode on standard connection: " do
|
|
|
|
setup do
|
|
|
|
@read_preference = :secondary
|
|
|
|
@con = Mongo::Connection.new('localhost', 27017, :read => @read_preference, :connect => false)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-10-17 18:41:09 +00:00
|
|
|
context "Read mode on replica set connection: " do
|
2011-08-29 21:49:58 +00:00
|
|
|
setup do
|
|
|
|
@read_preference = :secondary
|
2012-02-16 19:20:31 +00:00
|
|
|
@con = Mongo::ReplSetConnection.new(['localhost:27017'], :read => @read_preference, :connect => false)
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
should "store read preference on Connection" do
|
|
|
|
assert_equal @read_preference, @con.read_preference
|
|
|
|
end
|
|
|
|
|
|
|
|
should "propogate to DB" do
|
|
|
|
db = @con['foo']
|
|
|
|
assert_equal @read_preference, db.read_preference
|
|
|
|
|
|
|
|
db = @con.db('foo')
|
|
|
|
assert_equal @read_preference, db.read_preference
|
|
|
|
|
|
|
|
db = DB.new('foo', @con)
|
|
|
|
assert_equal @read_preference, db.read_preference
|
|
|
|
end
|
|
|
|
|
|
|
|
should "allow db override" do
|
|
|
|
db = DB.new('foo', @con, :read => :primary)
|
|
|
|
assert_equal :primary, db.read_preference
|
|
|
|
|
|
|
|
db = @con.db('foo', :read => :primary)
|
|
|
|
assert_equal :primary, db.read_preference
|
|
|
|
end
|
|
|
|
|
|
|
|
context "on DB: " do
|
|
|
|
setup do
|
|
|
|
@db = @con['foo']
|
|
|
|
end
|
|
|
|
|
|
|
|
should "propogate to collection" do
|
|
|
|
col = @db.collection('bar')
|
|
|
|
assert_equal @read_preference, col.read_preference
|
|
|
|
|
|
|
|
col = @db['bar']
|
|
|
|
assert_equal @read_preference, col.read_preference
|
|
|
|
|
|
|
|
col = Collection.new('bar', @db)
|
|
|
|
assert_equal @read_preference, col.read_preference
|
|
|
|
end
|
|
|
|
|
|
|
|
should "allow override on collection" do
|
|
|
|
col = @db.collection('bar', :read => :primary)
|
|
|
|
assert_equal :primary, col.read_preference
|
|
|
|
|
|
|
|
col = Collection.new('bar', @db, :read => :primary)
|
|
|
|
assert_equal :primary, col.read_preference
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "on read mode ops" do
|
|
|
|
setup do
|
|
|
|
@col = @con['foo']['bar']
|
2012-03-07 03:56:39 +00:00
|
|
|
@mock_socket = new_mock_socket
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
should "use default value on query" do
|
2011-10-17 18:41:09 +00:00
|
|
|
@cursor = @col.find({:a => 1})
|
2012-03-07 03:56:39 +00:00
|
|
|
sock = new_mock_socket
|
2011-11-18 20:47:06 +00:00
|
|
|
read_pool = stub(:checkin => true)
|
2011-11-04 19:12:58 +00:00
|
|
|
@con.stubs(:read_pool).returns(read_pool)
|
2011-11-18 20:47:06 +00:00
|
|
|
primary_pool = stub(:checkin => true)
|
2012-03-07 17:38:04 +00:00
|
|
|
sock.stubs(:pool).returns(primary_pool)
|
2011-11-04 19:12:58 +00:00
|
|
|
@con.stubs(:primary_pool).returns(primary_pool)
|
2011-11-18 20:47:06 +00:00
|
|
|
@con.expects(:checkout_reader).returns(sock)
|
2011-08-29 21:49:58 +00:00
|
|
|
@con.expects(:receive_message).with do |o, m, l, s, c, r|
|
2011-10-17 18:41:09 +00:00
|
|
|
r == nil
|
2011-08-29 21:49:58 +00:00
|
|
|
end.returns([[], 0, 0])
|
|
|
|
|
2011-10-17 18:41:09 +00:00
|
|
|
@cursor.next
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
should "allow override default value on query" do
|
2011-10-17 18:41:09 +00:00
|
|
|
@cursor = @col.find({:a => 1}, :read => :primary)
|
2012-03-07 03:56:39 +00:00
|
|
|
sock = new_mock_socket
|
2011-11-18 20:47:06 +00:00
|
|
|
primary_pool = stub(:checkin => true)
|
2012-03-07 17:38:04 +00:00
|
|
|
sock.stubs(:pool).returns(primary_pool)
|
2011-11-04 19:12:58 +00:00
|
|
|
@con.stubs(:primary_pool).returns(primary_pool)
|
2011-11-18 20:47:06 +00:00
|
|
|
@con.expects(:checkout_writer).returns(sock)
|
2011-08-29 21:49:58 +00:00
|
|
|
@con.expects(:receive_message).with do |o, m, l, s, c, r|
|
2011-10-17 18:41:09 +00:00
|
|
|
r == nil
|
2011-08-29 21:49:58 +00:00
|
|
|
end.returns([[], 0, 0])
|
|
|
|
|
2011-10-17 18:41:09 +00:00
|
|
|
@cursor.next
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
should "allow override alternate value on query" do
|
2011-09-06 18:58:03 +00:00
|
|
|
# TODO: enable this test once we enable reading from tags.
|
|
|
|
# @con.expects(:receive_message).with do |o, m, l, s, c, r|
|
|
|
|
# tags = {:dc => "ny"}
|
|
|
|
# end.returns([[], 0, 0])
|
|
|
|
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
@col.find_one({:a => 1}, :read => {:dc => "ny"})
|
|
|
|
end
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|