-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_cache_fileexecutor_serial.py
More file actions
213 lines (191 loc) · 6.84 KB
/
test_cache_fileexecutor_serial.py
File metadata and controls
213 lines (191 loc) · 6.84 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
from concurrent.futures import Future
import os
from queue import Queue
import shutil
import unittest
from threading import Thread
from executorlib.task_scheduler.file.subprocess_spawner import (
execute_in_subprocess,
terminate_subprocess,
)
try:
from executorlib.task_scheduler.file.task_scheduler import FileTaskScheduler, create_file_executor
from executorlib.task_scheduler.file.shared import execute_tasks_h5
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(ValueError):
create_file_executor(block_allocation=True)
with self.assertRaises(ValueError):
create_file_executor(init_function=True)
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()
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,
"cache_directory": cache_dir,
"execute_function": execute_in_subprocess,
"resource_dict": {"cores": 1, "cwd": None},
"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,
"cache_directory": cache_dir,
"execute_function": execute_in_subprocess,
"resource_dict": {"cores": 1, "cwd": None},
"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,
"cache_directory": cache_dir,
"execute_function": execute_in_subprocess,
"resource_dict": {"cores": 1},
"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_errors(self):
with self.assertRaises(ValueError):
execute_in_subprocess(
file_name=__file__, command=[], config_directory="test"
)
with self.assertRaises(ValueError):
execute_in_subprocess(file_name=__file__, command=[], backend="flux")
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)