]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add testcases. 1406/head
authorWei-Cheng Pan <legnaleurc@gmail.com>
Thu, 2 Apr 2015 14:22:36 +0000 (23:22 +0900)
committerWei-Cheng Pan <legnaleurc@gmail.com>
Thu, 2 Apr 2015 14:22:36 +0000 (23:22 +0900)
tornado/test/concurrent_test.py

index 5e93ad6a42ba5fd55a9f100a3fbd448085088e89..98b13a11c9aa4448c248c5ab959eaf5dc52f2a8e 100644 (file)
@@ -21,13 +21,14 @@ import socket
 import sys
 import traceback
 
-from tornado.concurrent import Future, return_future, ReturnValueIgnoredError
+from tornado.concurrent import Future, return_future, ReturnValueIgnoredError, run_on_executor
 from tornado.escape import utf8, to_unicode
 from tornado import gen
 from tornado.iostream import IOStream
 from tornado import stack_context
 from tornado.tcpserver import TCPServer
 from tornado.testing import AsyncTestCase, LogTrapTestCase, bind_unused_port, gen_test
+from tornado.test.util import unittest
 
 
 try:
@@ -334,3 +335,81 @@ class DecoratorClientTest(ClientTestMixin, AsyncTestCase, LogTrapTestCase):
 
 class GeneratorClientTest(ClientTestMixin, AsyncTestCase, LogTrapTestCase):
     client_class = GeneratorCapClient
+
+
+@unittest.skipIf(futures is None, "concurrent.futures module not present")
+class RunOnExecutorTest(AsyncTestCase):
+    @gen_test
+    def test_no_calling(self):
+        class Object(object):
+            def __init__(self, io_loop):
+                self.io_loop = io_loop
+                self.executor = futures.thread.ThreadPoolExecutor(1)
+
+            @run_on_executor
+            def f(self):
+                return 42
+
+        o = Object(io_loop=self.io_loop)
+        anwser = yield o.f()
+        self.assertEqual(anwser, 42)
+
+    @gen_test
+    def test_call_with_no_args(self):
+        class Object(object):
+            def __init__(self, io_loop):
+                self.io_loop = io_loop
+                self.executor = futures.thread.ThreadPoolExecutor(1)
+
+            @run_on_executor()
+            def f(self):
+                return 42
+
+        o = Object(io_loop=self.io_loop)
+        anwser = yield o.f()
+        self.assertEqual(anwser, 42)
+
+    @gen_test
+    def test_call_with_io_loop(self):
+        class Object(object):
+            def __init__(self, io_loop):
+                self._io_loop = io_loop
+                self.executor = futures.thread.ThreadPoolExecutor(1)
+
+            @run_on_executor(io_loop='_io_loop')
+            def f(self):
+                return 42
+
+        o = Object(io_loop=self.io_loop)
+        anwser = yield o.f()
+        self.assertEqual(anwser, 42)
+
+    @gen_test
+    def test_call_with_executor(self):
+        class Object(object):
+            def __init__(self, io_loop):
+                self.io_loop = io_loop
+                self.__executor = futures.thread.ThreadPoolExecutor(1)
+
+            @run_on_executor(executor='_Object__executor')
+            def f(self):
+                return 42
+
+        o = Object(io_loop=self.io_loop)
+        anwser = yield o.f()
+        self.assertEqual(anwser, 42)
+
+    @gen_test
+    def test_call_with_both(self):
+        class Object(object):
+            def __init__(self, io_loop):
+                self._io_loop = io_loop
+                self.__executor = futures.thread.ThreadPoolExecutor(1)
+
+            @run_on_executor(io_loop='_io_loop', executor='_Object__executor')
+            def f(self):
+                return 42
+
+        o = Object(io_loop=self.io_loop)
+        anwser = yield o.f()
+        self.assertEqual(anwser, 42)