-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
243 lines (219 loc) · 7.7 KB
/
test_client.py
File metadata and controls
243 lines (219 loc) · 7.7 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
import json
import unittest
import time
import re
from pathlib import Path
from threading import Thread, Event
import zmq
from discos_client import DISCOSClient
class TestPublisher:
PORT = 16000
def __init__(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.XPUB)
self.socket.setsockopt(zmq.LINGER, 0)
self.socket.setsockopt(zmq.SNDHWM, 10)
# This loop is necessary to wait for the client to close between tests
while True:
try:
self.socket.bind(f"tcp://127.0.0.1:{self.PORT}")
break
except zmq.ZMQError:
pass
messages_dir = Path(__file__).resolve().parent
message_files = messages_dir.glob("messages/*.json")
self.messages = {}
for message in message_files:
with open(message, "r", encoding="utf-8") as f:
topic_name = message.stem
self.messages[topic_name] = json.load(f)
self.t = Thread(target=self.publish)
self.event = Event()
self.t.start()
def __enter__(self):
return self
def _handle_subscription(self):
while True:
try:
event = self.socket.recv(flags=zmq.DONTWAIT)
except zmq.Again:
break
if not event:
continue
op = event[0]
topic = event[1:].decode(errors="ignore")
if op != 1:
continue
if re.match(r"^[0-9A-Za-z]{4}_.+$", topic):
t = topic.split("_", 1)[1]
if t in self.messages:
message = json.dumps(
self.messages[t],
separators=(",", ":")
).encode("utf-8")
self.socket.send_multipart([
topic.encode("ascii"),
message
])
else:
subparts = {}
for key, val in self.messages.items():
if key.startswith(f"{t}."):
subkey = key[len(t) + 1:]
subparts[subkey] = val
if subparts:
message = json.dumps(
subparts,
separators=(",", ":")
).encode("utf-8")
self.socket.send_multipart([
topic.encode("ascii"), message
])
def _send_periodic_messages(self):
for topic, payload in self.messages.items():
payload["timestamp"]["unix_time"] = time.time()
if "." in topic:
topic, obj = topic.split(".", 1)
payload = {obj: payload}
payload = json.dumps(
payload,
separators=(",", ":")
).encode("utf-8")
self.socket.send_multipart([
topic.encode("ascii"),
payload
])
def publish(self):
while not self.event.is_set():
self._handle_subscription()
self._send_periodic_messages()
time.sleep(0.1)
def __exit__(self, exc_type, exc_value, traceback):
self.event.set()
self.t.join()
self.socket.close()
self.context.term()
class TestDISCOSClient(unittest.TestCase):
def test_no_topics(self):
DISCOSClient(
address="127.0.0.1",
port=16000,
telescope="SRT"
)
def test_unknown_topic(self):
with self.assertRaises(ValueError) as ex:
DISCOSClient(
"foo",
address="127.0.0.1",
port=16000
)
self.assertTrue(
"Topic 'foo' is not known" in ex.exception.args[0]
)
with self.assertRaises(ValueError) as ex:
DISCOSClient(
"foo", "bar",
address="127.0.0.1",
port=16000,
)
self.assertTrue(
"Topics 'foo' and 'bar' are not known" in ex.exception.args[0]
)
def test_repr(self):
client = DISCOSClient(address="127.0.0.1", port=16000)
self.assertTrue(
repr(client).startswith("<DISCOSClient({") and
repr(client).endswith("})>")
)
def test_str(self):
client = DISCOSClient(address="127.0.0.1", port=16000)
self.assertTrue(
str(client).startswith("{") and
str(client).endswith("}")
)
def test_format(self):
client = DISCOSClient(address="127.0.0.1", port=16000)
self.assertTrue(
f"{client:}".startswith("{") and
f"{client:}".endswith("}")
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:u}"
self.assertEqual(
ex.exception.args[0],
"Unknown format code 'u' for DISCOSClient"
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:.3f}"
self.assertEqual(
str(ex.exception),
"Unknown format code '.3f' for DISCOSClient"
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:.3m}"
self.assertEqual(
str(ex.exception),
"Unknown format code '.3m' for DISCOSClient"
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:fm}"
self.assertEqual(
str(ex.exception),
"Format specifier cannot contain both 'f' and 'm'."
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:0i}"
self.assertEqual(
str(ex.exception),
"Indentation must be a positive integer"
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:ai}"
self.assertEqual(
str(ex.exception),
"Invalid indent in format spec: 'a'"
)
with self.assertRaises(ValueError) as ex:
_ = f"{client:3c}"
self.assertEqual(
str(ex.exception),
"Unknown format code '3c' for DISCOSClient"
)
self.assertNotIn("\": ", f"{client:c}")
def test_bind(self):
with TestPublisher():
client = DISCOSClient(address="127.0.0.1", port=16000)
time.sleep(1)
s = set()
called = set()
s.add(id(client.antenna.timestamp.unix_time))
s.add(id(client.antenna))
def callback(value):
called.add(id(value))
client.antenna.timestamp.unix_time.bind(callback)
client.antenna.bind(callback)
start = time.time()
while len(called) < 2 and (time.time() - start) < 60:
time.sleep(0.1)
self.assertEqual(s, called)
client.antenna.timestamp.unix_time.unbind(callback)
client.antenna.unbind(callback, str) # Never used predicate
client.antenna.unbind(callback)
client.antenna.unbind(int) # Never bound callback
client.antenna.unbind(None) # Unbind all callbacks
def test_wait(self):
with TestPublisher():
client = DISCOSClient(address="127.0.0.1", port=16000)
time.sleep(1)
unix_time = client.antenna.timestamp.unix_time.copy()
antenna = client.antenna.copy()
self.assertNotEqual(
unix_time,
client.antenna.timestamp.unix_time.wait(timeout=10)
)
self.assertNotEqual(
antenna,
client.antenna.wait(timeout=5)
)
if __name__ == '__main__':
unittest.main()