Skip to content

Commit 9c8489f

Browse files
committed
Add tests for _chain_future (threough _wrap_future)
1 parent 525cca2 commit 9c8489f

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

tests/test_futures.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,43 @@ def test_future_exception_never_retrieved(self):
317317
def test_future_exception_never_retrieved_debug(self):
318318
self.check_future_exception_never_retrieved(True)
319319

320-
def test_future_set_result_unless_cancelled(self):
321-
from asyncio import futures
322-
fut = self.create_future()
323-
fut.cancel()
324-
futures._set_result_unless_cancelled(fut, 2)
325-
self.assertTrue(fut.cancelled())
320+
def test_future_wrap_future(self):
321+
from uvloop.loop import _wrap_future
322+
def run(arg):
323+
return (arg, threading.get_ident())
324+
ex = concurrent.futures.ThreadPoolExecutor(1)
325+
f1 = ex.submit(run, 'oi')
326+
f2 = _wrap_future(f1, loop=self.loop)
327+
res, ident = self.loop.run_until_complete(f2)
328+
self.assertIsInstance(f2, asyncio.Future)
329+
self.assertEqual(res, 'oi')
330+
self.assertNotEqual(ident, threading.get_ident())
331+
332+
def test_future_wrap_future_future(self):
333+
from uvloop.loop import _wrap_future
334+
f1 = self.create_future()
335+
f2 = _wrap_future(f1)
336+
self.assertIs(f1, f2)
337+
338+
def test_future_wrap_future_cancel(self):
339+
from uvloop.loop import _wrap_future
340+
f1 = concurrent.futures.Future()
341+
f2 = _wrap_future(f1, loop=self.loop)
342+
f2.cancel()
343+
test_utils.run_briefly(self.loop)
344+
self.assertTrue(f1.cancelled())
345+
self.assertTrue(f2.cancelled())
346+
347+
def test_future_wrap_future_cancel2(self):
348+
from uvloop.loop import _wrap_future
349+
f1 = concurrent.futures.Future()
350+
f2 = _wrap_future(f1, loop=self.loop)
351+
f1.set_result(42)
352+
f2.cancel()
353+
test_utils.run_briefly(self.loop)
354+
self.assertFalse(f1.cancelled())
355+
self.assertEqual(f1.result(), 42)
356+
self.assertTrue(f2.cancelled())
326357

327358

328359
class _TestFuturesDoneCallbacks:

uvloop/chain_futs.pyx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Copied from asyncio 3.5.2
1+
# Copied from asyncio 3.5.2. Remove this file when we don't need
2+
# to support earlier versions.
23

34
cdef _set_concurrent_future_state(concurrent, source):
45
"""Copy state from a future to a concurrent.futures.Future."""
@@ -86,3 +87,23 @@ cdef _chain_future(source, destination):
8687

8788
destination.add_done_callback(_call_check_cancel)
8889
source.add_done_callback(_call_set_state)
90+
91+
92+
def _wrap_future(future, *, loop=None):
93+
# Don't use this function -- it's here for tests purposes only
94+
# and can be removed in future versions of uvloop.
95+
96+
if isinstance(future, aio_Future):
97+
return future
98+
assert isinstance(future, cc_Future), \
99+
'concurrent.futures.Future is expected, got {!r}'.format(future)
100+
if loop is None:
101+
loop = aio_get_event_loop()
102+
try:
103+
create_future = loop.create_future
104+
except AttributeError:
105+
new_future = aio_Future(loop=loop)
106+
else:
107+
new_future = create_future()
108+
_chain_future(future, new_future)
109+
return new_future

0 commit comments

Comments
 (0)