return (yield from _wait(fs, timeout, return_when, loop))
-def _release_waiter(waiter, value=True, *args):
+def _release_waiter(waiter, *args):
if not waiter.done():
- waiter.set_result(value)
+ waiter.set_result(None)
@coroutine
return (yield from fut)
waiter = futures.Future(loop=loop)
- timeout_handle = loop.call_later(timeout, _release_waiter, waiter, False)
- cb = functools.partial(_release_waiter, waiter, True)
+ timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
+ cb = functools.partial(_release_waiter, waiter)
fut = async(fut, loop=loop)
fut.add_done_callback(cb)
try:
- if (yield from waiter):
+ # wait until the future completes or the timeout
+ yield from waiter
+
+ if fut.done():
return fut.result()
else:
fut.remove_done_callback(cb)
if timeout_handle is not None:
timeout_handle.cancel()
if not waiter.done():
- waiter.set_result(False)
+ waiter.set_result(None)
for f in fs:
f.add_done_callback(_on_completion)
self.assertTrue(fut.done())
self.assertTrue(fut.cancelled())
+ def test_wait_for_race_condition(self):
+
+ def gen():
+ yield 0.1
+ yield 0.1
+ yield 0.1
+
+ loop = self.new_test_loop(gen)
+
+ fut = asyncio.Future(loop=loop)
+ task = asyncio.wait_for(fut, timeout=0.2, loop=loop)
+ loop.call_later(0.1, fut.set_result, "ok")
+ res = loop.run_until_complete(task)
+ self.assertEqual(res, "ok")
+
def test_wait(self):
def gen():