Improved replica set failover tests. A few

improved exception messages.
This commit is contained in:
Kyle Banker 2011-02-15 16:48:29 -05:00
parent 2335108162
commit ed44a7490c
5 changed files with 24 additions and 9 deletions

View File

@ -795,7 +795,7 @@ module Mongo
begin begin
message = new_binary_string message = new_binary_string
socket.read(length, message) socket.read(length, message)
raise ConnectionFailure, "connection closed" unless message.length > 0 raise ConnectionFailure, "connection closed" unless message && message.length > 0
if message.length < length if message.length < length
chunk = new_binary_string chunk = new_binary_string
while message.length < length while message.length < length

View File

@ -119,8 +119,10 @@ module Mongo
BSON::BSON_CODER.update_max_bson_size(self) BSON::BSON_CODER.update_max_bson_size(self)
else else
if @secondary_pools.empty? if @secondary_pools.empty?
close # close any existing pools and sockets
raise ConnectionFailure, "Failed to connect any given host:port" raise ConnectionFailure, "Failed to connect any given host:port"
else else
close # close any existing pools and sockets
raise ConnectionFailure, "Failed to connect to primary node." raise ConnectionFailure, "Failed to connect to primary node."
end end
end end
@ -136,7 +138,7 @@ module Mongo
# #
# @return [Boolean] # @return [Boolean]
def read_primary? def read_primary?
!@read_pool || @read_pool.length.zero? !@read_pool
end end
alias :primary? :read_primary? alias :primary? :read_primary?
@ -194,9 +196,13 @@ module Mongo
check_set_name(config, socket) check_set_name(config, socket)
rescue OperationFailure, SocketError, SystemCallError, IOError => ex rescue OperationFailure, SocketError, SystemCallError, IOError => ex
close unless connected? # It's necessary to rescue here. The #connect method will keep trying
# until it has no more nodes to try and raise a ConnectionFailure if
# it can't connect to a primary.
ensure ensure
socket.close if socket
@nodes_tried << node @nodes_tried << node
if config if config
nodes = [] nodes = []
nodes += config['hosts'] if config['hosts'] nodes += config['hosts'] if config['hosts']
@ -208,8 +214,6 @@ module Mongo
@logger.warn("MONGODB #{config['msg']}") @logger.warn("MONGODB #{config['msg']}")
end end
end end
socket.close if socket
end end
config config

View File

@ -76,7 +76,7 @@ module Mongo
socket = TCPSocket.new(@host, @port) socket = TCPSocket.new(@host, @port)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
rescue => ex rescue => ex
raise ConnectionFailure, "Failed to connect socket: #{ex}" raise ConnectionFailure, "Failed to connect to host #{@host} and port #{@port}: #{ex}"
end end
# If any saved authentications exist, we want to apply those # If any saved authentications exist, we want to apply those

View File

@ -17,8 +17,10 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
end end
def test_read_primary def test_read_primary
assert !@conn.read_primary? rescue_connection_failure do
assert !@conn.primary? assert !@conn.read_primary?
assert !@conn.primary?
end
end end
def test_con def test_con
@ -59,6 +61,12 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
# Should still be able to read immediately after killing master node # Should still be able to read immediately after killing master node
RS.kill_primary RS.kill_primary
assert_equal 2, @coll.find.to_a.length assert_equal 2, @coll.find.to_a.length
rescue_connection_failure do
@coll.save({:a => 50}, :safe => {:w => 2, :wtimeout => 10000})
end
RS.restart_killed_nodes
@coll.save({:a => 50}, :safe => {:w => 2, :wtimeout => 10000})
assert_equal 4, @coll.find.to_a.length
end end
def test_kill_secondary def test_kill_secondary
@ -71,6 +79,7 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
RS.kill(read_node) RS.kill(read_node)
# Should fail immediately on next read # Should fail immediately on next read
old_read_pool_port = @conn.read_pool.port
assert_raise ConnectionFailure do assert_raise ConnectionFailure do
@coll.find.to_a.length @coll.find.to_a.length
end end
@ -80,6 +89,8 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
length = @coll.find.to_a.length length = @coll.find.to_a.length
assert_equal 2, length assert_equal 2, length
end end
new_read_pool_port = @conn.read_pool.port
assert old_read_pool != new_read_pool
end end
end end

View File

@ -16,7 +16,7 @@ class Test::Unit::TestCase
begin begin
yield yield
rescue Mongo::ConnectionFailure => ex rescue Mongo::ConnectionFailure => ex
puts "Rescue attempt #{retries}" puts "Rescue attempt #{retries}: from #{ex}"
retries += 1 retries += 1
raise ex if retries > max_retries raise ex if retries > max_retries
sleep(1) sleep(1)