From a99969ef8a0ce7232fab10807ee1b46dcb5817f4 Mon Sep 17 00:00:00 2001 From: Oleksii Zamiatin Date: Mon, 11 Apr 2016 14:21:47 +0300 Subject: [PATCH] Provide single step check if eventlet is monkey_patched Currently to perform such check we need to check if eventlet is available first, that give us two steps, but it is desired to check this at once. This change is to remove the function here: [1] https://github.com/openstack/oslo.messaging/blob/master/oslo_messaging/_drivers/pika_driver/pika_commons.py#L37 Also will be useful in zmq-driver. Change-Id: Ie24ecb54946bfba465d293185ebfda25691a8701 --- oslo_utils/eventletutils.py | 11 +++++++++++ oslo_utils/tests/test_eventletutils.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/oslo_utils/eventletutils.py b/oslo_utils/eventletutils.py index 2d2a299f..fc3080a0 100644 --- a/oslo_utils/eventletutils.py +++ b/oslo_utils/eventletutils.py @@ -127,3 +127,14 @@ def warn_eventlet_not_patched(expected_patched_modules=None, " spurious or unexpected lock-ups" " and/or hangs)" % (not_patched, what), RuntimeWarning, stacklevel=3) + + +def is_monkey_patched(module): + """Determines safely is eventlet patching for module enabled or not + :param module: String, module name + :return Bool, True if module is patched, False otherwise + """ + + if _patcher is None: + return False + return _patcher.is_monkey_patched(module) diff --git a/oslo_utils/tests/test_eventletutils.py b/oslo_utils/tests/test_eventletutils.py index 28a30ffa..1b588d0f 100644 --- a/oslo_utils/tests/test_eventletutils.py +++ b/oslo_utils/tests/test_eventletutils.py @@ -78,6 +78,17 @@ class EventletUtilsTest(test_base.BaseTestCase): eventletutils.warn_eventlet_not_patched(['os']) self.assertEqual(0, len(capture)) + @mock.patch("oslo_utils.eventletutils._patcher") + def test_eventlet_is_patched(self, mock_patcher): + mock_patcher.is_monkey_patched.return_value = True + self.assertTrue(eventletutils.is_monkey_patched('os')) + mock_patcher.is_monkey_patched.return_value = False + self.assertFalse(eventletutils.is_monkey_patched('os')) + + @mock.patch("oslo_utils.eventletutils._patcher", None) + def test_eventlet_no_patcher(self): + self.assertFalse(eventletutils.is_monkey_patched('os')) + @mock.patch("oslo_utils.eventletutils._patcher") def test_partially_patched_warning(self, mock_patcher): is_patched = set()