more tests, loop= for more things
This commit is contained in:
parent
25a75afc43
commit
92b064f795
|
@ -83,6 +83,8 @@ def test_create_future_explicit_loop(framework):
|
|||
assert results == []
|
||||
txaio.resolve(f, 'some result')
|
||||
|
||||
# run_once() runs the txaio.config.loop so we shouldn't get any
|
||||
# results until we spin alt_loop
|
||||
assert results == []
|
||||
run_once()
|
||||
assert results == []
|
||||
|
@ -91,6 +93,63 @@ def test_create_future_explicit_loop(framework):
|
|||
assert results == ['some result']
|
||||
|
||||
|
||||
def test_create_future_success_explicit_loop(framework):
|
||||
"""
|
||||
process events on alternate loop= for create_future later
|
||||
"""
|
||||
pytest.importorskip('asyncio')
|
||||
if txaio.using_twisted:
|
||||
pytest.skip()
|
||||
|
||||
import asyncio
|
||||
alt_loop = asyncio.new_event_loop()
|
||||
f = txaio.create_future_success('some result', loop=alt_loop)
|
||||
|
||||
results = []
|
||||
f.add_done_callback(lambda r: results.append(r.result()))
|
||||
|
||||
# run_once() runs the txaio.config.loop so we shouldn't get any
|
||||
# results until we spin alt_loop
|
||||
assert results == []
|
||||
run_once()
|
||||
assert results == []
|
||||
with replace_loop(alt_loop):
|
||||
run_once()
|
||||
assert results == ['some result']
|
||||
|
||||
|
||||
def test_create_future_failure_explicit_loop(framework):
|
||||
"""
|
||||
process events on alternate loop= for create_future later
|
||||
"""
|
||||
pytest.importorskip('asyncio')
|
||||
if txaio.using_twisted:
|
||||
pytest.skip()
|
||||
|
||||
import asyncio
|
||||
alt_loop = asyncio.new_event_loop()
|
||||
the_exception = Exception('bad')
|
||||
f = txaio.create_future_error(the_exception, loop=alt_loop)
|
||||
|
||||
results = []
|
||||
|
||||
def boom(r):
|
||||
try:
|
||||
results.append(r.result())
|
||||
except Exception as e:
|
||||
results.append(e)
|
||||
f.add_done_callback(boom)
|
||||
|
||||
# run_once() runs the txaio.config.loop so we shouldn't get any
|
||||
# results until we spin alt_loop
|
||||
assert results == []
|
||||
run_once()
|
||||
assert results == []
|
||||
with replace_loop(alt_loop):
|
||||
run_once()
|
||||
assert results == [the_exception]
|
||||
|
||||
|
||||
def test_explicit_reactor_coroutine(framework):
|
||||
"""
|
||||
If we set an event-loop, Futures + Tasks should use it.
|
||||
|
@ -149,7 +208,6 @@ def test_call_later_aio(framework_aio):
|
|||
# even though we only do one call, I guess TestLoop needs
|
||||
# a "trailing" yield? "or something"
|
||||
when = yield 0
|
||||
print("Hmmm", when)
|
||||
from asyncio.test_utils import TestLoop
|
||||
new_loop = TestLoop(time_gen)
|
||||
|
||||
|
|
|
@ -25,6 +25,9 @@
|
|||
###############################################################################
|
||||
|
||||
|
||||
import txaio
|
||||
|
||||
|
||||
def run_once():
|
||||
'''
|
||||
A helper that takes one trip through the event-loop to process any
|
||||
|
@ -39,7 +42,7 @@ def run_once():
|
|||
try:
|
||||
import asyncio
|
||||
from asyncio.test_utils import run_once as _run_once
|
||||
return _run_once(asyncio.get_event_loop())
|
||||
return _run_once(txaio.config.loop or asyncio.get_event_loop())
|
||||
|
||||
except ImportError:
|
||||
import trollius as asyncio
|
||||
|
|
10
txaio/aio.py
10
txaio/aio.py
|
@ -281,16 +281,18 @@ def create_future(result=_unspecified, error=_unspecified, loop=None):
|
|||
return f
|
||||
|
||||
|
||||
def create_future_success(result):
|
||||
return create_future(result=result)
|
||||
def create_future_success(result, loop=None):
|
||||
return create_future(result=result, loop=loop)
|
||||
|
||||
|
||||
def create_future_error(error=None):
|
||||
f = create_future()
|
||||
def create_future_error(error=None, loop=None):
|
||||
f = create_future(loop=loop)
|
||||
reject(f, error)
|
||||
return f
|
||||
|
||||
|
||||
# XXX how to pass "loop" arg? could pop it out of kwargs, but .. what
|
||||
# if you're "as_future"-ing a function that itself takes a "loop" arg?
|
||||
def as_future(fun, *args, **kwargs):
|
||||
try:
|
||||
res = fun(*args, **kwargs)
|
||||
|
|
Loading…
Reference in New Issue