Permalink
Browse files

Added tests for async prepare

  • Loading branch information...
1 parent 29b2cfe commit e575b46c45f8a72f4f4b8956878ddac0b24560f6 Adria Casajus committed Oct 9, 2012
Showing with 65 additions and 0 deletions.
  1. +65 −0 tornado/test/web_test.py
View
@@ -953,3 +953,68 @@ def test_raise_with_reason(self):
def test_httperror_str(self):
self.assertEqual(str(HTTPError(682, reason="Foo")), "HTTP 682: Foo")
wsgi_safe.append(RaiseWithReasonTest)
+
+
+
+class BasePrepareTestHandler(RequestHandler):
+
+ def initialize( self, test ):
+ self.test = test
+
+ def on_finish(self):
+ self.test.on_finish()
+
+class AsyncPrepareAsyncHandler(BasePrepareTestHandler):
+
+ @asynchronous
+ def prepare( self ):
+ self.flush(callback=self.end_prepare)
+
+ @asynchronous
+ def get(self):
+ self.write( "hello" )
+ self.flush(callback=self.finish)
+
+class AsyncPrepareSyncHandler(BasePrepareTestHandler):
+
+ @asynchronous
+ def prepare( self ):
+ self.set_header( "X-Test", "hello" )
+ self.flush(callback=self.end_prepare)
+
+ def get(self):
+ self.write( "hello" )
+
+class SyncPrepareAsyncHandler(BasePrepareTestHandler):
+
+ @asynchronous
+ def get(self):
+ self.write( "hello" )
+ self.flush(callback=self.finish)
+
+class SyncPrepareSyncHandler(BasePrepareTestHandler):
+
+ def get(self):
+ self.write( "hello" )
+
+class AsyncPrepareTest(WebTestCase):
+
+ def get_handlers(self):
+ return [('/aa', AsyncPrepareAsyncHandler, dict(test=self)),
+ ('/as', AsyncPrepareSyncHandler, dict(test=self)),
+ ('/sa', SyncPrepareAsyncHandler, dict(test=self)),
+ ('/ss', SyncPrepareSyncHandler, dict(test=self))]
+
+ def test_async(self):
+ for url in ( '/aa', '/as', '/sa', '/ss' ):
+ logging.debug( "Trying connection to %s" % url )
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
+ s.connect(("localhost", self.get_http_port()))
+ self.stream = IOStream(s, io_loop=self.io_loop)
+ self.stream.write(b("GET %s HTTP/1.0\r\n\r\n") % url)
+ self.wait()
+
+ def on_finish(self):
+ logging.debug('connection closed')
+ self.stream.close()
+ self.stop()

0 comments on commit e575b46

Please sign in to comment.