From ab52b2f7726effda41eb002fd0cf4bb7c2ea32f9 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Fri, 28 Jan 2022 08:31:19 -0800 Subject: [PATCH] tests: Clean up some dangling timeouts Change-Id: Ia2710962b5a37b109bdd1b3a721da49c48c6f19a --- test/unit/common/test_exceptions.py | 5 +---- test/unit/common/test_utils.py | 31 ++++++++++++++--------------- test/unit/obj/test_updater.py | 17 ++++++---------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/test/unit/common/test_exceptions.py b/test/unit/common/test_exceptions.py index 6dbb0627a3..f369be2017 100644 --- a/test/unit/common/test_exceptions.py +++ b/test/unit/common/test_exceptions.py @@ -26,11 +26,8 @@ class TestExceptions(unittest.TestCase): self.assertEqual(str(exceptions.ReplicationException('test')), 'test') def test_replication_lock_timeout(self): - exc = exceptions.ReplicationLockTimeout(15, 'test') - try: + with exceptions.ReplicationLockTimeout(15, 'test') as exc: self.assertTrue(isinstance(exc, exceptions.MessageTimeout)) - finally: - exc.cancel() def test_client_exception(self): strerror = 'test: HTTP://random:888/randompath?foo=1 666 reason: ' \ diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index a5272a8b85..55deb47c40 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -1965,23 +1965,22 @@ class TestUtils(unittest.TestCase): self.assertIn('my error message', log_msg) # test eventlet.Timeout - connection_timeout = ConnectionTimeout(42, 'my error message') - log_exception(connection_timeout) - log_msg = strip_value(sio) - self.assertNotIn('Traceback', log_msg) - self.assertTrue('ConnectionTimeout' in log_msg) - self.assertTrue('(42s)' in log_msg) - self.assertNotIn('my error message', log_msg) - connection_timeout.cancel() + with ConnectionTimeout(42, 'my error message') \ + as connection_timeout: + log_exception(connection_timeout) + log_msg = strip_value(sio) + self.assertNotIn('Traceback', log_msg) + self.assertTrue('ConnectionTimeout' in log_msg) + self.assertTrue('(42s)' in log_msg) + self.assertNotIn('my error message', log_msg) - message_timeout = MessageTimeout(42, 'my error message') - log_exception(message_timeout) - log_msg = strip_value(sio) - self.assertNotIn('Traceback', log_msg) - self.assertTrue('MessageTimeout' in log_msg) - self.assertTrue('(42s)' in log_msg) - self.assertTrue('my error message' in log_msg) - message_timeout.cancel() + with MessageTimeout(42, 'my error message') as message_timeout: + log_exception(message_timeout) + log_msg = strip_value(sio) + self.assertNotIn('Traceback', log_msg) + self.assertTrue('MessageTimeout' in log_msg) + self.assertTrue('(42s)' in log_msg) + self.assertTrue('my error message' in log_msg) # test BadStatusLine log_exception(http_client.BadStatusLine('')) diff --git a/test/unit/obj/test_updater.py b/test/unit/obj/test_updater.py index a6c7cae98f..a45851f39d 100644 --- a/test/unit/obj/test_updater.py +++ b/test/unit/obj/test_updater.py @@ -618,11 +618,9 @@ class TestObjectUpdater(unittest.TestCase): # final update has Timeout ou.logger.clear() - mock_connect = mock.MagicMock() - mock_connect.getresponse = mock.MagicMock(side_effect=Timeout(99)) - - with mock.patch('swift.obj.updater.http_connect', - return_value=mock_connect): + with Timeout(99) as exc, \ + mock.patch('swift.obj.updater.http_connect') as mock_connect: + mock_connect.return_value.getresponse.side_effect = exc ou.run_once() self.assertTrue(os.path.exists(op_path)) self.assertEqual(ou.logger.get_increment_counts(), @@ -639,12 +637,9 @@ class TestObjectUpdater(unittest.TestCase): # final update has ConnectionTimeout ou.logger.clear() - mock_connect = mock.MagicMock() - mock_connect.getresponse = mock.MagicMock( - side_effect=ConnectionTimeout(9)) - - with mock.patch('swift.obj.updater.http_connect', - return_value=mock_connect): + with ConnectionTimeout(9) as exc, \ + mock.patch('swift.obj.updater.http_connect') as mock_connect: + mock_connect.return_value.getresponse.side_effect = exc ou.run_once() self.assertTrue(os.path.exists(op_path)) self.assertEqual(ou.logger.get_increment_counts(),