-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_singlenodeexecutor_noblock.py
More file actions
177 lines (149 loc) · 5.94 KB
/
Copy pathtest_singlenodeexecutor_noblock.py
File metadata and controls
177 lines (149 loc) · 5.94 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
import unittest
from time import sleep
from executorlib import SingleNodeExecutor
from executorlib.standalone.serialize import cloudpickle_register
from executorlib.standalone.interactive.communication import ExecutorlibSocketError
def calc(i):
return i
def resource_dict(resource_dict):
return resource_dict
def get_worker_id(executorlib_worker_id):
sleep(0.1)
return executorlib_worker_id
def init_function():
return {"a": 1, "b": 2}
def exit_funct():
import sys
sys.exit()
class TestExecutorBackend(unittest.TestCase):
def test_meta_executor_serial_with_dependencies(self):
with SingleNodeExecutor(
max_cores=2,
block_allocation=False,
disable_dependencies=True,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
def test_meta_executor_serial_without_dependencies(self):
with SingleNodeExecutor(
max_cores=2,
block_allocation=False,
disable_dependencies=False,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
def test_meta_executor_single(self):
with SingleNodeExecutor(
max_cores=1,
block_allocation=False,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
def test_errors(self):
with self.assertRaises(TypeError):
SingleNodeExecutor(
max_cores=1,
resource_dict={
"cores": 1,
"gpus_per_core": 1,
},
)
with self.assertRaises(ValueError):
with SingleNodeExecutor(
max_cores=1,
block_allocation=False,
) as exe:
exe.submit(resource_dict, resource_dict={})
with self.assertRaises(ValueError):
with SingleNodeExecutor(
max_cores=1,
block_allocation=True,
) as exe:
exe.submit(resource_dict, resource_dict={})
class TestWorkerID(unittest.TestCase):
def test_block_allocation_True(self):
with SingleNodeExecutor(
max_cores=1,
block_allocation=True,
) as exe:
worker_id = exe.submit(get_worker_id, resource_dict={}).result()
self.assertEqual(worker_id, 0)
def test_block_allocation_True_two_workers(self):
with SingleNodeExecutor(
max_cores=2,
block_allocation=True,
) as exe:
f1_worker_id = exe.submit(get_worker_id, resource_dict={})
f2_worker_id = exe.submit(get_worker_id, resource_dict={})
self.assertEqual(sum([f1_worker_id.result(), f2_worker_id.result()]), 1)
def test_init_function(self):
with SingleNodeExecutor(
max_cores=1,
block_allocation=True,
init_function=init_function,
) as exe:
worker_id = exe.submit(get_worker_id, resource_dict={}).result()
self.assertEqual(worker_id, 0)
def test_init_function_two_workers(self):
with SingleNodeExecutor(
max_cores=2,
block_allocation=True,
init_function=init_function,
) as exe:
f1_worker_id = exe.submit(get_worker_id, resource_dict={})
f2_worker_id = exe.submit(get_worker_id, resource_dict={})
self.assertEqual(sum([f1_worker_id.result(), f2_worker_id.result()]), 1)
def test_block_allocation_False(self):
with SingleNodeExecutor(
max_cores=1,
block_allocation=False,
) as exe:
worker_id = exe.submit(get_worker_id, resource_dict={}).result()
self.assertEqual(worker_id, 0)
def test_block_allocation_False_two_workers(self):
with SingleNodeExecutor(
max_cores=2,
block_allocation=False,
) as exe:
f1_worker_id = exe.submit(get_worker_id, resource_dict={})
f2_worker_id = exe.submit(get_worker_id, resource_dict={})
self.assertEqual(sum([f1_worker_id.result(), f2_worker_id.result()]), 0)
class TestFunctionCrashes(unittest.TestCase):
def test_single_node_executor(self):
with self.assertRaises(ExecutorlibSocketError):
with SingleNodeExecutor(max_workers=2) as exe:
f = exe.submit(exit_funct)
print(f.result())
def test_single_node_executor_block_allocation(self):
with self.assertRaises(ExecutorlibSocketError):
with SingleNodeExecutor(max_workers=2, block_allocation=True, resource_dict={"restart_limit": 2}) as exe:
f = exe.submit(exit_funct)
print(f.result())
def test_single_node_executor_init_function(self):
with self.assertRaises(ExecutorlibSocketError):
with SingleNodeExecutor(max_workers=2, init_function=exit_funct, block_allocation=True) as exe:
f = exe.submit(sum, [1, 1])
print(f.result())
def test_single_node_executor_exit(self):
exe = SingleNodeExecutor(max_workers=2)
self.assertEqual(exe.submit(sum, [1,2,3]).result(), 6)
exe.shutdown()
with self.assertRaises(RuntimeError):
exe.submit(sum, [1, 2, 3])
with self.assertRaises(RuntimeError):
exe.map(calc, [1, 2, 3])