/
socket_sync_testing.py
executable file
·75 lines (55 loc) · 2.23 KB
/
socket_sync_testing.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
#! /usr/bin/env python
"""
This script tests the 'data stream' oriented feature of the socket interface.
"""
from morse.testing.testing import MorseTestCase
try:
# Include this import to be able to use your test file as a regular
# builder script, ie, usable with: 'morse [run|exec] <your test>.py
from morse.builder import *
except ImportError:
pass
import sys
import time
import socket
from pymorse import Morse
class SocketSyncTest(MorseTestCase):
def setUpEnv(self):
bpymorse.set_speed(fps=10)
robot = Morsy()
clock = Clock()
clock.add_stream('socket')
robot.append(clock)
env = Environment('empty')
env.configure_stream_manager('socket', time_sync = True, sync_port = 5000)
def test_read_clock(self):
with Morse() as morse:
clock_stream = morse.robot.clock
time.sleep(0.5)
prev_clock = clock_stream.last()
time.sleep(0.2)
clock = clock_stream.last()
self.assertTrue(clock['timestamp'] > prev_clock['timestamp'])
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sync:
sync.connect(('localhost', 5000))
# Now the clock is blocked until we triggered it.
# Checking it :)
time.sleep(0.2)
prev_clock = clock_stream.last()
time.sleep(0.2)
clock = clock_stream.last()
self.assertTrue(clock['timestamp'] == prev_clock['timestamp'])
# triggering once
sync.send(bytes('foo', 'utf-8'))
clock = clock_stream.get()
self.assertAlmostEqual(clock['timestamp'] - prev_clock['timestamp'], 0.1, delta = 0.0001)
# So cool, isn't it :)
# Close the socket, no more control
prev_clock = clock_stream.last()
time.sleep(0.2)
clock = clock_stream.last()
self.assertTrue(clock['timestamp'] > prev_clock['timestamp'])
########################## Run these tests ##########################
if __name__ == "__main__":
from morse.testing.testing import main
main(SocketSyncTest, time_modes = [TimeStrategies.FixedSimulationStep])