]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix warnings during final GC of the test suite in python 3.4a1.
authorBen Darnell <ben@bendarnell.com>
Sun, 8 Sep 2013 18:02:21 +0000 (14:02 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 8 Sep 2013 18:02:21 +0000 (14:02 -0400)
3.4 can GC things that were uncollectable before, like abandoned generators.
It also seems generates ResourceWarnings for unclosed files in places
where older versions did not.

tornado/test/httpclient_test.py
tornado/test/simple_httpclient_test.py
tornado/test/stack_context_test.py

index d34d8a826508809c9cdfd86190ec3aac713dd095..0efef9b32703405bebad4a3ed962f935b8fd9593 100644 (file)
@@ -442,8 +442,8 @@ class SyncHTTPClientTest(unittest.TestCase):
 
         sock, self.port = bind_unused_port()
         app = Application([('/', HelloWorldHandler)])
-        server = HTTPServer(app, io_loop=self.server_ioloop)
-        server.add_socket(sock)
+        self.server = HTTPServer(app, io_loop=self.server_ioloop)
+        self.server.add_socket(sock)
 
         self.server_thread = threading.Thread(target=self.server_ioloop.start)
         self.server_thread.start()
@@ -451,7 +451,10 @@ class SyncHTTPClientTest(unittest.TestCase):
         self.http_client = HTTPClient()
 
     def tearDown(self):
-        self.server_ioloop.add_callback(self.server_ioloop.stop)
+        def stop_server():
+            self.server.stop()
+            self.server_ioloop.stop()
+        self.server_ioloop.add_callback(stop_server)
         self.server_thread.join()
         self.http_client.close()
         self.server_ioloop.close(all_fds=True)
index 915376a7184659a68e8b6ce1ff7c0916f8509b11..1e54ea1d2d22aa98dd57d2fc6138e1898ced078d 100644 (file)
@@ -122,9 +122,9 @@ class SimpleHTTPClientTestMixin(object):
                         SimpleAsyncHTTPClient(self.io_loop,
                                               force_instance=True))
         # different IOLoops use different objects
-        io_loop2 = IOLoop()
-        self.assertTrue(SimpleAsyncHTTPClient(self.io_loop) is not
-                        SimpleAsyncHTTPClient(io_loop2))
+        with closing(IOLoop()) as io_loop2:
+            self.assertTrue(SimpleAsyncHTTPClient(self.io_loop) is not
+                            SimpleAsyncHTTPClient(io_loop2))
 
     def test_connection_limit(self):
         with closing(self.create_client(max_clients=2)) as client:
index 26988ddb351d2f2041b5f2bc18ccc13b6effcff6..5f62852f6bffad98ff7d915fc2529411c6dc1d20 100644 (file)
@@ -219,12 +219,21 @@ class StackContextTest(AsyncTestCase):
     def test_yield_in_with(self):
         @gen.engine
         def f():
-            with StackContext(functools.partial(self.context, 'c1')):
-                # This yield is a problem: the generator will be suspended
-                # and the StackContext's __exit__ is not called yet, so
-                # the context will be left on _state.contexts for anything
-                # that runs before the yield resolves.
-                yield gen.Task(self.io_loop.add_callback)
+            try:
+                with StackContext(functools.partial(self.context, 'c1')):
+                    # This yield is a problem: the generator will be suspended
+                    # and the StackContext's __exit__ is not called yet, so
+                    # the context will be left on _state.contexts for anything
+                    # that runs before the yield resolves.
+                    yield gen.Task(self.io_loop.add_callback)
+            except StackContextInconsistentError:
+                # In python <= 3.3, this suspended generator is never garbage
+                # collected, so it remains suspended in the 'yield' forever.
+                # Starting in 3.4, it is made collectable by raising
+                # a GeneratorExit exception from the yield, which gets
+                # converted into a StackContextInconsistentError by the
+                # exit of the 'with' block.
+                pass
 
         with self.assertRaises(StackContextInconsistentError):
             f()
@@ -242,8 +251,11 @@ class StackContextTest(AsyncTestCase):
         # As above, but with ExceptionStackContext instead of StackContext.
         @gen.engine
         def f():
-            with ExceptionStackContext(lambda t, v, tb: False):
-                yield gen.Task(self.io_loop.add_callback)
+            try:
+                with ExceptionStackContext(lambda t, v, tb: False):
+                    yield gen.Task(self.io_loop.add_callback)
+            except StackContextInconsistentError:
+                pass
 
         with self.assertRaises(StackContextInconsistentError):
             f()