-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_dependency.py
More file actions
143 lines (117 loc) · 4.47 KB
/
Copy pathtest_dependency.py
File metadata and controls
143 lines (117 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from concurrent.futures import Future
import importlib.util
from time import sleep
import unittest
import numpy as np
from executorlib.task_scheduler.interactive.blockallocation import BlockAllocationTaskScheduler
from executorlib.standalone.interactive.spawner import MpiExecSpawner
skip_mpi4py_test = importlib.util.find_spec("mpi4py") is None
def calc(i):
return np.array(i**2)
class TestFuture(unittest.TestCase):
def test_pool_serial(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
) as p:
output = p.submit(calc, i=2)
self.assertTrue(isinstance(output, Future))
self.assertFalse(output.done())
sleep(1)
self.assertTrue(output.done())
self.assertEqual(output.result(), np.array(4))
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_pool_serial_multi_core(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
output = p.submit(calc, i=2)
self.assertTrue(isinstance(output, Future))
self.assertFalse(output.done())
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 = BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={},
spawner=MpiExecSpawner,
).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",
)
with self.subTest("From inside a class"):
class Foo:
def __init__(self):
self.running = False
def run(self):
self.running = True
future = BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={},
spawner=MpiExecSpawner,
).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",
)