Merge "Implement context manager protocol for logging mutexes"

This commit is contained in:
Zuul 2024-07-25 20:45:47 +00:00 committed by Gerrit Code Review
commit 682c71afe4
2 changed files with 40 additions and 0 deletions

View File

@ -187,6 +187,13 @@ class PipeMutex(object):
# do, so nobody does it and that's okay.
self.close()
def __enter__(self):
self.acquire()
return self
def __exit__(self, *args):
self.release()
class NoopMutex(object):
"""
@ -219,6 +226,12 @@ class NoopMutex(object):
def release(self):
pass
def __enter__(self):
return self
def __exit__(self, *args):
pass
class ThreadSafeSysLogHandler(SysLogHandler):
def createLock(self):

View File

@ -5733,6 +5733,15 @@ class TestPipeMutex(unittest.TestCase):
self.mutex.release()
self.assertTrue(eventlet.spawn(try_acquire_lock).wait())
def test_context_manager_api(self):
def try_acquire_lock():
return self.mutex.acquire(blocking=False)
with self.mutex as ref:
self.assertIs(ref, self.mutex)
self.assertFalse(eventlet.spawn(try_acquire_lock).wait())
self.assertTrue(eventlet.spawn(try_acquire_lock).wait())
def test_release_without_acquire(self):
self.assertRaises(RuntimeError, self.mutex.release)
@ -5867,6 +5876,24 @@ class TestPipeMutex(unittest.TestCase):
eventlet.debug.hub_prevent_multiple_readers(True)
class TestNoopMutex(unittest.TestCase):
def setUp(self):
self.mutex = utils.NoopMutex()
def test_acquire_release_api(self):
# Prior to 3.13, logging called these explicitly
self.mutex.acquire()
self.mutex.release()
def test_context_manager_api(self):
# python 3.13 started using it as a context manager
def try_acquire_lock():
return self.mutex.acquire(blocking=False)
with self.mutex as ref:
self.assertIs(ref, self.mutex)
class TestDistributeEvenly(unittest.TestCase):
def test_evenly_divided(self):
out = utils.distribute_evenly(range(12), 3)