]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Always close sockets in simple_httpclient instead of leaving them for the GC.
authorBen Darnell <ben@bendarnell.com>
Wed, 1 Feb 2012 17:17:03 +0000 (09:17 -0800)
committerBen Darnell <ben@bendarnell.com>
Wed, 1 Feb 2012 17:19:31 +0000 (09:19 -0800)
Also fix some other sockets left open in tests.

Closes #451.

tornado/simple_httpclient.py
tornado/test/httpserver_test.py
tornado/test/iostream_test.py
tornado/test/simple_httpclient_test.py

index 376d410c56fc3f3e2bed6fc36b61f6c229d071e4..c301295a5011f093ee9fc885007063b9541b7549 100644 (file)
@@ -250,6 +250,8 @@ class _HTTPConnection(object):
                     'proxy_username', 'proxy_password'):
             if getattr(self.request, key, None):
                 raise NotImplementedError('%s not supported' % key)
+        if "Connection" not in self.request.headers:
+            self.request.headers["Connection"] = "close"
         if "Host" not in self.request.headers:
             self.request.headers["Host"] = parsed.netloc
         username, password = None, None
@@ -313,6 +315,8 @@ class _HTTPConnection(object):
             self._run_callback(HTTPResponse(self.request, 599, error=e,
                                 request_time=time.time() - self.start_time,
                                 ))
+            if hasattr(self, "stream"):
+                self.stream.close()
 
     def _on_close(self):
         self._run_callback(HTTPResponse(
index 1a53a34fd6103bbe5ac8094ec262bda5b5c34844..132af561b3cdb0529da21660ddf8b6f5512f5047 100644 (file)
@@ -217,9 +217,10 @@ class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase):
         stream.connect(("localhost", self.get_http_port()), callback=self.stop)
         self.wait()
         stream.write(b("\r\n").join([b("POST /hello HTTP/1.1"),
-                                  b("Content-Length: 1024"),
-                                  b("Expect: 100-continue"),
-                                  b("\r\n")]), callback=self.stop)
+                                     b("Content-Length: 1024"),
+                                     b("Expect: 100-continue"),
+                                     b("Connection: close"),
+                                     b("\r\n")]), callback=self.stop)
         self.wait()
         stream.read_until(b("\r\n\r\n"), self.stop)
         data = self.wait()
@@ -234,6 +235,7 @@ class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase):
         stream.read_bytes(int(headers["Content-Length"]), self.stop)
         body = self.wait()
         self.assertEqual(body, b("Got 1024 bytes in POST"))
+        stream.close()
 
 class EchoHandler(RequestHandler):
     def get(self):
@@ -368,6 +370,8 @@ class UnixSocketTest(AsyncTestCase, LogTrapTestCase):
         stream.read_bytes(int(headers["Content-Length"]), self.stop)
         body = self.wait()
         self.assertEqual(body, b("Hello world"))
+        stream.close()
+        server.stop()
 
 if not hasattr(socket, 'AF_UNIX') or sys.platform == 'cygwin':
     del UnixSocketTest
index 43b2e17bcd94e7d8998201340d451c1839a25933..895a3de953aee949497f74d4aff26cf4bb289853 100644 (file)
@@ -58,6 +58,8 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         data = self.wait()
         self.assertEqual(data, b("200"))
 
+        s.close()
+
     def test_write_zero_bytes(self):
         # Attempting to write zero bytes should run the callback without
         # going into an infinite loop.
@@ -67,6 +69,8 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         # As a side effect, the stream is now listening for connection
         # close (if it wasn't already), but is not listening for writes
         self.assertEqual(server._state, IOLoop.READ|IOLoop.ERROR)
+        server.close()
+        client.close()
 
     def test_connection_refused(self):
         # When a connection is refused, the connect callback should not
index ebb265b916b9943d20203b0af89f56686e2ccb70..030876beb8308bc182e89ec65f9297c63463db62 100644 (file)
@@ -32,7 +32,8 @@ class TriggerHandler(RequestHandler):
     def get(self):
         logging.info("queuing trigger")
         self.queue.append(self.finish)
-        self.wake_callback()
+        if self.get_argument("wake", "true") == "true":
+            self.wake_callback()
 
 class HangHandler(RequestHandler):
     @asynchronous
@@ -173,10 +174,12 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase):
        self.assertEqual("POST", response.request.method)
 
     def test_request_timeout(self):
-        response = self.fetch('/hang', request_timeout=0.1)
+        response = self.fetch('/trigger?wake=false', request_timeout=0.1)
         self.assertEqual(response.code, 599)
         self.assertTrue(0.099 < response.request_time < 0.11, response.request_time)
         self.assertEqual(str(response.error), "HTTP 599: Timeout")
+        # trigger the hanging request to let it clean up after itself
+        self.triggers.popleft()()
 
     def test_ipv6(self):
         if not socket.has_ipv6: