def test_bytes_counter(self): action = master_api.basic_action() fields = {'action_type': 1, 'action_number': 2} pty = DummyPty([action.create_input(1, fields)]) SetUpTestInjections(controller_serial=pty) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() comm.start() pty.fd.write(bytearray(b'hello')) pty.master_reply(action.create_output(1, {'resp': 'OK'})) comm.do_command(action, fields) self.assertEqual(bytearray(b'hello'), comm.get_passthrough_data()) self.assertEqual(21, comm.get_communication_statistics()['bytes_written']) self.assertEqual(5 + 18, comm.get_communication_statistics()['bytes_read'])
def test_passthrough(self): """ Test the passthrough. """ master_pty = DummyPty( [bytearray(b'response'), bytearray(b'more response')]) passthrough_mock = SerialMock([ sin(bytearray(b'data for the passthrough')), sout(bytearray(b'response')), sin(bytearray(b'more data')), sout(bytearray(b'more response')) ]) SetUpTestInjections(controller_serial=master_pty, passthrough_serial=passthrough_mock) master_communicator = MasterCommunicator(init_master=False) master_communicator.enable_passthrough() master_communicator.start() SetUpTestInjections(master_communicator=master_communicator) passthrough = PassthroughService() passthrough.start() master_pty.fd.write(bytearray(b'data for the passthrough')) master_pty.master_wait() master_pty.fd.write(bytearray(b'more data')) master_pty.master_wait() time.sleep(0.2) self.assertEqual( 33, master_communicator.get_communication_statistics()['bytes_read']) self.assertEqual( 21, master_communicator.get_communication_statistics() ['bytes_written']) self.assertEqual(21, passthrough_mock.bytes_read) self.assertEqual(33, passthrough_mock.bytes_written) passthrough.stop()