From 5ebb93a1eef31eacecb10deaed2ca3f987bc043c Mon Sep 17 00:00:00 2001 From: Joel Barciauskas Date: Thu, 23 Jul 2015 10:36:40 -0400 Subject: [PATCH] Set the second arg to __init__ for the HeartbeatFailure exception to 'mongo', create a split_mongo connection test Fixes TNL-2267 Clean up and PEP8 fixes to the test Add missing paramater back Add pylint pragma to ignore unused argument warning --- .../split_mongo/mongo_connection.py | 2 +- .../test_split_mongo_mongo_connection.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py diff --git a/common/lib/xmodule/xmodule/modulestore/split_mongo/mongo_connection.py b/common/lib/xmodule/xmodule/modulestore/split_mongo/mongo_connection.py index abe9fd1543..f03e9cb8df 100644 --- a/common/lib/xmodule/xmodule/modulestore/split_mongo/mongo_connection.py +++ b/common/lib/xmodule/xmodule/modulestore/split_mongo/mongo_connection.py @@ -309,7 +309,7 @@ class MongoConnection(object): if self.database.connection.alive(): return True else: - raise HeartbeatFailure("Can't connect to {}".format(self.database.name)) + raise HeartbeatFailure("Can't connect to {}".format(self.database.name), 'mongo') def get_structure(self, key, course_context=None): """ diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py new file mode 100644 index 0000000000..76a4cf58b6 --- /dev/null +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py @@ -0,0 +1,19 @@ +""" Test the behavior of split_mongo/MongoConnection """ +import unittest +from mock import patch +from xmodule.modulestore.split_mongo.mongo_connection import MongoConnection +from xmodule.exceptions import HeartbeatFailure + + +class TestHeartbeatFailureException(unittest.TestCase): + """ Test that a heartbeat failure is thrown at the appropriate times """ + @patch('pymongo.MongoClient') + @patch('pymongo.database.Database') + def test_heartbeat_raises_exception_when_connection_alive_is_false(self, *calls): + # pylint: disable=W0613 + with patch('mongodb_proxy.MongoProxy') as mock_proxy: + mock_proxy.return_value.alive.return_value = False + useless_conn = MongoConnection('useless', 'useless', 'useless') + + with self.assertRaises(HeartbeatFailure): + useless_conn.heartbeat()