-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_communication.py
More file actions
212 lines (193 loc) · 7.11 KB
/
Copy pathtest_communication.py
File metadata and controls
212 lines (193 loc) · 7.11 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
import importlib.util
import os
import sys
import unittest
from time import sleep
from typing import Callable, Optional
import numpy as np
import zmq
from executorlib.standalone.interactive.communication import (
interface_connect,
interface_shutdown,
interface_send,
interface_receive,
SocketInterface,
ExecutorlibSocketError,
)
from executorlib.standalone.serialize import cloudpickle_register
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 BrokenSpawner(MpiExecSpawner):
def bootup(self, command_lst: list[str], stop_function: Optional[Callable] = None,):
return False
class TestInterface(unittest.TestCase):
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_interface_mpi(self):
cloudpickle_register(ind=1)
task_dict = {"fn": calc, "args": (), "kwargs": {"i": 2}}
interface = SocketInterface(
spawner=MpiExecSpawner(cwd=None, cores=1, openmpi_oversubscribe=False)
)
interface.bootup(
command_lst=[
sys.executable,
os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
"..",
"src",
"executorlib",
"backend",
"interactive_parallel.py",
)
),
"--zmqport",
str(interface.bind_to_random_port()),
]
)
self.assertTrue(interface.status)
self.assertEqual(
interface.send_and_receive_dict(input_dict=task_dict), np.array(4)
)
interface.shutdown(wait=True)
def test_interface_serial_without_debug(self):
cloudpickle_register(ind=1)
task_dict = {"fn": calc, "args": (), "kwargs": {"i": 2}}
interface = SocketInterface(
spawner=MpiExecSpawner(cwd=None, cores=1, openmpi_oversubscribe=False),
log_obj_size=False,
)
interface.bootup(
command_lst=[
sys.executable,
os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
"..",
"src",
"executorlib",
"backend",
"interactive_serial.py",
)
),
"--zmqport",
str(interface.bind_to_random_port()),
]
)
self.assertTrue(interface.status)
self.assertEqual(
interface.send_and_receive_dict(input_dict=task_dict), np.array(4)
)
interface.shutdown(wait=True)
def test_interface_serial_with_debug(self):
cloudpickle_register(ind=1)
task_dict = {"fn": calc, "args": (), "kwargs": {"i": 2}}
interface = SocketInterface(
spawner=MpiExecSpawner(cwd=None, cores=1, openmpi_oversubscribe=False),
log_obj_size=True,
)
interface.bootup(
command_lst=[
sys.executable,
os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
"..",
"src",
"executorlib",
"backend",
"interactive_serial.py",
)
),
"--zmqport",
str(interface.bind_to_random_port()),
]
)
self.assertTrue(interface.status)
self.assertEqual(
interface.send_and_receive_dict(input_dict=task_dict), np.array(4)
)
interface.shutdown(wait=True)
def test_interface_serial_with_error(self):
cloudpickle_register(ind=1)
interface = SocketInterface(
spawner=MpiExecSpawner(cwd=None, cores=1, openmpi_oversubscribe=False),
log_obj_size=True,
)
interface.bootup(command_lst=["bash", "exit"])
self.assertTrue(interface.status)
while interface._spawner.poll():
sleep(0.1)
self.assertFalse(interface._spawner.poll())
interface.shutdown(wait=True)
def test_interface_serial_wrong_input(self):
cloudpickle_register(ind=1)
interface = SocketInterface(
spawner=MpiExecSpawner(cwd=None, cores=1, openmpi_oversubscribe=False),
log_obj_size=True,
)
with self.assertRaises(ValueError):
interface.bootup(command_lst=None)
def test_interface_serial_with_broken_spawner(self):
cloudpickle_register(ind=1)
interface = SocketInterface(
spawner=BrokenSpawner(cwd=None, cores=1, openmpi_oversubscribe=False),
log_obj_size=True,
)
interface.bootup(command_lst=["bash", "exit"])
self.assertFalse(interface.status)
def test_interface_serial_with_stopped_process(self):
cloudpickle_register(ind=1)
task_dict = {"fn": calc, "args": (), "kwargs": {"i": 2}}
interface = SocketInterface(
spawner=MpiExecSpawner(cwd=None, cores=1, openmpi_oversubscribe=False),
log_obj_size=True,
)
interface.bootup(
command_lst=[
sys.executable,
os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
"..",
"src",
"executorlib",
"backend",
"interactive_serial.py",
)
),
"--zmqport",
str(interface.bind_to_random_port()),
]
)
self.assertTrue(interface.status)
interface.send_dict(input_dict=task_dict)
interface._spawner._process.terminate()
with self.assertRaises(ExecutorlibSocketError):
interface.receive_dict()
class TestZMQ(unittest.TestCase):
def test_interface_receive(self):
self.assertEqual(len(interface_receive(socket=None)), 0)
def test_initialize_zmq(self):
message = "test"
host = "localhost"
context_server = zmq.Context()
socket_server = context_server.socket(zmq.PAIR)
port = str(socket_server.bind_to_random_port("tcp://*"))
context_client, socket_client = interface_connect(host=host, port=port)
interface_send(socket=socket_server, result_dict={"message": message})
self.assertEqual(interface_receive(socket=socket_client), {"message": message})
interface_shutdown(socket=socket_client, context=context_client)
interface_shutdown(socket=socket_server, context=context_server)