def test_for_leaks(self): # Our original bad memory leak only happened on line numbers > 255, so # make a code object with more lines than that. Ugly string mumbo # jumbo to get 300 blank lines at the beginning.. code = ( """\ # blank line\n""" * 300 + """\ def once(x): # line 301 if x % 100 == 0: raise Exception("100!") elif x % 2: return 10 else: # line 306 return 11 i = 0 # Portable loop without alloc'ing memory. while i < ITERS: try: once(i) except: pass i += 1 # line 315 """ ) lines = list(range(301, 315)) lines.remove(306) # Line 306 is the "else". # This is a non-deterministic test, so try it a few times, and fail it # only if it predominantly fails. fails = 0 for _ in range(10): ram_0 = osinfo.process_ram() self.check_coverage(code.replace("ITERS", "10"), lines, "") ram_10 = osinfo.process_ram() self.check_coverage(code.replace("ITERS", "10000"), lines, "") ram_10k = osinfo.process_ram() # Running the code 10k times shouldn't grow the ram much more than # running it 10 times. ram_growth = (ram_10k - ram_10) - (ram_10 - ram_0) if ram_growth > 100000: fails += 1 if fails > 8: self.fail("RAM grew by %d" % (ram_growth))
def test_for_leaks(self): if env.JYTHON: self.skipTest("Don't bother on Jython") # Our original bad memory leak only happened on line numbers > 255, so # make a code object with more lines than that. Ugly string mumbo # jumbo to get 300 blank lines at the beginning.. code = """\ # blank line\n""" * 300 + """\ def once(x): # line 301 if x % 100 == 0: raise Exception("100!") elif x % 2: return 10 else: # line 306 return 11 i = 0 # Portable loop without alloc'ing memory. while i < ITERS: try: once(i) except: pass i += 1 # line 315 """ lines = list(range(301, 315)) lines.remove(306) # Line 306 is the "else". # This is a non-deterministic test, so try it a few times, and fail it # only if it predominantly fails. fails = 0 for _ in range(10): ram_0 = osinfo.process_ram() self.check_coverage(code.replace("ITERS", "10"), lines, "") ram_10 = osinfo.process_ram() self.check_coverage(code.replace("ITERS", "10000"), lines, "") ram_10k = osinfo.process_ram() # Running the code 10k times shouldn't grow the ram much more than # running it 10 times. ram_growth = (ram_10k - ram_10) - (ram_10 - ram_0) if ram_growth > 100000: fails += 1 # pragma: only failure if fails > 8: self.fail("RAM grew by %d" % (ram_growth)) # pragma: only failure