Split test_tasks.py in two parts
* Add a new test_asyncio_trollius.py file to test asyncio and trollius interoperability * Move task tests for trollius into test_tasks_trollius.py * Remove unused "import socket" in test_socket.py
This commit is contained in:
parent
78abe7cc41
commit
cbefa18887
|
@ -102,8 +102,6 @@ def load_modules(basedir, suffix='.py'):
|
|||
try:
|
||||
mod = load_module(modname, sourcefile)
|
||||
mods.append((mod, sourcefile))
|
||||
except SyntaxError:
|
||||
raise
|
||||
except Exception as err:
|
||||
print("Skipping '{}': {}".format(modname, err), file=sys.stderr)
|
||||
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
"""
|
||||
Test interoperability between asyncio and trollius. Trollius event loop should
|
||||
support asyncio coroutines.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import greenio
|
||||
import trollius
|
||||
import unittest
|
||||
from trollius import From, Return
|
||||
from trollius.test_utils import TestCase
|
||||
|
||||
|
||||
class TrolliusEventLoopTests(TestCase):
|
||||
def setUp(self):
|
||||
policy = greenio.GreenTrolliusEventLoopPolicy()
|
||||
asyncio.set_event_loop_policy(policy)
|
||||
trollius.set_event_loop_policy(policy)
|
||||
self.loop = trollius.new_event_loop()
|
||||
asyncio.set_event_loop(self.loop)
|
||||
trollius.set_event_loop(self.loop)
|
||||
|
||||
def tearDown(self):
|
||||
self.loop.close()
|
||||
asyncio.set_event_loop_policy(None)
|
||||
|
||||
def test_trollius_coroutine(self):
|
||||
@trollius.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
raise Return(30)
|
||||
|
||||
@trollius.coroutine
|
||||
def foo():
|
||||
bar_result = greenio.yield_from(bar())
|
||||
return bar_result + 12
|
||||
|
||||
@greenio.task
|
||||
def test():
|
||||
res = yield From(foo())
|
||||
raise Return(res)
|
||||
|
||||
fut = test()
|
||||
self.loop.run_until_complete(fut)
|
||||
|
||||
self.assertEqual(fut.result(), 42)
|
||||
|
||||
def test_asyncio_coroutine(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield from []
|
||||
return 30
|
||||
|
||||
@asyncio.coroutine
|
||||
def foo():
|
||||
bar_result = greenio.yield_from(bar())
|
||||
return bar_result + 12
|
||||
|
||||
@greenio.task
|
||||
def test():
|
||||
res = yield From(foo())
|
||||
raise Return(res)
|
||||
|
||||
fut = test()
|
||||
self.loop.run_until_complete(fut)
|
||||
|
||||
self.assertEqual(fut.result(), 42)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -10,7 +10,6 @@ try:
|
|||
except ImportError:
|
||||
import trollius as asyncio
|
||||
from trollius.test_utils import TestCase
|
||||
import socket
|
||||
|
||||
import greenio
|
||||
import greenio.socket as greensocket
|
||||
|
|
|
@ -5,15 +5,8 @@
|
|||
|
||||
|
||||
import greenio
|
||||
try:
|
||||
trollius = None
|
||||
import asyncio
|
||||
from unittest import TestCase
|
||||
except ImportError:
|
||||
import trollius
|
||||
from trollius import From, Return
|
||||
asyncio = trollius
|
||||
from trollius.test_utils import TestCase
|
||||
import asyncio
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class TaskTests(TestCase):
|
||||
|
@ -26,124 +19,62 @@ class TaskTests(TestCase):
|
|||
self.loop.close()
|
||||
asyncio.set_event_loop_policy(None)
|
||||
|
||||
if trollius is not None:
|
||||
def test_task_yield_from_plain(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
raise Return(30)
|
||||
def test_task_yield_from_plain(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield from []
|
||||
return 30
|
||||
|
||||
@asyncio.coroutine
|
||||
def foo():
|
||||
bar_result = greenio.yield_from(bar())
|
||||
return bar_result + 12
|
||||
@asyncio.coroutine
|
||||
def foo():
|
||||
bar_result = greenio.yield_from(bar())
|
||||
return bar_result + 12
|
||||
|
||||
@greenio.task
|
||||
def test():
|
||||
res = yield From(foo())
|
||||
raise Return(res)
|
||||
@greenio.task
|
||||
def test():
|
||||
return (yield from foo())
|
||||
|
||||
fut = test()
|
||||
self.loop.run_until_complete(fut)
|
||||
fut = test()
|
||||
self.loop.run_until_complete(fut)
|
||||
|
||||
self.assertEqual(fut.result(), 42)
|
||||
self.assertEqual(fut.result(), 42)
|
||||
|
||||
def test_task_yield_from_exception_propagation(self):
|
||||
non_local = {'CHK': 0}
|
||||
def test_task_yield_from_exception_propagation(self):
|
||||
non_local = {'CHK': 0}
|
||||
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
yield From(None)
|
||||
1/0
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield
|
||||
yield
|
||||
1/0
|
||||
|
||||
@greenio.task
|
||||
def foo():
|
||||
greenio.yield_from(bar())
|
||||
@greenio.task
|
||||
def foo():
|
||||
greenio.yield_from(bar())
|
||||
|
||||
@asyncio.coroutine
|
||||
def test():
|
||||
try:
|
||||
res = yield From(foo())
|
||||
raise Return(res)
|
||||
except ZeroDivisionError:
|
||||
non_local['CHK'] += 1
|
||||
|
||||
self.loop.run_until_complete(test())
|
||||
self.assertEqual(non_local['CHK'], 1)
|
||||
|
||||
def test_task_yield_from_coroutine(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
raise Return(5)
|
||||
|
||||
@greenio.task
|
||||
def foo():
|
||||
return greenio.yield_from(bar())
|
||||
|
||||
fut = foo()
|
||||
self.loop.run_until_complete(fut)
|
||||
self.assertEqual(fut.result(), 5)
|
||||
else:
|
||||
exec('''if 1:
|
||||
def test_task_yield_from_plain(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield from []
|
||||
return 30
|
||||
|
||||
@asyncio.coroutine
|
||||
def foo():
|
||||
bar_result = greenio.yield_from(bar())
|
||||
return bar_result + 12
|
||||
|
||||
@greenio.task
|
||||
def test():
|
||||
@asyncio.coroutine
|
||||
def test():
|
||||
try:
|
||||
return (yield from foo())
|
||||
except ZeroDivisionError:
|
||||
non_local['CHK'] += 1
|
||||
|
||||
fut = test()
|
||||
self.loop.run_until_complete(fut)
|
||||
self.loop.run_until_complete(test())
|
||||
self.assertEqual(non_local['CHK'], 1)
|
||||
|
||||
self.assertEqual(fut.result(), 42)
|
||||
def test_task_yield_from_coroutine(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield from []
|
||||
return 5
|
||||
|
||||
def test_task_yield_from_exception_propagation(self):
|
||||
non_local = {'CHK': 0}
|
||||
@greenio.task
|
||||
def foo():
|
||||
return greenio.yield_from(bar())
|
||||
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield
|
||||
yield
|
||||
1/0
|
||||
|
||||
@greenio.task
|
||||
def foo():
|
||||
greenio.yield_from(bar())
|
||||
|
||||
@asyncio.coroutine
|
||||
def test():
|
||||
try:
|
||||
return (yield from foo())
|
||||
except ZeroDivisionError:
|
||||
non_local['CHK'] += 1
|
||||
|
||||
self.loop.run_until_complete(test())
|
||||
self.assertEqual(non_local['CHK'], 1)
|
||||
|
||||
def test_task_yield_from_coroutine(self):
|
||||
@asyncio.coroutine
|
||||
def bar():
|
||||
yield from []
|
||||
return 5
|
||||
|
||||
@greenio.task
|
||||
def foo():
|
||||
return greenio.yield_from(bar())
|
||||
|
||||
fut = foo()
|
||||
self.loop.run_until_complete(fut)
|
||||
self.assertEqual(fut.result(), 5)
|
||||
''')
|
||||
fut = foo()
|
||||
self.loop.run_until_complete(fut)
|
||||
self.assertEqual(fut.result(), 5)
|
||||
|
||||
def test_task_yield_from_invalid(self):
|
||||
def bar():
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
##
|
||||
# Copyright (c) 2013 Yury Selivanov
|
||||
# License: Apache 2.0
|
||||
##
|
||||
|
||||
|
||||
from trollius import From, Return
|
||||
from trollius.test_utils import TestCase
|
||||
import greenio
|
||||
import trollius
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
asyncio = None
|
||||
|
||||
|
||||
class TrolliusTaskTests(TestCase):
|
||||
def setUp(self):
|
||||
trollius.set_event_loop_policy(greenio.GreenTrolliusEventLoopPolicy())
|
||||
self.loop = trollius.new_event_loop()
|
||||
trollius.set_event_loop(self.loop)
|
||||
if asyncio is not None:
|
||||
asyncio.set_event_loop(self.loop)
|
||||
|
||||
def tearDown(self):
|
||||
self.loop.close()
|
||||
trollius.set_event_loop_policy(None)
|
||||
|
||||
def test_task_yield_from_plain(self):
|
||||
@trollius.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
raise Return(30)
|
||||
|
||||
@trollius.coroutine
|
||||
def foo():
|
||||
bar_result = greenio.yield_from(bar())
|
||||
return bar_result + 12
|
||||
|
||||
@greenio.task
|
||||
def test():
|
||||
res = yield From(foo())
|
||||
raise Return(res)
|
||||
|
||||
fut = test()
|
||||
self.loop.run_until_complete(fut)
|
||||
|
||||
self.assertEqual(fut.result(), 42)
|
||||
|
||||
def test_task_yield_from_exception_propagation(self):
|
||||
non_local = {'CHK': 0}
|
||||
|
||||
@trollius.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
yield From(None)
|
||||
1/0
|
||||
|
||||
@greenio.task
|
||||
def foo():
|
||||
greenio.yield_from(bar())
|
||||
|
||||
@trollius.coroutine
|
||||
def test():
|
||||
try:
|
||||
res = yield From(foo())
|
||||
raise Return(res)
|
||||
except ZeroDivisionError:
|
||||
non_local['CHK'] += 1
|
||||
|
||||
self.loop.run_until_complete(test())
|
||||
self.assertEqual(non_local['CHK'], 1)
|
||||
|
||||
def test_task_yield_from_coroutine(self):
|
||||
@trollius.coroutine
|
||||
def bar():
|
||||
yield From(None)
|
||||
raise Return(5)
|
||||
|
||||
@greenio.task
|
||||
def foo():
|
||||
return greenio.yield_from(bar())
|
||||
|
||||
fut = foo()
|
||||
self.loop.run_until_complete(fut)
|
||||
self.assertEqual(fut.result(), 5)
|
||||
|
||||
def test_task_yield_from_invalid(self):
|
||||
def bar():
|
||||
pass
|
||||
|
||||
err_msg = (r"^greenlet.yield_from was supposed to receive "
|
||||
r"only Futures, got .* in task .*$")
|
||||
|
||||
@trollius.coroutine
|
||||
def foo():
|
||||
with self.assertRaisesRegex(RuntimeError, err_msg):
|
||||
greenio.yield_from(bar)
|
||||
|
||||
self.loop.run_until_complete(foo())
|
Loading…
Reference in New Issue