forked from voxel51/fiftyone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_tests.py
161 lines (125 loc) · 3.98 KB
/
ipc_tests.py
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
"""
Tests related to inter-process communication (for services).
| Copyright 2017-2023, Voxel51, Inc.
| `voxel51.com <https://voxel51.com/>`_
|
"""
import contextlib
import pickle
import random
import socket
import sys
import threading
import time
import psutil
import pytest
import retrying
from fiftyone.service.ipc import IPCServer, send_request
from fiftyone.service.util import (
find_processes_by_args,
get_listening_tcp_ports,
normalize_wrapper_process,
send_ipc_message,
)
current_process = psutil.Process()
def list_current_ports():
return list(get_listening_tcp_ports(current_process))
@contextlib.contextmanager
def SingleRequestHandler(server):
t = threading.Thread(target=server.handle_request)
t.start()
try:
yield
finally:
server.stop()
t.join()
@contextlib.contextmanager
def MultiRequestHandler(server):
t = threading.Thread(target=server.serve_forever)
t.start()
try:
yield
finally:
server.stop()
t.join()
def test_one_request():
with IPCServer(lambda x: x * 2) as server, SingleRequestHandler(server):
assert send_request(server.port, 5) == 10
def test_multiple_requests():
with IPCServer(lambda x: x * 2) as server, MultiRequestHandler(server):
assert send_request(server.port, 5) == 10
assert send_request(server.port, "a") == "aa"
def test_bad_request():
with IPCServer(lambda _: None) as server, SingleRequestHandler(server):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", server.port))
s.send(b"foo")
res = pickle.loads(s.recv(2048))
assert isinstance(res, pickle.UnpicklingError)
def test_large_request():
with IPCServer(lambda x: x) as server, SingleRequestHandler(server):
data = list(range(10000))
assert send_request(server.port, data) == data
def test_timeout():
with IPCServer(lambda _: None) as server:
server.timeout = 1
timeout_called = threading.Event()
server.handle_timeout = timeout_called.set
with SingleRequestHandler(server):
time.sleep(server.timeout + 0.5)
assert timeout_called.is_set()
def test_stop_single():
requests = []
with IPCServer(requests.append) as server, SingleRequestHandler(server):
server.timeout = 1
server.stop()
with pytest.raises(socket.error):
send_request(server.port, 5)
assert not requests
def test_stop_multi():
requests = []
with IPCServer(requests.append) as server, MultiRequestHandler(server):
send_request(server.port, 1)
assert requests == [1]
server.stop()
with pytest.raises(socket.error):
send_request(server.port, 2)
assert requests == [1]
def test_run_in_background():
requests = []
with IPCServer.run_in_background(requests.append) as server:
send_request(server.port, 2)
send_request(server.port, 3)
assert requests == [2, 3]
def test_find_processes_by_args():
assert current_process in list(
find_processes_by_args(current_process.cmdline())
)
random_arg = str(5 + random.random())
p = psutil.Popen(
[
sys.executable,
"-c",
"import sys, time; time.sleep(float(sys.argv[1]))",
random_arg,
]
)
@retrying.retry(stop_max_delay=2000)
def _check():
assert normalize_wrapper_process(p) in list(
find_processes_by_args([random_arg])
)
try:
_check()
finally:
p.kill()
def test_get_listening_tcp_ports():
assert not list_current_ports()
with IPCServer(lambda _: None) as server:
assert list_current_ports() == [server.port]
assert not list_current_ports()
def test_send_ipc_message():
with IPCServer.run_in_background(lambda x: x) as server:
assert send_ipc_message(current_process, 6) == 6
with pytest.raises(IOError):
send_ipc_message(current_process, 7)