|
| 1 | +#! /usr/bin/env python |
| 2 | +""" |
| 3 | +This script tests the 'data stream' oriented feature of the socket interface. |
| 4 | +""" |
| 5 | + |
| 6 | +from morse.testing.testing import MorseTestCase |
| 7 | + |
| 8 | +try: |
| 9 | + # Include this import to be able to use your test file as a regular |
| 10 | + # builder script, ie, usable with: 'morse [run|exec] <your test>.py |
| 11 | + from morse.builder import * |
| 12 | +except ImportError: |
| 13 | + pass |
| 14 | + |
| 15 | +import sys |
| 16 | +import time |
| 17 | +import socket |
| 18 | +from pymorse import Morse |
| 19 | + |
| 20 | + |
| 21 | +class SocketSyncTest(MorseTestCase): |
| 22 | + |
| 23 | + def setUpEnv(self): |
| 24 | + bpymorse.set_speed(fps=10) |
| 25 | + |
| 26 | + robot = Morsy() |
| 27 | + |
| 28 | + clock = Clock() |
| 29 | + clock.add_stream('socket') |
| 30 | + robot.append(clock) |
| 31 | + |
| 32 | + env = Environment('empty') |
| 33 | + env.configure_stream_manager('socket', time_sync = True, sync_port = 5000) |
| 34 | + |
| 35 | + def test_read_clock(self): |
| 36 | + with Morse() as morse: |
| 37 | + clock_stream = morse.robot.clock |
| 38 | + |
| 39 | + time.sleep(0.5) |
| 40 | + |
| 41 | + prev_clock = clock_stream.last() |
| 42 | + time.sleep(0.2) |
| 43 | + clock = clock_stream.last() |
| 44 | + |
| 45 | + self.assertTrue(clock['timestamp'] > prev_clock['timestamp']) |
| 46 | + |
| 47 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sync: |
| 48 | + sync.connect(('localhost', 5000)) |
| 49 | + |
| 50 | + # Now the clock is blocked until we triggered it. |
| 51 | + # Checking it :) |
| 52 | + time.sleep(0.2) |
| 53 | + prev_clock = clock_stream.last() |
| 54 | + time.sleep(0.2) |
| 55 | + clock = clock_stream.last() |
| 56 | + self.assertTrue(clock['timestamp'] == prev_clock['timestamp']) |
| 57 | + |
| 58 | + # triggering once |
| 59 | + sync.send(bytes('foo', 'utf-8')) |
| 60 | + clock = clock_stream.get() |
| 61 | + self.assertAlmostEqual(clock['timestamp'] - prev_clock['timestamp'], 0.1, delta = 0.0001) |
| 62 | + |
| 63 | + # So cool, isn't it :) |
| 64 | + # Close the socket, no more control |
| 65 | + |
| 66 | + prev_clock = clock_stream.last() |
| 67 | + time.sleep(0.2) |
| 68 | + clock = clock_stream.last() |
| 69 | + |
| 70 | + self.assertTrue(clock['timestamp'] > prev_clock['timestamp']) |
| 71 | + |
| 72 | +########################## Run these tests ########################## |
| 73 | +if __name__ == "__main__": |
| 74 | + from morse.testing.testing import main |
| 75 | + main(SocketSyncTest, time_modes = [TimeStrategies.FixedSimulationStep]) |
0 commit comments