Removed monkeypatching of __del__ in tests

On PyPy adding __del__ to a class after it's constructed does not
work, instead implement the leakchecker in tests by using weakrefs,
which works everywhere.

Change-Id: I873efb3e2f85f731d5836bf9bf06a21e939e8542
This commit is contained in:
Alex Gaynor 2013-08-12 22:21:15 -04:00
parent c2722411bc
commit 72d6f6d5f7

View File

@ -24,10 +24,12 @@ import signal
from contextlib import contextmanager, nested, closing from contextlib import contextmanager, nested, closing
from gzip import GzipFile from gzip import GzipFile
from shutil import rmtree from shutil import rmtree
import gc
import time import time
from urllib import quote from urllib import quote
from hashlib import md5 from hashlib import md5
from tempfile import mkdtemp from tempfile import mkdtemp
import weakref
import mock import mock
from eventlet import sleep, spawn, wsgi, listen from eventlet import sleep, spawn, wsgi, listen
@ -59,22 +61,15 @@ logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
STATIC_TIME = time.time() STATIC_TIME = time.time()
_request_instances = 0 _request_instances = weakref.WeakKeyDictionary()
_test_coros = _test_servers = _test_sockets = _orig_container_listing_limit = \ _test_coros = _test_servers = _test_sockets = _orig_container_listing_limit = \
_testdir = _orig_SysLogHandler = None _testdir = _orig_SysLogHandler = None
def request_init(self, *args, **kwargs): def request_init(self, *args, **kwargs):
global _request_instances
self._orig_init(*args, **kwargs) self._orig_init(*args, **kwargs)
_request_instances += 1
_request_instances[self] = None
def request_del(self):
global _request_instances
if self._orig_del:
self._orig_del()
_request_instances -= 1
def setup(): def setup():
@ -85,8 +80,6 @@ def setup():
utils.SysLogHandler = mock.MagicMock() utils.SysLogHandler = mock.MagicMock()
Request._orig_init = Request.__init__ Request._orig_init = Request.__init__
Request.__init__ = request_init Request.__init__ = request_init
Request._orig_del = getattr(Request, '__del__', None)
Request.__del__ = request_del
monkey_patch_mimetools() monkey_patch_mimetools()
# Since we're starting up a lot here, we're going to test more than # Since we're starting up a lot here, we're going to test more than
# just chunked puts; we're also going to test parts of # just chunked puts; we're also going to test parts of
@ -188,8 +181,6 @@ def teardown():
_orig_container_listing_limit _orig_container_listing_limit
rmtree(os.path.dirname(_testdir)) rmtree(os.path.dirname(_testdir))
Request.__init__ = Request._orig_init Request.__init__ = Request._orig_init
if Request._orig_del:
Request.__del__ = Request._orig_del
utils.SysLogHandler = _orig_SysLogHandler utils.SysLogHandler = _orig_SysLogHandler
@ -4384,7 +4375,6 @@ class TestObjectController(unittest.TestCase):
self.assertTrue('X-Delete-At in past' in resp.body) self.assertTrue('X-Delete-At in past' in resp.body)
def test_leak_1(self): def test_leak_1(self):
global _request_instances
prolis = _test_sockets[0] prolis = _test_sockets[0]
prosrv = _test_servers[0] prosrv = _test_servers[0]
obj_len = prosrv.client_chunk_size * 2 obj_len = prosrv.client_chunk_size * 2
@ -4402,8 +4392,11 @@ class TestObjectController(unittest.TestCase):
headers = readuntil2crlfs(fd) headers = readuntil2crlfs(fd)
exp = 'HTTP/1.1 201' exp = 'HTTP/1.1 201'
self.assertEqual(headers[:len(exp)], exp) self.assertEqual(headers[:len(exp)], exp)
# Remember Request instance count # Remember Request instance count, make sure the GC is run for pythons
before_request_instances = _request_instances # without reference counting.
for i in xrange(3):
gc.collect()
before_request_instances = len(_request_instances)
# GET test file, but disconnect early # GET test file, but disconnect early
sock = connect_tcp(('localhost', prolis.getsockname()[1])) sock = connect_tcp(('localhost', prolis.getsockname()[1]))
fd = sock.makefile() fd = sock.makefile()
@ -4419,7 +4412,10 @@ class TestObjectController(unittest.TestCase):
fd.read(1) fd.read(1)
fd.close() fd.close()
sock.close() sock.close()
self.assertEquals(before_request_instances, _request_instances) # Make sure the GC is run again for pythons without reference counting
for i in xrange(3):
gc.collect()
self.assertEquals(before_request_instances, len(_request_instances))
def test_OPTIONS(self): def test_OPTIONS(self):
with save_globals(): with save_globals():