-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_serial.py
More file actions
261 lines (237 loc) · 8.82 KB
/
Copy pathtest_serial.py
File metadata and controls
261 lines (237 loc) · 8.82 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from concurrent.futures import Future
import os
from queue import Queue
import shutil
import unittest
from threading import Thread
from time import sleep
try:
from executorlib.task_scheduler.file.spawner_subprocess import (
execute_in_subprocess,
terminate_subprocess,
)
from executorlib.task_scheduler.file.task_scheduler import FileTaskScheduler, create_file_executor
from executorlib.task_scheduler.file.shared import execute_tasks_h5, _convert_args_and_kwargs
skip_h5py_test = False
except ImportError:
skip_h5py_test = True
def my_funct(a, b):
return a + b
def list_files_in_working_directory():
return os.listdir(os.getcwd())
def get_error(a):
raise ValueError(a)
@unittest.skipIf(
skip_h5py_test, "h5py is not installed, so the h5py tests are skipped."
)
class TestCacheExecutorSerial(unittest.TestCase):
def test_executor_mixed(self):
with FileTaskScheduler(execute_function=execute_in_subprocess) as exe:
fs1 = exe.submit(my_funct, 1, b=2)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), 3)
self.assertTrue(fs1.done())
def test_executor_mixed_cache_key(self):
with FileTaskScheduler(execute_function=execute_in_subprocess) as exe:
fs1 = exe.submit(my_funct, 1, b=2, resource_dict={"cache_key": "a/b/c"})
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), 3)
self.assertTrue(fs1.done())
def test_executor_dependence_mixed(self):
with FileTaskScheduler(execute_function=execute_in_subprocess) as exe:
fs1 = exe.submit(my_funct, 1, b=2)
fs2 = exe.submit(my_funct, 1, b=fs1)
self.assertFalse(fs2.done())
self.assertEqual(fs2.result(), 4)
self.assertTrue(fs2.done())
def test_create_file_executor_error(self):
with self.assertRaises(TypeError):
create_file_executor()
with self.assertRaises(ValueError):
create_file_executor(block_allocation=True, resource_dict={})
with self.assertRaises(ValueError):
create_file_executor(init_function=True, resource_dict={})
def test_executor_dependence_error(self):
with self.assertRaises(ValueError):
with FileTaskScheduler(
execute_function=execute_in_subprocess, disable_dependencies=True
) as exe:
fs = exe.submit(my_funct, 1, b=exe.submit(my_funct, 1, b=2))
fs.result()
def test_executor_working_directory(self):
cwd = os.path.join(os.path.dirname(__file__), "..", "..", "executables")
with FileTaskScheduler(
resource_dict={"cwd": cwd}, execute_function=execute_in_subprocess
) as exe:
fs1 = exe.submit(list_files_in_working_directory)
self.assertEqual(fs1.result(), os.listdir(cwd))
def test_executor_error(self):
cwd = os.path.join(os.path.dirname(__file__), "..", "..", "executables")
with FileTaskScheduler(
resource_dict={"cwd": cwd}, execute_function=execute_in_subprocess
) as exe:
fs1 = exe.submit(get_error, a=1)
with self.assertRaises(ValueError):
fs1.result()
self.assertEqual(len(os.listdir(cwd)), 1)
def test_executor_error_file(self):
cwd = os.path.join(os.path.dirname(__file__), "..", "..", "executables")
with FileTaskScheduler(
resource_dict={"cwd": cwd, "error_log_file": "error.out"},
execute_function=execute_in_subprocess
) as exe:
fs1 = exe.submit(get_error, a=1)
with self.assertRaises(ValueError):
fs1.result()
working_directory_file_lst = os.listdir(cwd)
self.assertEqual(len(working_directory_file_lst), 2)
self.assertTrue("error.out" in working_directory_file_lst)
os.remove(os.path.join(cwd, "error.out"))
def test_executor_function(self):
fs1 = Future()
q = Queue()
q.put(
{
"fn": my_funct,
"args": (),
"kwargs": {"a": 1, "b": 2},
"future": fs1,
"resource_dict": {},
}
)
cache_dir = os.path.abspath("executorlib_cache")
os.makedirs(cache_dir, exist_ok=True)
process = Thread(
target=execute_tasks_h5,
kwargs={
"future_queue": q,
"execute_function": execute_in_subprocess,
"resource_dict": {"cores": 1, "cwd": None, "cache_directory": cache_dir},
"terminate_function": terminate_subprocess,
},
)
process.start()
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), 3)
self.assertTrue(fs1.done())
q.put({"shutdown": True, "wait": True})
process.join()
def test_executor_function_dependence_kwargs(self):
fs1 = Future()
fs2 = Future()
q = Queue()
q.put(
{
"fn": my_funct,
"args": (),
"kwargs": {"a": 1, "b": 2},
"future": fs1,
"resource_dict": {},
}
)
q.put(
{
"fn": my_funct,
"args": (),
"kwargs": {"a": 1, "b": fs1},
"future": fs2,
"resource_dict": {},
}
)
cache_dir = os.path.abspath("executorlib_cache")
os.makedirs(cache_dir, exist_ok=True)
process = Thread(
target=execute_tasks_h5,
kwargs={
"future_queue": q,
"execute_function": execute_in_subprocess,
"resource_dict": {"cores": 1, "cwd": None, "cache_directory": cache_dir},
"terminate_function": terminate_subprocess,
},
)
process.start()
self.assertFalse(fs2.done())
self.assertEqual(fs2.result(), 4)
self.assertTrue(fs2.done())
q.put({"shutdown": True, "wait": True})
process.join()
def test_executor_function_dependence_args(self):
fs1 = Future()
fs2 = Future()
q = Queue()
q.put(
{
"fn": my_funct,
"args": (),
"kwargs": {"a": 1, "b": 2},
"future": fs1,
"resource_dict": {},
}
)
q.put(
{
"fn": my_funct,
"args": [fs1],
"kwargs": {"b": 2},
"future": fs2,
"resource_dict": {},
}
)
cache_dir = os.path.abspath("executorlib_cache")
os.makedirs(cache_dir, exist_ok=True)
process = Thread(
target=execute_tasks_h5,
kwargs={
"future_queue": q,
"execute_function": execute_in_subprocess,
"resource_dict": {"cores": 1, "cache_directory": cache_dir},
"terminate_function": terminate_subprocess,
},
)
process.start()
self.assertFalse(fs2.done())
self.assertEqual(fs2.result(), 5)
self.assertTrue(fs2.done())
q.put({"shutdown": True, "wait": True})
process.join()
def test_execute_in_subprocess(self):
process = execute_in_subprocess(
command=["sleep", "5"],
file_name="test.h5",
data_dict={"fn": sleep, "args": (5,)},
)
self.assertIsNone(terminate_subprocess(task=process))
def test_execute_in_subprocess_errors(self):
file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "executorlib_cache", "test.h5"))
os.makedirs(os.path.dirname(file_name), exist_ok=True)
with open(file_name, "w") as f:
f.write("test")
with self.assertRaises(ValueError):
execute_in_subprocess(
file_name=file_name,
data_dict={},
command=[],
config_directory="test",
)
with self.assertRaises(ValueError):
execute_in_subprocess(
file_name=file_name,
data_dict={},
command=[],
backend="flux",
)
def test_convert_args_and_kwargs(self):
f1 = Future()
f1.set_result(1)
f2 = Future()
f2.set_result(2)
task_args, task_kwargs, future_wait_key_lst = _convert_args_and_kwargs(
task_dict={"fn": 1, "args": (f1,), "kwargs": {"a": f2}},
memory_dict={},
file_name_dict={},
)
self.assertEqual(task_args, [1])
self.assertEqual(task_kwargs, {"a": 2})
self.assertTrue(len(future_wait_key_lst) == 0)
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)