Search code examples
unit-testingasynchronoustornado

tornado blocked while testing asynchronous method using AsyncHttpTastCase


i'm new to python and tornado, and trying to write an unit test for a GET method with @tornado.web.asynchronous. But it allways blocked and output the following error message:

Failure
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 327, in run
    testMethod()
File "/Developer/PycharmProjects/CanMusic/tornadotestcase.py", line 19, in test_gen
    response = self.fetch(r'/gen')
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 265, in fetch
    return self.wait()
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 205, in wait
    self.__rethrow()
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 149, in __rethrow
    raise_exc_info(failure)
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 186, in timeout_func
    timeout)
AssertionError: Async operation timed out after 5 seconds

I write the following code as an example. Run it as an unit test (nose), get error above. while run it as a standalone application and access the url via browser, it works fine. I also try the callback version asynchronous (without @tornado.gen.engine and yield), that results the same.

Why? Did i miss something?

import tornado.web, tornado.gen, tornado.httpclient, tornado.testing

class GenHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http = tornado.httpclient.AsyncHTTPClient()
        response = yield tornado.gen.Task(http.fetch, 'http://www.google.com')
        self.finish()

# code for run nose test
class GenTestCase(tornado.testing.AsyncHTTPTestCase):
    def get_app(self):
        return tornado.web.Application([(r'/gen', GenHandler)])
    def test_gen(self):
        response = self.fetch(r'/gen')
        assert response.code == 200

# code for run standalone
application = tornado.web.Application([(r'/gen', GenHandler),])
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Solution

  • What is happening is the AsyncHTTPClient in your RequestHandler is running on a different IOLoop, which is never started.

    You can get around this by overriding get_new_ioloop in your test case:

    def get_new_ioloop(self):
        return tornado.ioloop.IOLoop.instance()
    

    This is a bit strange, but it occurs because each AsyncTestCase/AsyncHTTPTestCase method generates it's own IOLoop, for greater isolation.