Fixed up the webp leak check

This commit is contained in:
Eric Soroos 2018-01-24 14:02:33 +00:00
parent 0da68dee98
commit 379d3ccce2

View File

@ -1,38 +1,25 @@
from __future__ import division from helper import unittest, PillowLeakTestCase
from helper import unittest, PillowTestCase from PIL import Image, features
import sys
from PIL import Image
from io import BytesIO from io import BytesIO
# Limits for testing the leak
mem_limit = 16 # max increase in MB
iterations = 5000
test_file = "Tests/images/hopper.webp" test_file = "Tests/images/hopper.webp"
@unittest.skipUnless(features.check('webp'), "WebP is not installed")
class TestWebPLeaks(PillowLeakTestCase):
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") mem_limit = 3 * 1024 # kb
class TestWebPLeaks(PillowTestCase): iterations = 100
def setUp(self):
try:
from PIL import _webp
except ImportError:
self.skipTest('WebP support not installed')
def _get_mem_usage(self):
from resource import getpagesize, getrusage, RUSAGE_SELF
mem = getrusage(RUSAGE_SELF).ru_maxrss
return mem * getpagesize() / 1024 / 1024
def test_leak_load(self): def test_leak_load(self):
with open(test_file, 'rb') as f: with open(test_file, 'rb') as f:
im_data = f.read() im_data = f.read()
start_mem = self._get_mem_usage()
for _ in range(iterations): def core():
with Image.open(BytesIO(im_data)) as im: with Image.open(BytesIO(im_data)) as im:
im.load() im.load()
mem = (self._get_mem_usage() - start_mem)
self.assertLess(mem, mem_limit, msg='memory usage limit exceeded') self._test_leak(core)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()