diff --git a/pymongo/replica_set_connection.py b/pymongo/replica_set_connection.py index 750745b411..7637d0ef52 100644 --- a/pymongo/replica_set_connection.py +++ b/pymongo/replica_set_connection.py @@ -815,7 +815,7 @@ def _send_message_with_response(self, msg, _connection_to_use=None, except AutoReconnect, why: self.disconnect() errors.append(why) - raise AutoReconnect(', '.join(errors)) + raise AutoReconnect(', '.join(map(str, errors))) def __cmp__(self, other): # XXX: Implement this? diff --git a/test/test_replica_set_connection.py b/test/test_replica_set_connection.py index 2ee9f5a132..51f44ba141 100644 --- a/test/test_replica_set_connection.py +++ b/test/test_replica_set_connection.py @@ -28,6 +28,7 @@ from bson.son import SON from bson.tz_util import utc +from mock import Mock, patch from pymongo import ReadPreference from pymongo.connection import Connection from pymongo.replica_set_connection import ReplicaSetConnection @@ -157,6 +158,14 @@ def make_db(base, name): self.assertEqual(connection.test, Database(connection, "test")) connection.close() + def test_auto_reconnect_exception_when_read_preference_is_secondary(self): + c = self._get_connection() + db = c.pymongo_test + + with patch('socket.socket.sendall', Mock(side_effect=socket.error)): + cursor = db.test.find(read_preference=ReadPreference.SECONDARY) + self.assertRaises(AutoReconnect, cursor.next) + def test_operations(self): c = self._get_connection()