本文整理匯總了Python中tornado.test.util.exec_test方法的典型用法代碼示例。如果您正苦於以下問題:Python util.exec_test方法的具體用法?Python util.exec_test怎麽用?Python util.exec_test使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tornado.test.util
的用法示例。
在下文中一共展示了util.exec_test方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_undecorated_coroutine
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_undecorated_coroutine(self):
namespace = exec_test(globals(), locals(), """
class Test(AsyncTestCase):
async def test_coro(self):
pass
""")
test_class = namespace['Test']
test = test_class('test_coro')
result = unittest.TestResult()
# Silence "RuntimeWarning: coroutine 'test_coro' was never awaited".
with warnings.catch_warnings():
warnings.simplefilter('ignore')
test.run(result)
self.assertEqual(len(result.errors), 1)
self.assertIn("should be decorated", result.errors[0][1])
示例2: test_acquire_fifo_async_with
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_acquire_fifo_async_with(self):
# Repeat the above test using `async with lock:`
# instead of `with (yield lock.acquire()):`.
lock = locks.Lock()
self.assertTrue(lock.acquire().done())
N = 5
history = []
namespace = exec_test(globals(), locals(), """
async def f(idx):
async with lock:
history.append(idx)
""")
futures = [namespace['f'](i) for i in range(N)]
lock.release()
yield futures
self.assertEqual(list(range(N)), history)
示例3: test_async_await_mixed_multi_native_yieldpoint
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_async_await_mixed_multi_native_yieldpoint(self):
namespace = exec_test(globals(), locals(), """
async def f1():
await gen.Task(self.io_loop.add_callback)
return 42
""")
@gen.coroutine
def f2():
yield gen.Task(self.io_loop.add_callback)
raise gen.Return(43)
f2(callback=(yield gen.Callback('cb')))
results = yield [namespace['f1'](), gen.Wait('cb')]
self.assertEqual(results, [42, 43])
self.finished = True
示例4: test_native_coroutine
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_native_coroutine(self):
namespace = exec_test(globals(), locals(), """
@gen_test
async def test(self):
self.finished = True
""")
namespace['test'](self)
示例5: test_native_coroutine_timeout
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_native_coroutine_timeout(self):
# Set a short timeout and exceed it.
namespace = exec_test(globals(), locals(), """
@gen_test(timeout=0.1)
async def test(self):
await gen.sleep(1)
""")
try:
namespace['test'](self)
self.fail("did not get expected exception")
except ioloop.TimeoutError:
self.finished = True
示例6: test_context_manager_async_await
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_context_manager_async_await(self):
# Repeat the above test using 'async with'.
sem = locks.Semaphore()
namespace = exec_test(globals(), locals(), """
async def f():
async with sem as yielded:
self.assertTrue(yielded is None)
""")
yield namespace['f']()
# Semaphore was released and can be acquired again.
self.assertTrue(sem.acquire().done())
示例7: test_exception_logging_native_coro
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_exception_logging_native_coro(self):
"""The IOLoop examines exceptions from awaitables and logs them."""
namespace = exec_test(globals(), locals(), """
async def callback():
self.io_loop.add_callback(self.stop)
1 / 0
""")
with NullContext():
self.io_loop.add_callback(namespace["callback"])
with ExpectLog(app_log, "Exception in callback"):
self.wait()
示例8: test_native_coroutine
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_native_coroutine(self):
namespace = exec_test(globals(), locals(), """
async def f():
await gen.Task(self.io_loop.add_callback)
""")
self.io_loop.run_sync(namespace['f'])
示例9: test_async_return
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_async_return(self):
namespace = exec_test(globals(), locals(), """
@gen.coroutine
def f():
yield gen.Task(self.io_loop.add_callback)
return 42
""")
result = yield namespace['f']()
self.assertEqual(result, 42)
self.finished = True
示例10: test_async_early_return
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_async_early_return(self):
# A yield statement exists but is not executed, which means
# this function "returns" via an exception. This exception
# doesn't happen before the exception handling is set up.
namespace = exec_test(globals(), locals(), """
@gen.coroutine
def f():
if True:
return 42
yield gen.Task(self.io_loop.add_callback)
""")
result = yield namespace['f']()
self.assertEqual(result, 42)
self.finished = True
示例11: test_async_await
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_async_await(self):
# This test verifies that an async function can await a
# yield-based gen.coroutine, and that a gen.coroutine
# (the test method itself) can yield an async function.
namespace = exec_test(globals(), locals(), """
async def f():
await gen.Task(self.io_loop.add_callback)
return 42
""")
result = yield namespace['f']()
self.assertEqual(result, 42)
self.finished = True
示例12: test_iterator_async_await
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_iterator_async_await(self):
# Recreate the previous test with py35 syntax. It's a little clunky
# because of the way the previous test handles an exception on
# a single iteration.
futures = [Future(), Future(), Future(), Future()]
self.finish_coroutines(0, futures)
self.finished = False
namespace = exec_test(globals(), locals(), """
async def f():
i = 0
g = gen.WaitIterator(*futures)
try:
async for r in g:
if i == 0:
self.assertEqual(r, 24, 'iterator value incorrect')
self.assertEqual(g.current_index, 2, 'wrong index')
else:
raise Exception("expected exception on iteration 1")
i += 1
except ZeroDivisionError:
i += 1
async for r in g:
if i == 2:
self.assertEqual(r, 42, 'iterator value incorrect')
self.assertEqual(g.current_index, 1, 'wrong index')
elif i == 3:
self.assertEqual(r, 84, 'iterator value incorrect')
self.assertEqual(g.current_index, 3, 'wrong index')
else:
raise Exception("didn't expect iteration %d" % i)
i += 1
self.finished = True
""")
yield namespace['f']()
self.assertTrue(self.finished)
示例13: get_handlers
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def get_handlers(self):
class NativeFlowControlHandler(BaseFlowControlHandler):
data_received = exec_test(globals(), locals(), """
async def data_received(self, data):
with self.in_method('data_received'):
await gen.Task(IOLoop.current().add_callback)
""")["data_received"]
return [('/', NativeFlowControlHandler, dict(test=self))]
示例14: test_asyncio_yield_from
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_asyncio_yield_from(self):
# Test that we can use asyncio coroutines with 'yield from'
# instead of asyncio.async(). This requires python 3.3 syntax.
namespace = exec_test(globals(), locals(), """
@gen.coroutine
def f():
event_loop = asyncio.get_event_loop()
x = yield from event_loop.run_in_executor(None, lambda: 42)
return x
""")
result = yield namespace['f']()
self.assertEqual(result, 42)
示例15: test_native_body_producer_content_length
# 需要導入模塊: from tornado.test import util [as 別名]
# 或者: from tornado.test.util import exec_test [as 別名]
def test_native_body_producer_content_length(self):
namespace = exec_test(globals(), locals(), """
async def body_producer(write):
await write(b'1234')
await gen.Task(IOLoop.current().add_callback)
await write(b'5678')
""")
response = self.fetch("/echo_post", method="POST",
body_producer=namespace["body_producer"],
headers={'Content-Length': '8'})
response.rethrow()
self.assertEqual(response.body, b"12345678")