def test_pull_rx_message_corrupted(): """ Should not receive a corrupted message """ port = MockSerialPort() frame = Frame(port) # this is the serial representation of framed data, with checksum port.serial_data_in = [ frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 21, frame.EOF ] time.sleep(0.1) assert frame.pull_rx_message() == []
def test_pull_rx_message_corrupted(): """ Should not receive a corrupted message """ port = MockSerialPort() frame = Frame(port) # this is the serial representation of framed data, with checksum port.serial_data_in = [frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 21, frame.EOF] time.sleep(0.1) assert frame.pull_rx_message() == []
def test_rx_is_available(): """ When *valid* serial data is received, rx available should not be empty """ port = MockSerialPort() frame = Frame(port) # this is the serial representation of framed data, with checksum port.serial_data_in = [frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 20, frame.EOF] time.sleep(0.1) assert frame.rx_is_available is True
def test_rx_is_available(): """ When *valid* serial data is received, rx available should not be empty """ port = MockSerialPort() frame = Frame(port) # this is the serial representation of framed data, with checksum port.serial_data_in = [ frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 20, frame.EOF ] time.sleep(0.1) assert frame.rx_is_available is True
def test_rx_is_available_empty(): """ At initialization, rx available should be empty """ port = MockSerialPort() frame = Frame(port) assert not frame.rx_is_available
def test_pull_rx_message(): """ Test the retrieval of a message """ port = MockSerialPort() frame = Frame(port) # this is the serial representation of framed data, with checksum port.serial_data_in = [frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 20, frame.EOF] time.sleep(0.1) # this is the expected 'de-framed' data data_to_receive = [1, 2, frame.SOF, 4, frame.EOF, 6, frame.ESC, 8, 9, 10] assert frame.pull_rx_message() == data_to_receive
def test_pull_rx_message(): """ Test the retrieval of a message """ port = MockSerialPort() frame = Frame(port) # this is the serial representation of framed data, with checksum port.serial_data_in = [ frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 20, frame.EOF ] time.sleep(0.1) # this is the expected 'de-framed' data data_to_receive = [1, 2, frame.SOF, 4, frame.EOF, 6, frame.ESC, 8, 9, 10] assert frame.pull_rx_message() == data_to_receive
def test_publish_3_u8(): port = MockSerialPort() sd = SerialDispatch(port) # this is the data that should be published sd.publish('foo', [[10, 20, 30]], ['U8']) # this is the data that should be contained in the serial port outbuffer data_test = [ Frame.SOF, 102, 111, 111, 0, 1, 3, 0, 2, 10, 20, 30, 134, 36, Frame.EOF ] assert port.serial_data_out == data_test
def test_push_tx_message(): """ Test to ensure that a straightforward frame may be sent """ port = MockSerialPort() frame = Frame(port) data_to_send = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] frame.push_tx_message(bytearray(data_to_send)) data_expected = [ frame.SOF, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 55, 220, frame.EOF ] assert set(data_expected) == set(port.serial_data_out)
def test_publish_3_s8(): port = MockSerialPort() sd = SerialDispatch(port) # this is the data that should be published sd.publish('foo', [[-10, -20, -30]], ['S8']) # this is the data that should be contained in the serial port outbuffer data_test = [ Frame.SOF, 102, 111, 111, 0, 1, 3, 0, 3, 246, 246 ^ 0x20, 236, 226, 15, 96, Frame.EOF ] assert port.serial_data_out == data_test
def test_publish_3x3_u8u16u32(): port = MockSerialPort() sd = SerialDispatch(port) # this is the data that should be published sd.publish('foo', [[10, 20, 30], [40, 50, 60], [70, 80, 90]], ['U8', 'U16', 'U32']) # this is the data that should be contained in the serial port outbuffer data_test = [ Frame.SOF, 102, 111, 111, 0, 3, 3, 0, 0x42, 0x06, 10, 20, 30, 40, 0, 50, 0, 60, 0, 70, 0, 0, 0, 80, 0, 0, 0, 90, 0, 0, 0, 84, 186, Frame.EOF ] assert port.serial_data_out == data_test
def test_publish_3x3_s8s16s32(): port = MockSerialPort() sd = SerialDispatch(port) # this is the data that should be published sd.publish('foo', [[-10, -20, -30], [-40, -50, -60], [-70, -80, -90]], ['S8', 'S16', 'S32']) # this is the data that should be contained in the serial port outbuffer data_test = [ Frame.SOF, 102, 111, 111, 0, 3, 3, 0, 0x53, 0x07, 246, 246 ^ 0x20, 236, 226, # <= one of these happens to be an esc char 216, 255, 206, 255, 196, 255, 186, 255, 255, 255, 176, 255, 255, 255, 166, 255, 255, 255, 214, 236, Frame.EOF ] assert port.serial_data_out == data_test
def test_push_tx_message_with_escapes(): """ Test to ensure that a frame with all required escape characters may be sent """ port = MockSerialPort() frame = Frame(port) data_to_send = [1, 2, frame.SOF, 4, frame.EOF, 6, frame.ESC, 8, 9, 10] frame.push_tx_message(bytearray(data_to_send)) data_expected = [ frame.SOF, 1, 2, frame.ESC, frame.SOF ^ frame.ESC_XOR, 4, frame.ESC, frame.EOF ^ frame.ESC_XOR, 6, frame.ESC, frame.ESC ^ frame.ESC_XOR, 8, 9, 10, 148, 20, frame.EOF ] assert set(data_expected) == set(port.serial_data_out)
def test_publish_str(): """ test publish """ port = MockSerialPort() sd = SerialDispatch(port) # this is the data that should be published sd.publish('foo', [['bar']]) # this is the data that should be contained in the serial port outbuffer data_test = [ Frame.SOF, 102, 111, 111, 0, 1, 3, 0, 1, 98, 97, 114, 126, 22, Frame.EOF ] assert port.serial_data_out == data_test
def subscribing_fixture(): global process_data port = MockSerialPort() sd = SerialDispatch(port, threaded=False) process_data = None def toggle_flag(data): global process_data nonlocal sd process_data = data sd.subscribe('foo', toggle_flag) yield port, sd
def sending_fixture(): port = MockSerialPort() sd = SerialDispatch(port) yield port, sd