Search code examples
djangounit-testingasynchronoustornado

Testing asynchronous capabilities of a django + tornado application


I'm using django-on-tornado to build an application that is similar to the chat applicatoin proposed. All tutorials are focused on how to run a django application over tornado server, but how can I test an asynchronous feature that depends on tornado?

My current test does the following:

  • Starts a thread that sleeps for some time than sends a chat message
  • Do a request to ask for messages
  • When request ends, check that message arrived and that time elapsed is compatible with thread sleep time

When I run the test (with manage.py test), I get an "AttributeError: 'WSGIRequest' object has no attribute '_tornado_handler'", which is expected, since the _tornado_handler property of the request is set in runtornado command.

Is there a way to make this setup so that I can test the asynchronous feature? I use nose with django_nose plugin for tests.


Solution

  • Actually django-on-tornado does not anyhow change the manage.py test command of Django, so the Tornado is invoked only via runtornado. You will need to add command to manage.py called something like "testtornado" with implementation similar to https://github.com/koblas/django-on-tornado/blob/master/myproject/django_tornado/management/commands/runtornado.py - it should set up _tornado_handler and proceed with launching your test code.