def test_passthrough_with_commands(self): action = master_api.basic_action(self.master_version) fields = {'action_type': 1, 'action_number': 2} pty = DummyPty([ action.create_input(1, fields), action.create_input(2, fields), action.create_input(3, fields) ]) SetUpTestInjections(controller_serial=pty) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() comm.start() pty.master_reply( bytearray(b'hello') + action.create_output(1, {'resp': 'OK'})) self.assertEqual('OK', comm.do_command(action, fields)['resp']) self.assertEqual(bytearray(b'hello'), comm.get_passthrough_data()) pty.master_reply( action.create_output(2, {'resp': 'OK'}) + bytearray(b'world')) self.assertEqual('OK', comm.do_command(action, fields)['resp']) self.assertEqual(bytearray(b'world'), comm.get_passthrough_data()) pty.master_reply( bytearray(b'hello') + action.create_output(3, {'resp': 'OK'}) + bytearray(b' world')) self.assertEqual('OK', comm.do_command(action, fields)['resp']) self.assertEqual(bytearray(b'hello world'), comm.get_passthrough_data())
def test_background_consumer_passthrough(self): action = master_api.basic_action() fields = {'action_type': 1, 'action_number': 2} pty = DummyPty([action.create_input(1, fields)]) SetUpTestInjections(controller_serial=pty) got_output = {'passed': False} def callback(output_): """ Callback that check if the correct result was returned for OL. """ self.assertEqual([(3, int(12 * 10.0 / 6.0))], output_['outputs']) got_output['passed'] = True consumer = BackgroundConsumer(master_api.output_list(), 0, callback, True) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() comm.register_consumer(consumer) comm.start() pty.fd.write(bytearray(b'OL\x00\x01')) pty.fd.write(bytearray(b'\x03\x0c\r\n')) pty.master_reply(action.create_output(1, {'resp': 'OK'})) output = comm.do_command(action, fields) self.assertEqual('OK', output['resp']) try: consumer._consume() except: pass # Just ensure it has at least consumed once self.assertEqual(True, got_output['passed']) self.assertEqual(bytearray(b'OL\x00\x01\x03\x0c\r\n'), comm.get_passthrough_data())
def test_background_consumer(self): action = master_api.basic_action() fields = {'action_type': 1, 'action_number': 2} pty = DummyPty([action.create_input(1, fields)]) SetUpTestInjections(controller_serial=pty) got_output = {'phase': 1} def callback(output): """ Callback that check if the correct result was returned for OL. """ if got_output['phase'] == 1: self.assertEqual([(3, int(12 * 10.0 / 6.0))], output['outputs']) got_output['phase'] = 2 elif got_output['phase'] == 2: self.assertEqual([(3, int(12 * 10.0 / 6.0)), (5, int(6 * 10.0 / 6.0))], output['outputs']) got_output['phase'] = 3 comm = MasterCommunicator(init_master=False) comm.enable_passthrough() comm.register_consumer(BackgroundConsumer(master_api.output_list(), 0, callback)) comm.start() pty.fd.write(bytearray(b'OL\x00\x01\x03\x0c\r\n')) pty.fd.write(bytearray(b'junkOL\x00\x02\x03\x0c\x05\x06\r\n here')) pty.master_reply(action.create_output(1, {'resp': 'OK'})) output = comm.do_command(action, fields) self.assertEqual('OK', output['resp']) time.sleep(0.2) self.assertEqual(3, got_output['phase']) self.assertEqual(bytearray(b'junk here'), comm.get_passthrough_data())
def test_passthrough(self): pty = DummyPty([b'data from passthrough']) SetUpTestInjections(controller_serial=pty) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() comm.start() pty.master_reply(bytearray(b'got it!')) comm.send_passthrough_data(bytearray(b'data from passthrough')) self.assertEqual(bytearray(b'got it!'), comm.get_passthrough_data())
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'])