From 0079808006f28a770810340905f9754e40455a54 Mon Sep 17 00:00:00 2001 From: liamhuber Date: Thu, 9 Nov 2023 10:44:04 -0800 Subject: [PATCH] Test for future persisting past the lifetime of the executor Motivated the `wait=False` in executor __del__ --- tests/test_future.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_future.py b/tests/test_future.py index 1d59d4ee..f7f5d4d0 100644 --- a/tests/test_future.py +++ b/tests/test_future.py @@ -27,3 +27,51 @@ def test_pool_serial_multi_core(self): sleep(1) self.assertTrue(output.done()) self.assertEqual(output.result(), [np.array(4), np.array(4)]) + + def test_independence_from_executor(self): + """ + Ensure that futures are able to live on after the executor gets garbage + collected. + """ + + with self.subTest("From the main process"): + mutable = [] + + def slow_callable(): + from time import sleep + sleep(1) + return True + + def callback(future): + mutable.append("Called back") + + def submit(): + # Executor only exists in this scope and can get garbage collected after + # this function is exits + future = PyMPISingleTaskExecutor().submit(slow_callable) + future.add_done_callback(callback) + return future + + self.assertListEqual( + [], + mutable, + msg="Sanity check that test is starting in the expected condition" + ) + future = submit() + + self.assertFalse( + future.done(), + msg="The submit function is slow, it should be running still" + ) + self.assertListEqual( + [], + mutable, + msg="While running, the mutable should not have been impacted by the " + "callback" + ) + future.result() # Wait for the calculation to finish + self.assertListEqual( + ["Called back"], + mutable, + msg="After completion, the callback should modify the mutable data" + )