Skip to content

Commit

Permalink
FIX-modin-project#2465: Implement more reliable check_file_leaks()
Browse files Browse the repository at this point in the history
Signed-off-by: Vasilij Litvinov <vasilij.n.litvinov@intel.com>
  • Loading branch information
vnlitvinov committed Dec 7, 2020
1 parent bcb3bd8 commit 5b9b9a7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modin/pandas/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import shutil
import sqlalchemy as sa
import csv
from pandas.util._test_decorators import check_file_leaks

from .utils import (
check_file_leaks,
df_equals,
json_short_string,
json_short_bytes,
Expand Down
35 changes: 35 additions & 0 deletions modin/pandas/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import os
from string import ascii_letters
import csv
import psutil
import functools

random_state = np.random.RandomState(seed=42)

Expand Down Expand Up @@ -1001,3 +1003,36 @@ def insert_lines_to_csv(
**csv_reader_writer_params,
)
writer.writerows(lines)


def _get_open_files():
"""
psutil open_files() can return a lot of extra information that we can allow to
be different, like file position; for simplicity we care about path and fd only.
"""
return sorted((info.path, info.fd) for info in psutil.Process().open_files())


def check_file_leaks(func):
"""
A decorator that ensures that no *newly* opened file handles are left
after decorated function is finished.
"""

@functools.wraps(func)
def check(*a, **kw):
fstart = _get_open_files()
try:
return func(*a, **kw)
finally:
leaks = []
for item in _get_open_files():
try:
fstart.remove(item)
except ValueError:
leaks.append(item)
assert (
not leaks
), f"Unexpected open handles left for: {', '.join(item[0] for item in leaks)}"

return check

0 comments on commit 5b9b9a7

Please sign in to comment.