MTP: Add tests for mem leaks

This commit is contained in:
Kovid Goyal 2012-08-24 11:50:07 +05:30
parent 19b196f782
commit 5197dff52d
2 changed files with 43 additions and 4 deletions

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import unittest import unittest, gc
from calibre.constants import iswindows, islinux from calibre.constants import iswindows, islinux
from calibre.utils.icu import lower from calibre.utils.icu import lower
@ -78,13 +78,52 @@ class TestDeviceInteraction(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.dev.create_folder(root_file[0], 'sub-folder') self.dev.create_folder(root_file[0], 'sub-folder')
def measure_memory_usage(self, repetitions, func, *args, **kwargs):
from calibre.utils.mem import memory
gc.disable()
try:
start_mem = memory()
for i in xrange(repetitions):
func(*args, **kwargs)
for i in xrange(3): gc.collect()
end_mem = memory()
finally:
gc.enable()
return end_mem - start_mem
def test_memory_leaks(self): def test_memory_leaks(self):
''' Test for memory leaks in the C modules '''
if not (iswindows or islinux): if not (iswindows or islinux):
self.skipTest('Can only test for leaks on windows and linux') self.skipTest('Can only test for leaks on windows and linux')
from calibre.utils.mem import memory
# Test device scanning
used_by_one = self.measure_memory_usage(1,
self.dev.detect_managed_devices, self.scanner.devices,
force_refresh=True)
used_by_many = self.measure_memory_usage(1000,
self.dev.detect_managed_devices, self.scanner.devices,
force_refresh=True)
self.assertTrue(used_by_many <= used_by_one,
msg='Memory consumption during device scan: for one: %g for many:%g'%
(used_by_one, used_by_many))
# Test get_filesystem
used_by_one = self.measure_memory_usage(1,
self.dev.dev.get_filesystem, self.storage.object_id)
used_by_many = self.measure_memory_usage(5,
self.dev.dev.get_filesystem, self.storage.object_id)
self.assertTrue(used_by_many <= used_by_one,
msg='Memory consumption during get_filesystem: for one: %g for many:%g'%
(used_by_one, used_by_many))
def tests(): def tests():
return unittest.TestLoader().loadTestsFromTestCase(TestDeviceInteraction) tl = unittest.TestLoader()
return tl.loadTestsFromName('test.TestDeviceInteraction.test_memory_leaks')
return tl.loadTestsFromTestCase(TestDeviceInteraction)
def run(): def run():
unittest.TextTestRunner(verbosity=2).run(tests()) unittest.TextTestRunner(verbosity=2).run(tests())

View File

@ -14,7 +14,7 @@
#include "devices.h" #include "devices.h"
// Macros and utilities // Macros and utilities {{{
static PyObject *MTPError = NULL; static PyObject *MTPError = NULL;
#define ENSURE_DEV(rval) \ #define ENSURE_DEV(rval) \