From 48c06f5042edc90802ec87619a91a0e8d38eaefa Mon Sep 17 00:00:00 2001 From: "Siyuan (Ryans) Zhuang" Date: Wed, 19 Feb 2020 18:35:35 -0800 Subject: [PATCH] Enhance the serialization refcount test for dynamic classes (#7222) * enhance the test for dynamic classes --- python/ray/tests/test_basic.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python/ray/tests/test_basic.py b/python/ray/tests/test_basic.py index 03a3789ee..9af8eb798 100644 --- a/python/ray/tests/test_basic.py +++ b/python/ray/tests/test_basic.py @@ -460,13 +460,15 @@ def test_reducer_override_no_reference_cycle(ray_start_regular): # bpo-39492: reducer_override used to induce a spurious reference cycle # inside the Pickler object, that could prevent all serialized objects # from being garbage-collected without explicity invoking gc.collect. + + # test a dynamic function def f(): return 4669201609102990671853203821578 wr = weakref.ref(f) bio = io.BytesIO() - from ray.cloudpickle import CloudPickler, loads + from ray.cloudpickle import CloudPickler, loads, dumps p = CloudPickler(bio, protocol=5) p.dump(f) new_f = loads(bio.getvalue()) @@ -477,6 +479,18 @@ def test_reducer_override_no_reference_cycle(ray_start_regular): assert wr() is None + # test a dynamic class + class ShortlivedObject: + def __del__(self): + print("Went out of scope!") + + obj = ShortlivedObject() + new_obj = weakref.ref(obj) + + dumps(obj) + del obj + assert new_obj() is None + def test_passing_arguments_by_value_out_of_the_box(ray_start_regular): @ray.remote