Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fork in test test_cache_return_value_per_process #5636

Merged
merged 10 commits into from
Apr 12, 2022
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions tests/utils/test_process_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid

from mlflow.utils.process import cache_return_value_per_process
from multiprocessing import Process, Queue
import os


@cache_return_value_per_process
Expand All @@ -20,14 +20,6 @@ def _gen_random_no_arg():
return uuid.uuid4().hex


def _test_cache_return_value_per_process_child_proc_target(path1, path3, queue):
# in forked out child process
child_path1 = _gen_random_str1(True)
child_path2 = _gen_random_str1(False)
result = len({path1, path3, child_path1, child_path2}) == 4
queue.put(result)


def test_cache_return_value_per_process():

path1 = _gen_random_str1(True)
Expand All @@ -54,10 +46,24 @@ def test_cache_return_value_per_process():

assert len({path1, path3, f2_path1, f2_path2}) == 4

queue = Queue()
child_proc = Process(
target=_test_cache_return_value_per_process_child_proc_target, args=(path1, path3, queue)
)
child_proc.start()
child_proc.join()
assert queue.get(), "Testing inside child process failed."
# Skip the following block for Windows which doesn't support `os.fork`
if os.name != "nt":
WeichenXu123 marked this conversation as resolved.
Show resolved Hide resolved
# Test child process invalidates the cache.
# We don't create child process by `multiprocessing.Process` because
# `multiprocessing.Process` creates child process by pickling the target function
# and starts a new process to run the pickled function. But the global variable
# `_per_process_value_cache_map` dict content is not pickled, this makes child
# # and parent don't share the same global variables.
WeichenXu123 marked this conversation as resolved.
Show resolved Hide resolved
WeichenXu123 marked this conversation as resolved.
Show resolved Hide resolved
pid = os.fork()
if pid > 0:
# in parent process
child_pid = pid
# check child process exit with return value 0.
assert os.waitpid(child_pid, 0)[1] == 0
else:
# in forked out child process
child_path1 = _gen_random_str1(True)
child_path2 = _gen_random_str1(False)
test_pass = len({path1, path3, child_path1, child_path2}) == 4
harupy marked this conversation as resolved.
Show resolved Hide resolved
# exit forked out child process with exit code representing testing pass or fail.
os._exit(0 if test_pass else 1)
dbczumar marked this conversation as resolved.
Show resolved Hide resolved