2019-09-07 08:59:19 +00:00
|
|
|
import os
|
2019-09-21 17:06:20 +00:00
|
|
|
import platform
|
2019-09-07 08:59:19 +00:00
|
|
|
import psutil
|
2019-09-21 17:06:20 +00:00
|
|
|
import pytest
|
2019-09-07 08:59:19 +00:00
|
|
|
from rustapi_module.buf_and_str import BytesExtractor
|
|
|
|
|
2019-09-21 17:06:20 +00:00
|
|
|
PYPY = platform.python_implementation() == "PyPy"
|
2019-09-07 08:59:19 +00:00
|
|
|
|
2019-09-21 17:06:20 +00:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
PYPY,
|
|
|
|
reason="PyPy has a segfault bug around this area."
|
|
|
|
"See https://github.com/PyO3/pyo3/issues/589 for detail.",
|
|
|
|
)
|
2019-09-07 08:59:19 +00:00
|
|
|
def test_pybuffer_doesnot_leak_memory():
|
2019-09-14 06:42:42 +00:00
|
|
|
N = 1000
|
2019-09-07 08:59:19 +00:00
|
|
|
extractor = BytesExtractor()
|
|
|
|
process = psutil.Process(os.getpid())
|
|
|
|
|
|
|
|
def memory_diff(f):
|
|
|
|
before = process.memory_info().rss
|
|
|
|
f()
|
|
|
|
after = process.memory_info().rss
|
|
|
|
return after - before
|
|
|
|
|
|
|
|
message_b = b'\\(-"-;) Praying that memory leak would not happen..'
|
|
|
|
message_s = '\\(-"-;) Praying that memory leak would not happen..'
|
|
|
|
|
|
|
|
def to_vec():
|
|
|
|
for i in range(N):
|
|
|
|
extractor.to_vec(message_b)
|
|
|
|
|
|
|
|
def to_str():
|
|
|
|
for i in range(N):
|
|
|
|
extractor.to_str(message_s)
|
|
|
|
|
2019-10-06 16:50:33 +00:00
|
|
|
assert memory_diff(to_vec) == 0
|
|
|
|
assert memory_diff(to_str) == 0
|