summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2014-01-26 00:51:57 (GMT)
committerGuido van Rossum <guido@python.org>2014-01-26 00:51:57 (GMT)
commitab3c88983bc6c2a6ae98625296eef9d7588c8d69 (patch)
tree011a16257d0b36aa1f0fa4ab3535f9eca25098d0 /Lib/test
parentab27a9fc4b4edff7c17e699b7e9e2173e9f8bc53 (diff)
downloadcpython-ab3c88983bc6c2a6ae98625296eef9d7588c8d69.zip
cpython-ab3c88983bc6c2a6ae98625296eef9d7588c8d69.tar.gz
cpython-ab3c88983bc6c2a6ae98625296eef9d7588c8d69.tar.bz2
asyncio: Locks refactor: use a separate context manager; remove Semaphore._locked.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_asyncio/test_locks.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index 5d0e09e..0975f49 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -208,6 +208,24 @@ class LockTests(unittest.TestCase):
self.assertFalse(lock.locked())
+ def test_context_manager_cant_reuse(self):
+ lock = asyncio.Lock(loop=self.loop)
+
+ @asyncio.coroutine
+ def acquire_lock():
+ return (yield from lock)
+
+ # This spells "yield from lock" outside a generator.
+ cm = self.loop.run_until_complete(acquire_lock())
+ with cm:
+ self.assertTrue(lock.locked())
+
+ self.assertFalse(lock.locked())
+
+ with self.assertRaises(AttributeError):
+ with cm:
+ pass
+
def test_context_manager_no_yield(self):
lock = asyncio.Lock(loop=self.loop)
@@ -219,6 +237,8 @@ class LockTests(unittest.TestCase):
str(err),
'"yield from" should be used as context manager expression')
+ self.assertFalse(lock.locked())
+
class EventTests(unittest.TestCase):
@@ -655,6 +675,8 @@ class ConditionTests(unittest.TestCase):
str(err),
'"yield from" should be used as context manager expression')
+ self.assertFalse(cond.locked())
+
class SemaphoreTests(unittest.TestCase):
@@ -830,6 +852,19 @@ class SemaphoreTests(unittest.TestCase):
self.assertEqual(2, sem._value)
+ def test_context_manager_no_yield(self):
+ sem = asyncio.Semaphore(2, loop=self.loop)
+
+ try:
+ with sem:
+ self.fail('RuntimeError is not raised in with expression')
+ except RuntimeError as err:
+ self.assertEqual(
+ str(err),
+ '"yield from" should be used as context manager expression')
+
+ self.assertEqual(2, sem._value)
+
if __name__ == '__main__':
unittest.main()