Skip to content

Commit

Permalink
Add a test of throwing an exception out of a region locked by using a…
Browse files Browse the repository at this point in the history
… DeferredLock as an asynchronous context manager. Add some docstrings.
  • Loading branch information
wiml committed Feb 21, 2019
1 parent fcbb1d9 commit 55cab3e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/twisted/test/test_defer.py.3only
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class DeferredTestsAsync(unittest.TestCase):

@ensuringDeferred
async def test_asyncWithLock(self):
"""
L{defer.DeferredLock} can be used as an asynchronous context manager.
"""
lock = defer.DeferredLock()
async with lock:
self.assertTrue(lock.locked)
Expand All @@ -45,6 +48,9 @@ class DeferredTestsAsync(unittest.TestCase):

@ensuringDeferred
async def test_asyncWithSemaphore(self):
"""
L{defer.DeferredSemaphore} can be used as an asynchronous context manager.
"""
sem = defer.DeferredSemaphore(3)

async with sem:
Expand All @@ -64,3 +70,16 @@ class DeferredTestsAsync(unittest.TestCase):
await d2
self.assertEqual(sem.tokens, 2)
self.assertEqual(sem.tokens, 3)

@ensuringDeferred
async def test_asyncWithLockException(self):
"""
C{defer.DeferredLock} correctly propagates exceptions when
used as an asynchronous context manager.
"""
lock = defer.DeferredLock()
with self.assertRaisesRegexp(Exception, 'some specific exception'):
async with lock:
self.assertTrue(lock.locked)
raise Exception('some specific exception')
self.assertFalse(lock.locked)

0 comments on commit 55cab3e

Please sign in to comment.