From 6b8ab02ec8cf0ff3b6e4c323f18b289fc15c0249 Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Fri, 16 Dec 2011 13:44:21 -0200 Subject: [PATCH] Fix issue RUBY-391 Fix a problem with the exception raised when trying to insert in a replicaset when primary is not available. The ssue is reported here: https://jira.mongodb.org/browse/RUBY-391 --- lib/mongo/networking.rb | 10 +++++++--- test/replica_sets/connect_test.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/mongo/networking.rb b/lib/mongo/networking.rb index eed51f8..001e7a4 100644 --- a/lib/mongo/networking.rb +++ b/lib/mongo/networking.rb @@ -87,9 +87,13 @@ module Mongo end if num_received == 1 && (error = docs[0]['err'] || docs[0]['errmsg']) - close if error == "not master" - error = "wtimeout" if error == "timeout" - raise OperationFailure.new(docs[0]['code'].to_s + ': ' + error, docs[0]['code'], docs[0]) + if error.include?("not master") + close + raise ConnectionFailure.new(docs[0]['code'].to_s + ': ' + error, docs[0]['code'], docs[0]) + else + error = "wtimeout" if error == "timeout" + raise OperationFailure.new(docs[0]['code'].to_s + ': ' + error, docs[0]['code'], docs[0]) + end end docs[0] diff --git a/test/replica_sets/connect_test.rb b/test/replica_sets/connect_test.rb index 8c17204..f0f6e2a 100644 --- a/test/replica_sets/connect_test.rb +++ b/test/replica_sets/connect_test.rb @@ -80,6 +80,23 @@ class ConnectTest < Test::Unit::TestCase end end + def test_save_with_primary_stepped_down + @conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]], [self.rs.host, self.rs.ports[1]], + [self.rs.host, self.rs.ports[2]]) + + primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port) + + # Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors + step_down_command = BSON::OrderedHash.new + step_down_command[:replSetStepDown] = 60 + step_down_command[:force] = true + primary['admin'].command(step_down_command) + + rescue_connection_failure do + @conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}}) + end + end + def test_connect_with_connection_string @conn = Connection.from_uri("mongodb://#{self.rs.host}:#{self.rs.ports[0]},#{self.rs.host}:#{self.rs.ports[1]}?replicaset=#{self.rs.name}") assert @conn.is_a?(ReplSetConnection)