]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Handle IOStream.read_bytes(0) gracefully.
authorBen Darnell <ben@bendarnell.com>
Wed, 29 Sep 2010 03:11:42 +0000 (20:11 -0700)
committerBen Darnell <ben@bendarnell.com>
Wed, 29 Sep 2010 03:11:42 +0000 (20:11 -0700)
tornado/iostream.py
tornado/test/iostream_test.py [new file with mode: 0644]
tornado/test/runtests.py

index 361955487bbc2b9c08e6b8e851fcd3c369fe6978..fe15134af338df61e9f457ca6fe839469858f918 100644 (file)
@@ -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 (file)
index 0000000..8e0d64f
--- /dev/null
@@ -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")
index 34db64d2fd06e32a7da912e27d445aa026eed81e..f66199318ee75afdaa298efafeff1ea6af434f6c 100755 (executable)
@@ -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',