2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for lock.py"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import unittest.mock
|
2013-11-04 17:18:19 -04:00
|
|
|
import re
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
import asyncio
|
2013-10-17 17:40:50 -03:00
|
|
|
from asyncio import test_utils
|
|
|
|
|
|
|
|
|
2013-11-04 17:18:19 -04:00
|
|
|
STR_RGX_REPR = (
|
|
|
|
r'^<(?P<class>.*?) object at (?P<address>.*?)'
|
|
|
|
r'\[(?P<extras>'
|
|
|
|
r'(set|unset|locked|unlocked)(,value:\d)?(,waiters:\d+)?'
|
|
|
|
r')\]>\Z'
|
|
|
|
)
|
|
|
|
RGX_REPR = re.compile(STR_RGX_REPR)
|
|
|
|
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
class LockTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = test_utils.TestLoop()
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
def test_ctor_loop(self):
|
|
|
|
loop = unittest.mock.Mock()
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(lock._loop, loop)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(lock._loop, self.loop)
|
|
|
|
|
|
|
|
def test_ctor_noloop(self):
|
|
|
|
try:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
lock = asyncio.Lock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(lock._loop, self.loop)
|
|
|
|
finally:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_repr(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(repr(lock).endswith('[unlocked]>'))
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertTrue(RGX_REPR.match(repr(lock)))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def acquire_lock():
|
|
|
|
yield from lock
|
|
|
|
|
|
|
|
self.loop.run_until_complete(acquire_lock())
|
|
|
|
self.assertTrue(repr(lock).endswith('[locked]>'))
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertTrue(RGX_REPR.match(repr(lock)))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_lock(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def acquire_lock():
|
|
|
|
return (yield from lock)
|
|
|
|
|
|
|
|
res = self.loop.run_until_complete(acquire_lock())
|
|
|
|
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertTrue(lock.locked())
|
|
|
|
|
|
|
|
lock.release()
|
|
|
|
self.assertFalse(lock.locked())
|
|
|
|
|
|
|
|
def test_acquire(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
result = []
|
|
|
|
|
|
|
|
self.assertTrue(self.loop.run_until_complete(lock.acquire()))
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
if (yield from lock.acquire()):
|
|
|
|
result.append(1)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c2(result):
|
|
|
|
if (yield from lock.acquire()):
|
|
|
|
result.append(2)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c3(result):
|
|
|
|
if (yield from lock.acquire()):
|
|
|
|
result.append(3)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(c1(result), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(c2(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
lock.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t3 = asyncio.Task(c3(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
lock.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1, 2], result)
|
|
|
|
|
|
|
|
lock.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1, 2, 3], result)
|
|
|
|
|
|
|
|
self.assertTrue(t1.done())
|
|
|
|
self.assertTrue(t1.result())
|
|
|
|
self.assertTrue(t2.done())
|
|
|
|
self.assertTrue(t2.result())
|
|
|
|
self.assertTrue(t3.done())
|
|
|
|
self.assertTrue(t3.result())
|
|
|
|
|
|
|
|
def test_acquire_cancel(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.loop.run_until_complete(lock.acquire()))
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
task = asyncio.Task(lock.acquire(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(task.cancel)
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError,
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete, task)
|
|
|
|
self.assertFalse(lock._waiters)
|
|
|
|
|
|
|
|
def test_cancel_race(self):
|
|
|
|
# Several tasks:
|
|
|
|
# - A acquires the lock
|
|
|
|
# - B is blocked in aqcuire()
|
|
|
|
# - C is blocked in aqcuire()
|
|
|
|
#
|
|
|
|
# Now, concurrently:
|
|
|
|
# - B is cancelled
|
|
|
|
# - A releases the lock
|
|
|
|
#
|
|
|
|
# If B's waiter is marked cancelled but not yet removed from
|
|
|
|
# _waiters, A's release() call will crash when trying to set
|
|
|
|
# B's waiter; instead, it should move on to C's waiter.
|
|
|
|
|
|
|
|
# Setup: A has the lock, b and c are waiting.
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def lockit(name, blocker):
|
|
|
|
yield from lock.acquire()
|
|
|
|
try:
|
|
|
|
if blocker is not None:
|
|
|
|
yield from blocker
|
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
fa = asyncio.Future(loop=self.loop)
|
|
|
|
ta = asyncio.Task(lockit('A', fa), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertTrue(lock.locked())
|
2014-01-25 10:32:06 -04:00
|
|
|
tb = asyncio.Task(lockit('B', None), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(len(lock._waiters), 1)
|
2014-01-25 10:32:06 -04:00
|
|
|
tc = asyncio.Task(lockit('C', None), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(len(lock._waiters), 2)
|
|
|
|
|
|
|
|
# Create the race and check.
|
|
|
|
# Without the fix this failed at the last assert.
|
|
|
|
fa.set_result(None)
|
|
|
|
tb.cancel()
|
|
|
|
self.assertTrue(lock._waiters[0].cancelled())
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(lock.locked())
|
|
|
|
self.assertTrue(ta.done())
|
|
|
|
self.assertTrue(tb.cancelled())
|
|
|
|
self.assertTrue(tc.done())
|
|
|
|
|
|
|
|
def test_release_not_acquired(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(RuntimeError, lock.release)
|
|
|
|
|
|
|
|
def test_release_no_waiters(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(lock.acquire())
|
|
|
|
self.assertTrue(lock.locked())
|
|
|
|
|
|
|
|
lock.release()
|
|
|
|
self.assertFalse(lock.locked())
|
|
|
|
|
|
|
|
def test_context_manager(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def acquire_lock():
|
|
|
|
return (yield from lock)
|
|
|
|
|
|
|
|
with self.loop.run_until_complete(acquire_lock()):
|
|
|
|
self.assertTrue(lock.locked())
|
|
|
|
|
|
|
|
self.assertFalse(lock.locked())
|
|
|
|
|
2014-01-25 20:51:57 -04:00
|
|
|
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
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_context_manager_no_yield(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
lock = asyncio.Lock(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
try:
|
|
|
|
with lock:
|
|
|
|
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')
|
|
|
|
|
2014-01-25 20:51:57 -04:00
|
|
|
self.assertFalse(lock.locked())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
class EventTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = test_utils.TestLoop()
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
def test_ctor_loop(self):
|
|
|
|
loop = unittest.mock.Mock()
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(ev._loop, loop)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(ev._loop, self.loop)
|
|
|
|
|
|
|
|
def test_ctor_noloop(self):
|
|
|
|
try:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
ev = asyncio.Event()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(ev._loop, self.loop)
|
|
|
|
finally:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_repr(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(repr(ev).endswith('[unset]>'))
|
2013-11-04 17:18:19 -04:00
|
|
|
match = RGX_REPR.match(repr(ev))
|
|
|
|
self.assertEqual(match.group('extras'), 'unset')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
ev.set()
|
|
|
|
self.assertTrue(repr(ev).endswith('[set]>'))
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertTrue(RGX_REPR.match(repr(ev)))
|
|
|
|
|
|
|
|
ev._waiters.append(unittest.mock.Mock())
|
|
|
|
self.assertTrue('waiters:1' in repr(ev))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(ev)))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(ev.is_set())
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
if (yield from ev.wait()):
|
|
|
|
result.append(1)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c2(result):
|
|
|
|
if (yield from ev.wait()):
|
|
|
|
result.append(2)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c3(result):
|
|
|
|
if (yield from ev.wait()):
|
|
|
|
result.append(3)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(c1(result), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(c2(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t3 = asyncio.Task(c3(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
ev.set()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([3, 1, 2], result)
|
|
|
|
|
|
|
|
self.assertTrue(t1.done())
|
|
|
|
self.assertIsNone(t1.result())
|
|
|
|
self.assertTrue(t2.done())
|
|
|
|
self.assertIsNone(t2.result())
|
|
|
|
self.assertTrue(t3.done())
|
|
|
|
self.assertIsNone(t3.result())
|
|
|
|
|
|
|
|
def test_wait_on_set(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
ev.set()
|
|
|
|
|
|
|
|
res = self.loop.run_until_complete(ev.wait())
|
|
|
|
self.assertTrue(res)
|
|
|
|
|
|
|
|
def test_wait_cancel(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
wait = asyncio.Task(ev.wait(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(wait.cancel)
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError,
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete, wait)
|
|
|
|
self.assertFalse(ev._waiters)
|
|
|
|
|
|
|
|
def test_clear(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(ev.is_set())
|
|
|
|
|
|
|
|
ev.set()
|
|
|
|
self.assertTrue(ev.is_set())
|
|
|
|
|
|
|
|
ev.clear()
|
|
|
|
self.assertFalse(ev.is_set())
|
|
|
|
|
|
|
|
def test_clear_with_waiters(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
ev = asyncio.Event(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
result = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
if (yield from ev.wait()):
|
|
|
|
result.append(1)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(c1(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
ev.set()
|
|
|
|
ev.clear()
|
|
|
|
self.assertFalse(ev.is_set())
|
|
|
|
|
|
|
|
ev.set()
|
|
|
|
ev.set()
|
|
|
|
self.assertEqual(1, len(ev._waiters))
|
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
self.assertEqual(0, len(ev._waiters))
|
|
|
|
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(t.result())
|
|
|
|
|
|
|
|
|
|
|
|
class ConditionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = test_utils.TestLoop()
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
def test_ctor_loop(self):
|
|
|
|
loop = unittest.mock.Mock()
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(cond._loop, loop)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(cond._loop, self.loop)
|
|
|
|
|
|
|
|
def test_ctor_noloop(self):
|
|
|
|
try:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
cond = asyncio.Condition()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(cond._loop, self.loop)
|
|
|
|
finally:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
result = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(1)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c2(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(2)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c3(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(3)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(c1(result), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(c2(result), loop=self.loop)
|
|
|
|
t3 = asyncio.Task(c3(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
self.assertFalse(cond.locked())
|
|
|
|
|
|
|
|
self.assertTrue(self.loop.run_until_complete(cond.acquire()))
|
|
|
|
cond.notify()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
cond.notify(2)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1, 2], result)
|
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1, 2, 3], result)
|
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
self.assertTrue(t1.done())
|
|
|
|
self.assertTrue(t1.result())
|
|
|
|
self.assertTrue(t2.done())
|
|
|
|
self.assertTrue(t2.result())
|
|
|
|
self.assertTrue(t3.done())
|
|
|
|
self.assertTrue(t3.result())
|
|
|
|
|
|
|
|
def test_wait_cancel(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
wait = asyncio.Task(cond.wait(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(wait.cancel)
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError,
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete, wait)
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertFalse(cond._waiters)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
def test_wait_unacquired(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(
|
|
|
|
RuntimeError,
|
|
|
|
self.loop.run_until_complete, cond.wait())
|
|
|
|
|
|
|
|
def test_wait_for(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
presult = False
|
|
|
|
|
|
|
|
def predicate():
|
|
|
|
return presult
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait_for(predicate)):
|
|
|
|
result.append(1)
|
|
|
|
cond.release()
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(c1(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
cond.notify()
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
presult = True
|
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
cond.notify()
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(t.result())
|
|
|
|
|
|
|
|
def test_wait_for_unacquired(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
# predicate can return true immediately
|
|
|
|
res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))
|
|
|
|
self.assertEqual([1, 2, 3], res)
|
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
RuntimeError,
|
|
|
|
self.loop.run_until_complete,
|
|
|
|
cond.wait_for(lambda: False))
|
|
|
|
|
|
|
|
def test_notify(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
result = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(1)
|
|
|
|
cond.release()
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c2(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(2)
|
|
|
|
cond.release()
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c3(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(3)
|
|
|
|
cond.release()
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(c1(result), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(c2(result), loop=self.loop)
|
|
|
|
t3 = asyncio.Task(c3(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
cond.notify(1)
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
|
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
cond.notify(1)
|
|
|
|
cond.notify(2048)
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1, 2, 3], result)
|
|
|
|
|
|
|
|
self.assertTrue(t1.done())
|
|
|
|
self.assertTrue(t1.result())
|
|
|
|
self.assertTrue(t2.done())
|
|
|
|
self.assertTrue(t2.result())
|
|
|
|
self.assertTrue(t3.done())
|
|
|
|
self.assertTrue(t3.result())
|
|
|
|
|
|
|
|
def test_notify_all(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
result = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(1)
|
|
|
|
cond.release()
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c2(result):
|
|
|
|
yield from cond.acquire()
|
|
|
|
if (yield from cond.wait()):
|
|
|
|
result.append(2)
|
|
|
|
cond.release()
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(c1(result), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(c2(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
cond.notify_all()
|
|
|
|
cond.release()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1, 2], result)
|
|
|
|
|
|
|
|
self.assertTrue(t1.done())
|
|
|
|
self.assertTrue(t1.result())
|
|
|
|
self.assertTrue(t2.done())
|
|
|
|
self.assertTrue(t2.result())
|
|
|
|
|
|
|
|
def test_notify_unacquired(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(RuntimeError, cond.notify)
|
|
|
|
|
|
|
|
def test_notify_all_unacquired(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(RuntimeError, cond.notify_all)
|
|
|
|
|
2013-11-04 17:18:19 -04:00
|
|
|
def test_repr(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertTrue('unlocked' in repr(cond))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(cond)))
|
|
|
|
|
|
|
|
self.loop.run_until_complete(cond.acquire())
|
|
|
|
self.assertTrue('locked' in repr(cond))
|
|
|
|
|
|
|
|
cond._waiters.append(unittest.mock.Mock())
|
|
|
|
self.assertTrue('waiters:1' in repr(cond))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(cond)))
|
|
|
|
|
|
|
|
cond._waiters.append(unittest.mock.Mock())
|
|
|
|
self.assertTrue('waiters:2' in repr(cond))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(cond)))
|
|
|
|
|
|
|
|
def test_context_manager(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-11-04 17:18:19 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-11-04 17:18:19 -04:00
|
|
|
def acquire_cond():
|
|
|
|
return (yield from cond)
|
|
|
|
|
|
|
|
with self.loop.run_until_complete(acquire_cond()):
|
|
|
|
self.assertTrue(cond.locked())
|
|
|
|
|
|
|
|
self.assertFalse(cond.locked())
|
|
|
|
|
|
|
|
def test_context_manager_no_yield(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
cond = asyncio.Condition(loop=self.loop)
|
2013-11-04 17:18:19 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
with cond:
|
|
|
|
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')
|
|
|
|
|
2014-01-25 20:51:57 -04:00
|
|
|
self.assertFalse(cond.locked())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
class SemaphoreTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = test_utils.TestLoop()
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
def test_ctor_loop(self):
|
|
|
|
loop = unittest.mock.Mock()
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(sem._loop, loop)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(sem._loop, self.loop)
|
|
|
|
|
|
|
|
def test_ctor_noloop(self):
|
|
|
|
try:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
sem = asyncio.Semaphore()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(sem._loop, self.loop)
|
|
|
|
finally:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-21 15:07:45 -04:00
|
|
|
def test_initial_value_zero(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(0, loop=self.loop)
|
2013-11-21 15:07:45 -04:00
|
|
|
self.assertTrue(sem.locked())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_repr(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertTrue(RGX_REPR.match(repr(sem)))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.loop.run_until_complete(sem.acquire())
|
|
|
|
self.assertTrue(repr(sem).endswith('[locked]>'))
|
2013-11-04 17:18:19 -04:00
|
|
|
self.assertTrue('waiters' not in repr(sem))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(sem)))
|
|
|
|
|
|
|
|
sem._waiters.append(unittest.mock.Mock())
|
|
|
|
self.assertTrue('waiters:1' in repr(sem))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(sem)))
|
|
|
|
|
|
|
|
sem._waiters.append(unittest.mock.Mock())
|
|
|
|
self.assertTrue('waiters:2' in repr(sem))
|
|
|
|
self.assertTrue(RGX_REPR.match(repr(sem)))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_semaphore(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(1, sem._value)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def acquire_lock():
|
|
|
|
return (yield from sem)
|
|
|
|
|
|
|
|
res = self.loop.run_until_complete(acquire_lock())
|
|
|
|
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertTrue(sem.locked())
|
|
|
|
self.assertEqual(0, sem._value)
|
|
|
|
|
|
|
|
sem.release()
|
|
|
|
self.assertFalse(sem.locked())
|
|
|
|
self.assertEqual(1, sem._value)
|
|
|
|
|
|
|
|
def test_semaphore_value(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertRaises(ValueError, asyncio.Semaphore, -1)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_acquire(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(3, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
result = []
|
|
|
|
|
|
|
|
self.assertTrue(self.loop.run_until_complete(sem.acquire()))
|
|
|
|
self.assertTrue(self.loop.run_until_complete(sem.acquire()))
|
|
|
|
self.assertFalse(sem.locked())
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c1(result):
|
|
|
|
yield from sem.acquire()
|
|
|
|
result.append(1)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c2(result):
|
|
|
|
yield from sem.acquire()
|
|
|
|
result.append(2)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c3(result):
|
|
|
|
yield from sem.acquire()
|
|
|
|
result.append(3)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def c4(result):
|
|
|
|
yield from sem.acquire()
|
|
|
|
result.append(4)
|
|
|
|
return True
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(c1(result), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(c2(result), loop=self.loop)
|
|
|
|
t3 = asyncio.Task(c3(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual([1], result)
|
|
|
|
self.assertTrue(sem.locked())
|
|
|
|
self.assertEqual(2, len(sem._waiters))
|
|
|
|
self.assertEqual(0, sem._value)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t4 = asyncio.Task(c4(result), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
sem.release()
|
|
|
|
sem.release()
|
|
|
|
self.assertEqual(2, sem._value)
|
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(0, sem._value)
|
|
|
|
self.assertEqual([1, 2, 3], result)
|
|
|
|
self.assertTrue(sem.locked())
|
|
|
|
self.assertEqual(1, len(sem._waiters))
|
|
|
|
self.assertEqual(0, sem._value)
|
|
|
|
|
|
|
|
self.assertTrue(t1.done())
|
|
|
|
self.assertTrue(t1.result())
|
|
|
|
self.assertTrue(t2.done())
|
|
|
|
self.assertTrue(t2.result())
|
|
|
|
self.assertTrue(t3.done())
|
|
|
|
self.assertTrue(t3.result())
|
|
|
|
self.assertFalse(t4.done())
|
|
|
|
|
|
|
|
# cleanup locked semaphore
|
|
|
|
sem.release()
|
|
|
|
|
|
|
|
def test_acquire_cancel(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(sem.acquire())
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
acquire = asyncio.Task(sem.acquire(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(acquire.cancel)
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError,
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete, acquire)
|
|
|
|
self.assertFalse(sem._waiters)
|
|
|
|
|
|
|
|
def test_release_not_acquired(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.BoundedSemaphore(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(ValueError, sem.release)
|
|
|
|
|
|
|
|
def test_release_no_waiters(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(sem.acquire())
|
|
|
|
self.assertTrue(sem.locked())
|
|
|
|
|
|
|
|
sem.release()
|
|
|
|
self.assertFalse(sem.locked())
|
|
|
|
|
|
|
|
def test_context_manager(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
sem = asyncio.Semaphore(2, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def acquire_lock():
|
|
|
|
return (yield from sem)
|
|
|
|
|
|
|
|
with self.loop.run_until_complete(acquire_lock()):
|
|
|
|
self.assertFalse(sem.locked())
|
|
|
|
self.assertEqual(1, sem._value)
|
|
|
|
|
|
|
|
with self.loop.run_until_complete(acquire_lock()):
|
|
|
|
self.assertTrue(sem.locked())
|
|
|
|
|
|
|
|
self.assertEqual(2, sem._value)
|
|
|
|
|
2014-01-25 20:51:57 -04:00
|
|
|
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)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|