Skip to content

Commit

Permalink
Test for future persisting past the lifetime of the executor
Browse files Browse the repository at this point in the history
Motivated the `wait=False` in executor __del__
  • Loading branch information
liamhuber committed Nov 9, 2023
1 parent c0f519d commit 0079808
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit 0079808

Please sign in to comment.