From: Ben Darnell Date: Wed, 29 Sep 2010 03:11:42 +0000 (-0700) Subject: Handle IOStream.read_bytes(0) gracefully. X-Git-Tag: v1.2.0~114 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75bfe842c5afe8945ee0635ac431b27de6202f6d;p=thirdparty%2Ftornado.git Handle IOStream.read_bytes(0) gracefully. --- diff --git a/tornado/iostream.py b/tornado/iostream.py index 361955487..fe15134af 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -99,6 +99,9 @@ class IOStream(object): def read_bytes(self, num_bytes, callback): """Call callback when we read the given number of bytes.""" assert not self._read_callback, "Already reading" + if num_bytes == 0: + callback("") + return self._read_bytes = num_bytes self._read_callback = callback while True: diff --git a/tornado/test/iostream_test.py b/tornado/test/iostream_test.py new file mode 100644 index 000000000..8e0d64f5c --- /dev/null +++ b/tornado/test/iostream_test.py @@ -0,0 +1,33 @@ +from tornado.iostream import IOStream +from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase +from tornado.web import RequestHandler, Application +import socket + +class HelloHandler(RequestHandler): + def get(self): + self.write("Hello") + +class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase): + def get_app(self): + return Application([('/', HelloHandler)]) + + def test_read_zero_bytes(self): + 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("GET / HTTP/1.0\r\n\r\n") + + # normal read + self.stream.read_bytes(9, self.stop) + data = self.wait() + self.assertEqual(data, "HTTP/1.0 ") + + # zero bytes + self.stream.read_bytes(0, self.stop) + data = self.wait() + self.assertEqual(data, "") + + # another normal read + self.stream.read_bytes(3, self.stop) + data = self.wait() + self.assertEqual(data, "200") diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index 34db64d2f..f66199318 100755 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -5,6 +5,7 @@ TEST_MODULES = [ 'tornado.httputil.doctests', 'tornado.test.httpserver_test', 'tornado.test.ioloop_test', + 'tornado.test.iostream_test', 'tornado.test.stack_context_test', 'tornado.test.testing_test', 'tornado.test.web_test',