Permalink
Browse files

Add gen.YieldFuture.

  • Loading branch information...
1 parent cd8862d commit 5587a45eb2096891f45c52e2f4efc413849bcbab @bdarnell bdarnell committed Sep 3, 2012
Showing with 39 additions and 0 deletions.
  1. +19 −0 tornado/gen.py
  2. +20 −0 tornado/test/concurrent_test.py
View
@@ -69,6 +69,7 @@ def get(self):
import sys
import types
+from tornado.ioloop import IOLoop
from tornado.stack_context import ExceptionStackContext
@@ -247,6 +248,24 @@ def get_result(self):
return self.runner.pop_result(self.key)
+class YieldFuture(YieldPoint):
+ def __init__(self, future, io_loop=None):
+ self.future = future
+ self.io_loop = io_loop or IOLoop.instance()
+
+ def start(self, runner):
+ self.runner = runner
+ self.key = object()
+ runner.register_callback(self.key)
+ self.io_loop.add_future(self.future, runner.result_callback(self.key))
+
+ def is_ready(self):
+ return self.runner.is_ready(self.key)
+
+ def get_result(self):
+ return self.runner.pop_result(self.key).result()
+
+
class Multi(YieldPoint):
"""Runs multiple asynchronous operations in parallel.
@@ -158,6 +158,26 @@ def test_future_error(self):
self.wait()
self.assertRaisesRegexp(CapError, "already capitalized", future.result)
+ def test_generator(self):
+ @gen.engine
+ def f():
+ result = yield gen.YieldFuture(self.client.capitalize("hello"),
+ io_loop=self.io_loop)
+ self.assertEqual(result, "HELLO")
+ self.stop()
+ f()
+ self.wait()
+
+ def test_generator_error(self):
+ @gen.engine
+ def f():
+ with self.assertRaisesRegexp(CapError, "already capitalized"):
+ yield gen.YieldFuture(self.client.capitalize("HELLO"),
+ io_loop=self.io_loop)
+ self.stop()
+ f()
+ self.wait()
+
class ManualClientTest(ClientTestMixin, AsyncTestCase, LogTrapTestCase):
client_class = ManualCapClient

0 comments on commit 5587a45

Please sign in to comment.