I'm interested in my Tornado handlers not blocking, so I would like to write some unit tests as a health check.
I mean a handler that sleeps asynchronously for 2 seconds. In the test, I want to call this handler twice to simulate "simultaneous" requests.
If I am not mistaken, both of these requests should work simultaneously and, thus, be completed in less than 4 seconds. The problem is that I'm not sure how to make 2 simultaneous requests in my application through AsyncHTTPTestCase
.
Here is what I still have:
class SyncSleepHandler(tornado.web.RequestHandler):
def get(self):
time.sleep(2)
class AsyncSleepHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
yield gen.sleep(2)
class SleepTest(AsyncHTTPTestCase):
def get_app(self):
return Application([(r'/sync', SyncSleepHandler),
(r'/async', AsyncSleepHandler)], debug=True)
def test_async_sleep(self):
start = time.time()
resp1 = self.fetch(r'/async', method='GET')
resp2 = self.fetch(r'/async', method='GET')
diff = time.time() - start
self.assertTrue(2 < diff < 4, msg="Difference is {:}".format(diff))
source
share