| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # $Id: test_memory_leaks.py 1142 2011-10-05 18:45:49Z g.rodola $ | 3 # $Id: test_memory_leaks.py 1203 2011-10-24 19:10:36Z g.rodola $ |
| 4 # | 4 # |
| 5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. | 5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 6 # Use of this source code is governed by a BSD-style license that can be | 6 # Use of this source code is governed by a BSD-style license that can be |
| 7 # found in the LICENSE file. | 7 # found in the LICENSE file. |
| 8 | 8 |
| 9 """ | 9 """ |
| 10 A test script which attempts to detect memory leaks by calling C | 10 A test script which attempts to detect memory leaks by calling C |
| 11 functions many times and compare process memory usage before and | 11 functions many times and compare process memory usage before and |
| 12 after the calls. It might produce false positives. | 12 after the calls. It might produce false positives. |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 import os | 15 import os |
| 16 import gc | 16 import gc |
| 17 import unittest | 17 import unittest |
| 18 import time | 18 import time |
| 19 | 19 |
| 20 import psutil | 20 import psutil |
| 21 from test_psutil import reap_children, skipUnless, skipIf, \ | 21 from test_psutil import reap_children, skipUnless, skipIf, \ |
| 22 POSIX, LINUX, WINDOWS, OSX, BSD, PY3 | 22 POSIX, LINUX, WINDOWS, OSX, BSD, PY3 |
| 23 | 23 |
| 24 LOOPS = 1000 | 24 LOOPS = 1000 |
| 25 TOLERANCE = 4096 | 25 TOLERANCE = 4096 |
| 26 | 26 |
| 27 if PY3: | 27 if PY3: |
| 28 xrange = range | 28 xrange = range |
| 29 try: |
| 30 callable |
| 31 except NameError: |
| 32 callable = lambda x: hasattr(x, '__call__') |
| 29 | 33 |
| 30 | 34 |
| 31 class Base(unittest.TestCase): | 35 class Base(unittest.TestCase): |
| 32 | 36 |
| 33 def execute(self, function, *args, **kwargs): | 37 def execute(self, function, *args, **kwargs): |
| 34 # step 1 | 38 # step 1 |
| 35 for x in xrange(LOOPS): | 39 for x in xrange(LOOPS): |
| 36 self.call(function, *args, **kwargs) | 40 self.call(function, *args, **kwargs) |
| 37 del x | 41 del x |
| 38 gc.collect() | 42 gc.collect() |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 self.execute('is_running') | 138 self.execute('is_running') |
| 135 | 139 |
| 136 @skipIf(WINDOWS) | 140 @skipIf(WINDOWS) |
| 137 def test_terminal(self): | 141 def test_terminal(self): |
| 138 self.execute('terminal') | 142 self.execute('terminal') |
| 139 | 143 |
| 140 @skipUnless(WINDOWS) | 144 @skipUnless(WINDOWS) |
| 141 def test_resume(self): | 145 def test_resume(self): |
| 142 self.execute('resume') | 146 self.execute('resume') |
| 143 | 147 |
| 144 @skipUnless(WINDOWS) | 148 @skipIf(not hasattr(psutil.Process, 'getcwd')) |
| 145 def test_getcwd(self): | 149 def test_getcwd(self): |
| 146 self.execute('getcwd') | 150 self.execute('getcwd') |
| 147 | 151 |
| 148 @skipUnless(WINDOWS or OSX) | |
| 149 def test_get_open_files(self): | 152 def test_get_open_files(self): |
| 150 self.execute('get_open_files') | 153 self.execute('get_open_files') |
| 151 | 154 |
| 152 @skipUnless(WINDOWS or OSX) | 155 # XXX - still using provisional lsof implementation |
| 156 @skipIf(BSD) |
| 153 def test_get_connections(self): | 157 def test_get_connections(self): |
| 154 self.execute('get_connections') | 158 self.execute('get_connections') |
| 155 | 159 |
| 156 | 160 |
| 157 class TestModuleFunctionsLeaks(Base): | 161 class TestModuleFunctionsLeaks(Base): |
| 158 """Test leaks of psutil module functions.""" | 162 """Test leaks of psutil module functions.""" |
| 159 | 163 |
| 160 def setUp(self): | 164 def setUp(self): |
| 161 gc.collect() | 165 gc.collect() |
| 162 | 166 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 206 |
| 203 def test_main(): | 207 def test_main(): |
| 204 test_suite = unittest.TestSuite() | 208 test_suite = unittest.TestSuite() |
| 205 test_suite.addTest(unittest.makeSuite(TestProcessObjectLeaks)) | 209 test_suite.addTest(unittest.makeSuite(TestProcessObjectLeaks)) |
| 206 test_suite.addTest(unittest.makeSuite(TestModuleFunctionsLeaks)) | 210 test_suite.addTest(unittest.makeSuite(TestModuleFunctionsLeaks)) |
| 207 unittest.TextTestRunner(verbosity=2).run(test_suite) | 211 unittest.TextTestRunner(verbosity=2).run(test_suite) |
| 208 | 212 |
| 209 if __name__ == '__main__': | 213 if __name__ == '__main__': |
| 210 test_main() | 214 test_main() |
| 211 | 215 |
| OLD | NEW |