From aec5c9879e5a34688027c6bda0e1226f154cd96d Mon Sep 17 00:00:00 2001 From: Edward Oakes Date: Fri, 11 Dec 2020 21:47:05 -0600 Subject: [PATCH] Add tests for atexit handler behavior (#12808) --- python/ray/tests/test_actor.py | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/python/ray/tests/test_actor.py b/python/ray/tests/test_actor.py index 5d885715d..05d53c1b3 100644 --- a/python/ray/tests/test_actor.py +++ b/python/ray/tests/test_actor.py @@ -8,6 +8,7 @@ try: except ImportError: pytest_timeout = None import sys +import tempfile import datetime import ray @@ -867,5 +868,61 @@ def test_actor_creation_latency(ray_start_regular_shared): actor_create_time - start, end - start)) +@pytest.mark.parametrize( + "exit_condition", + [ + # "out_of_scope", TODO(edoakes): enable this once fixed. + "__ray_terminate__", + "ray.actor.exit_actor", + "ray.kill" + ]) +def test_atexit_handler(ray_start_regular_shared, exit_condition): + @ray.remote + class A(): + def __init__(self, tmpfile, data): + import atexit + + def f(*args, **kwargs): + with open(tmpfile, "w") as f: + f.write(data) + f.flush() + + atexit.register(f) + + def ready(self): + pass + + def exit(self): + ray.actor.exit_actor() + + data = "hello" + tmpfile = tempfile.NamedTemporaryFile() + a = A.remote(tmpfile.name, data) + ray.get(a.ready.remote()) + + if exit_condition == "out_of_scope": + del a + elif exit_condition == "__ray_terminate__": + ray.wait([a.__ray_terminate__.remote()]) + elif exit_condition == "ray.actor.exit_actor": + ray.wait([a.exit.remote()]) + elif exit_condition == "ray.kill": + ray.kill(a) + else: + assert False, "Unrecognized condition" + + def check_file_written(): + with open(tmpfile.name) as f: + if f.read() == data: + return True + return False + + # ray.kill() should not trigger atexit handlers, all other methods should. + if exit_condition == "ray.kill": + assert not check_file_written() + else: + ray.test_utils.wait_for_condition(check_file_written) + + if __name__ == "__main__": sys.exit(pytest.main(["-v", __file__]))