diff options
Diffstat (limited to 'test/unit/common/test_utils.py')
-rw-r--r-- | test/unit/common/test_utils.py | 110 |
1 files changed, 86 insertions, 24 deletions
diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index e477ee85d..e66508c6d 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -210,6 +210,61 @@ class TestUtils(unittest.TestCase): self.md5_digest = '0d6dc3c588ae71a04ce9a6beebbbba06' self.fips_enabled = True + def test_monkey_patch(self): + def take_and_release(lock): + try: + lock.acquire() + finally: + lock.release() + + def do_test(): + res = 0 + try: + # this module imports eventlet original threading, so re-import + # locally... + import threading + import traceback + logging_lock_before = logging._lock + my_lock_before = threading.RLock() + self.assertIsInstance(logging_lock_before, + type(my_lock_before)) + + utils.monkey_patch() + + logging_lock_after = logging._lock + my_lock_after = threading.RLock() + self.assertIsInstance(logging_lock_after, + type(my_lock_after)) + + self.assertTrue(logging_lock_after.acquire()) + thread = threading.Thread(target=take_and_release, + args=(logging_lock_after,)) + thread.start() + self.assertTrue(thread.isAlive()) + # we should timeout while the thread is still blocking on lock + eventlet.sleep() + thread.join(timeout=0.1) + self.assertTrue(thread.isAlive()) + + logging._lock.release() + thread.join(timeout=0.1) + self.assertFalse(thread.isAlive()) + except AssertionError: + traceback.print_exc() + res = 1 + finally: + os._exit(res) + + pid = os.fork() + if pid == 0: + # run the test in an isolated environment to avoid monkey patching + # in this one + do_test() + else: + child_pid, errcode = os.waitpid(pid, 0) + self.assertEqual(0, os.WEXITSTATUS(errcode), + 'Forked do_test failed') + def test_get_zero_indexed_base_string(self): self.assertEqual(utils.get_zero_indexed_base_string('something', 0), 'something') @@ -1144,11 +1199,15 @@ class TestUtils(unittest.TestCase): # test eventlet.Timeout with ConnectionTimeout(42, 'my error message') \ as connection_timeout: - log_exception(connection_timeout) + now = time.time() + connection_timeout.created_at = now - 123.456 + with mock.patch('swift.common.utils.time.time', + return_value=now): + 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.assertTrue('(42s after 123.46s)' in log_msg) self.assertNotIn('my error message', log_msg) with MessageTimeout(42, 'my error message') as message_timeout: @@ -3403,7 +3462,7 @@ cluster_dfw1 = http://dfw1.host/v1/ if tempdir: shutil.rmtree(tempdir) - def test_find_shard_range(self): + def test_find_namespace(self): ts = utils.Timestamp.now().internal start = utils.ShardRange('a/-a', ts, '', 'a') atof = utils.ShardRange('a/a-f', ts, 'a', 'f') @@ -3413,29 +3472,29 @@ cluster_dfw1 = http://dfw1.host/v1/ end = utils.ShardRange('a/z-', ts, 'z', '') ranges = [start, atof, ftol, ltor, rtoz, end] - found = utils.find_shard_range('', ranges) + found = utils.find_namespace('', ranges) self.assertEqual(found, None) - found = utils.find_shard_range(' ', ranges) + found = utils.find_namespace(' ', ranges) self.assertEqual(found, start) - found = utils.find_shard_range(' ', ranges[1:]) + found = utils.find_namespace(' ', ranges[1:]) self.assertEqual(found, None) - found = utils.find_shard_range('b', ranges) + found = utils.find_namespace('b', ranges) self.assertEqual(found, atof) - found = utils.find_shard_range('f', ranges) + found = utils.find_namespace('f', ranges) self.assertEqual(found, atof) - found = utils.find_shard_range('f\x00', ranges) + found = utils.find_namespace('f\x00', ranges) self.assertEqual(found, ftol) - found = utils.find_shard_range('x', ranges) + found = utils.find_namespace('x', ranges) self.assertEqual(found, rtoz) - found = utils.find_shard_range('r', ranges) + found = utils.find_namespace('r', ranges) self.assertEqual(found, ltor) - found = utils.find_shard_range('}', ranges) + found = utils.find_namespace('}', ranges) self.assertEqual(found, end) - found = utils.find_shard_range('}', ranges[:-1]) + found = utils.find_namespace('}', ranges[:-1]) self.assertEqual(found, None) # remove l-r from list of ranges and try and find a shard range for an # item in that range. - found = utils.find_shard_range('p', ranges[:-3] + ranges[-2:]) + found = utils.find_namespace('p', ranges[:-3] + ranges[-2:]) self.assertEqual(found, None) # add some sub-shards; a sub-shard's state is less than its parent @@ -3445,20 +3504,20 @@ cluster_dfw1 = http://dfw1.host/v1/ htok = utils.ShardRange('a/h-k', ts, 'h', 'k') overlapping_ranges = ranges[:2] + [ftoh, htok] + ranges[2:] - found = utils.find_shard_range('g', overlapping_ranges) + found = utils.find_namespace('g', overlapping_ranges) self.assertEqual(found, ftoh) - found = utils.find_shard_range('h', overlapping_ranges) + found = utils.find_namespace('h', overlapping_ranges) self.assertEqual(found, ftoh) - found = utils.find_shard_range('k', overlapping_ranges) + found = utils.find_namespace('k', overlapping_ranges) self.assertEqual(found, htok) - found = utils.find_shard_range('l', overlapping_ranges) + found = utils.find_namespace('l', overlapping_ranges) self.assertEqual(found, ftol) - found = utils.find_shard_range('m', overlapping_ranges) + found = utils.find_namespace('m', overlapping_ranges) self.assertEqual(found, ltor) ktol = utils.ShardRange('a/k-l', ts, 'k', 'l') overlapping_ranges = ranges[:2] + [ftoh, htok, ktol] + ranges[2:] - found = utils.find_shard_range('l', overlapping_ranges) + found = utils.find_namespace('l', overlapping_ranges) self.assertEqual(found, ktol) def test_parse_db_filename(self): @@ -7960,7 +8019,7 @@ class TestShardRange(unittest.TestCase): with self.assertRaises(KeyError): utils.ShardRange.from_dict(bad_dict) # But __init__ still (generally) works! - if key not in ('name', 'timestamp'): + if key != 'name': utils.ShardRange(**bad_dict) else: with self.assertRaises(TypeError): @@ -8744,13 +8803,16 @@ class TestWatchdog(unittest.TestCase): w._evt.send = mock.Mock(side_effect=w._evt.send) gth = object() + now = time.time() + timeout_value = 1.0 with patch('eventlet.greenthread.getcurrent', return_value=gth),\ - patch('time.time', return_value=10.0): + patch('time.time', return_value=now): # On first call, _next_expiration is None, it should unblock # greenthread that is blocked for ever - key = w.start(1.0, Timeout) + key = w.start(timeout_value, Timeout) self.assertIn(key, w._timeouts) - self.assertEqual(w._timeouts[key], (1.0, 11.0, gth, Timeout)) + self.assertEqual(w._timeouts[key], ( + timeout_value, now + timeout_value, gth, Timeout, now)) w._evt.send.assert_called_once() w.stop(key) |