Skip to content

Commit ed44a74

Browse files
committed
Improved replica set failover tests. A few
improved exception messages.
1 parent 2335108 commit ed44a74

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

lib/mongo/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ def receive_message_on_socket(length, socket)
795795
begin
796796
message = new_binary_string
797797
socket.read(length, message)
798-
raise ConnectionFailure, "connection closed" unless message.length > 0
798+
raise ConnectionFailure, "connection closed" unless message && message.length > 0
799799
if message.length < length
800800
chunk = new_binary_string
801801
while message.length < length

lib/mongo/repl_set_connection.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ def connect
119119
BSON::BSON_CODER.update_max_bson_size(self)
120120
else
121121
if @secondary_pools.empty?
122+
close # close any existing pools and sockets
122123
raise ConnectionFailure, "Failed to connect any given host:port"
123124
else
125+
close # close any existing pools and sockets
124126
raise ConnectionFailure, "Failed to connect to primary node."
125127
end
126128
end
@@ -136,7 +138,7 @@ def connecting?
136138
#
137139
# @return [Boolean]
138140
def read_primary?
139-
!@read_pool || @read_pool.length.zero?
141+
!@read_pool
140142
end
141143
alias :primary? :read_primary?
142144

@@ -194,9 +196,13 @@ def check_is_master(node)
194196

195197
check_set_name(config, socket)
196198
rescue OperationFailure, SocketError, SystemCallError, IOError => ex
197-
close unless connected?
199+
# It's necessary to rescue here. The #connect method will keep trying
200+
# until it has no more nodes to try and raise a ConnectionFailure if
201+
# it can't connect to a primary.
198202
ensure
203+
socket.close if socket
199204
@nodes_tried << node
205+
200206
if config
201207
nodes = []
202208
nodes += config['hosts'] if config['hosts']
@@ -208,8 +214,6 @@ def check_is_master(node)
208214
@logger.warn("MONGODB #{config['msg']}")
209215
end
210216
end
211-
212-
socket.close if socket
213217
end
214218

215219
config

lib/mongo/util/pool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def checkout_new_socket
7676
socket = TCPSocket.new(@host, @port)
7777
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
7878
rescue => ex
79-
raise ConnectionFailure, "Failed to connect socket: #{ex}"
79+
raise ConnectionFailure, "Failed to connect to host #{@host} and port #{@port}: #{ex}"
8080
end
8181

8282
# If any saved authentications exist, we want to apply those

test/replica_sets/query_secondaries.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ def teardown
1717
end
1818

1919
def test_read_primary
20-
assert !@conn.read_primary?
21-
assert !@conn.primary?
20+
rescue_connection_failure do
21+
assert !@conn.read_primary?
22+
assert !@conn.primary?
23+
end
2224
end
2325

2426
def test_con
@@ -59,6 +61,12 @@ def test_kill_primary
5961
# Should still be able to read immediately after killing master node
6062
RS.kill_primary
6163
assert_equal 2, @coll.find.to_a.length
64+
rescue_connection_failure do
65+
@coll.save({:a => 50}, :safe => {:w => 2, :wtimeout => 10000})
66+
end
67+
RS.restart_killed_nodes
68+
@coll.save({:a => 50}, :safe => {:w => 2, :wtimeout => 10000})
69+
assert_equal 4, @coll.find.to_a.length
6270
end
6371

6472
def test_kill_secondary
@@ -71,6 +79,7 @@ def test_kill_secondary
7179
RS.kill(read_node)
7280

7381
# Should fail immediately on next read
82+
old_read_pool_port = @conn.read_pool.port
7483
assert_raise ConnectionFailure do
7584
@coll.find.to_a.length
7685
end
@@ -80,6 +89,8 @@ def test_kill_secondary
8089
length = @coll.find.to_a.length
8190
assert_equal 2, length
8291
end
92+
new_read_pool_port = @conn.read_pool.port
93+
assert old_read_pool != new_read_pool
8394
end
8495

8596
end

test/replica_sets/rs_test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def rescue_connection_failure(max_retries=60)
1616
begin
1717
yield
1818
rescue Mongo::ConnectionFailure => ex
19-
puts "Rescue attempt #{retries}"
19+
puts "Rescue attempt #{retries}: from #{ex}"
2020
retries += 1
2121
raise ex if retries > max_retries
2222
sleep(1)

0 commit comments

Comments
 (0)