From 8b6e8a670134a9941f44e5056af68730b5a449ef Mon Sep 17 00:00:00 2001 From: liamhuber Date: Thu, 9 Nov 2023 10:44:44 -0800 Subject: [PATCH] Test for persistence of future when submitting from a class Motivated the changes to `join` behaviour during `shutdown` --- tests/test_future.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_future.py b/tests/test_future.py index f7f5d4d0..426d0156 100644 --- a/tests/test_future.py +++ b/tests/test_future.py @@ -75,3 +75,40 @@ def submit(): mutable, msg="After completion, the callback should modify the mutable data" ) + + with self.subTest("From inside a class"): + class Foo: + def __init__(self): + self.running = False + + def run(self): + self.running = True + + future = PyMPISingleTaskExecutor().submit(self.return_42) + future.add_done_callback(self.finished) + + return future + + def return_42(self): + from time import sleep + sleep(1) + return 42 + + def finished(self, future): + self.running = False + + foo = Foo() + self.assertFalse( + foo.running, + msg="Sanity check that the test starts in the expected condition" + ) + fs = foo.run() + self.assertTrue( + foo.running, + msg="We should be able to exit the run method before the task completes" + ) + fs.result() # Wait for completion + self.assertFalse( + foo.running, + msg="After task completion, we expect the callback to modify the class" + ) \ No newline at end of file