-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_libcluon_wrappers.py
229 lines (159 loc) · 4.92 KB
/
test_libcluon_wrappers.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
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
import time
import gc
import sys
import threading
from datetime import datetime
import numpy as np
import pytest
from pycluon._pycluon import (
Envelope,
OD4Session,
UDPSender,
UDPReceiver,
TCPConnection,
TCPServer,
SharedMemory,
)
def test_envelope_setters_getters():
e = Envelope()
assert e.data_type == 0
e.data_type = 53
assert e.data_type == 53
assert e.serialized_data == b""
e.serialized_data = b"muppet"
assert e.serialized_data == b"muppet"
assert isinstance(e.sent_at, datetime)
e.sent_at = datetime.fromtimestamp(347238.438274)
assert e.sent_at.timestamp() == 347238.438274
assert isinstance(e.received_at, datetime)
e.received_at = datetime.fromtimestamp(347238.438274)
assert e.received_at.timestamp() == 347238.438274
assert isinstance(e.sampled_at, datetime)
e.sampled_at = datetime.fromtimestamp(347238.438274)
assert e.sampled_at.timestamp() == 347238.438274
assert e.sender_stamp == 0
e.sender_stamp = 53
assert e.sender_stamp == 53
def test_OD4Session_time_trigger():
od = OD4Session(111)
_called = 0
def callback():
nonlocal _called
_called += 1
if _called >= 3:
return False
return True
od.set_time_trigger(10, callback)
assert _called == 3
def test_OD4Session_send_data_trigger():
od1 = OD4Session(111)
od2 = OD4Session(111)
sender = 13
message_id = 31
_called = False
def callback(envelope):
nonlocal _called, sender, message_id
if envelope.sender_stamp == sender and envelope.data_type == message_id:
_called = True
od1.add_data_trigger(message_id, callback)
e = Envelope()
e.data_type = message_id
e.sender_stamp = sender
od2.send(e)
time.sleep(0.01)
assert _called
def test_UDP_ping():
received = {}
def receive_callback(data, sender, timestamp):
nonlocal received
received["data"] = data
received["sender"] = sender
received["timestamp"] = timestamp
r = UDPReceiver("127.0.0.1", 50032, receive_callback)
size, error = UDPSender("127.0.0.1", 50032).send("test")
assert size == 4
assert error == 0
time.sleep(0.1)
assert received["data"] == "test"
assert isinstance(received["timestamp"], datetime)
def test_TCP_ping():
CALLED_ON_CONNECTION = False
def on_connection(connectee, connection):
nonlocal CALLED_ON_CONNECTION
CALLED_ON_CONNECTION = True
assert isinstance(connectee, str)
assert isinstance(connection, TCPConnection)
connection.send("test")
server = TCPServer(50033, on_connection)
CALLED_ON_CONNECTION_LOST = False
def on_connection_lost():
nonlocal CALLED_ON_CONNECTION_LOST
CALLED_ON_CONNECTION_LOST = True
CALLED_ON_MESSAGE = False
def on_message(data, timestamp):
nonlocal CALLED_ON_MESSAGE
CALLED_ON_MESSAGE = True
assert data == "test"
user = TCPConnection("127.0.0.1", 50033, on_message, on_connection_lost)
time.sleep(0.1)
assert CALLED_ON_CONNECTION
assert CALLED_ON_MESSAGE
del server
gc.collect()
assert CALLED_ON_CONNECTION_LOST
@pytest.mark.skipif(
sys.platform == "win32" or sys.platform == "darwin",
reason="See issue https://github.com/MO-RISE/pycluon/issues/11",
)
def test_shared_memory():
sm = SharedMemory("trial.data", 10)
sm2 = SharedMemory("trial.data")
print(sm.name())
assert sm.valid()
assert sm2.valid()
assert len(sm.data) == 10
assert len(sm2.data) == 10
assert sm.data == b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
assert sm2.data == b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
assert not sm.is_locked()
assert not sm2.is_locked()
sm.lock()
assert sm.is_locked()
assert not sm2.is_locked()
sm.unlock()
sm2.lock()
assert sm2.is_locked()
assert not sm.is_locked()
sm2.unlock()
with pytest.raises(BufferError):
sm.timestamp = datetime.now()
sm.lock()
dt = sm.timestamp = datetime.now()
with pytest.raises(TypeError):
sm.data = "abcdefghij"
sm.data = b"abcdefghij" # len == 10
sm.unlock()
sm2.lock()
assert sm2.data == b"abcdefghij"
assert sm2.timestamp == dt
sm2.unlock()
t = threading.Thread(target=sm2.wait, daemon=True)
t.start()
time.sleep(0.1)
assert t.is_alive()
sm.notify_all()
time.sleep(0.1)
assert not t.is_alive()
@pytest.mark.skipif(
sys.platform == "win32" or sys.platform == "darwin",
reason="See issue https://github.com/MO-RISE/pycluon/issues/11",
)
def test_for_issue_17():
# Create image with one element equal to 0
img = np.ones((480, 640, 3), dtype=np.uint8)
img[240, 320, 0] = 0
sm = SharedMemory("img.rgb", img.size)
sm.lock()
sm.data = img.tobytes()
assert sm.data == img.tobytes() # Fails but should pass
sm.unlock()