Skip to content

Commit

Permalink
Fix warnings during final GC of the test suite in python 3.4a1.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bdarnell committed Sep 8, 2013
1 parent 51c9796 commit 8bd3ef0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
9 changes: 6 additions & 3 deletions tornado/test/httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,19 @@ def setUp(self):

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()

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)
Expand Down
6 changes: 3 additions & 3 deletions tornado/test/simple_httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ def test_singleton(self):
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:
Expand Down
28 changes: 20 additions & 8 deletions tornado/test/stack_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,21 @@ def f3():
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()
Expand All @@ -242,8 +251,11 @@ def test_yield_in_with_exception_stack_context(self):
# 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()
Expand Down

0 comments on commit 8bd3ef0

Please sign in to comment.