def test_do_command_passthrough(self): """ Test for the do_command with passthrough data. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock([ sin(action.create_input(1, in_fields)), sout("hello" + action.create_output(1, out_fields)), sin(action.create_input(2, in_fields)), sout(action.create_output(2, out_fields) + "world"), sin(action.create_input(3, in_fields)), sout("hello" + action.create_output(3, out_fields) + " world"), sin(action.create_input(4, in_fields)), sout("hello"), sout(action.create_output(4, out_fields)) ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello", comm.get_passthrough_data()) self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("world", comm.get_passthrough_data()) self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello world", comm.get_passthrough_data()) self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello", comm.get_passthrough_data())
def test_do_command_passthrough(self): """ Test for the do_command with passthrough data. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock( [sin(action.create_input(1, in_fields)), sout("hello" + action.create_output(1, out_fields)), sin(action.create_input(2, in_fields)), sout(action.create_output(2, out_fields) + "world"), sin(action.create_input(3, in_fields)), sout("hello" + action.create_output(3, out_fields) + " world"), sin(action.create_input(4, in_fields)), sout("hello"), sout(action.create_output(4, out_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello", comm.get_passthrough_data()) self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("world", comm.get_passthrough_data()) self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello world", comm.get_passthrough_data()) self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello", comm.get_passthrough_data())
def test_background_consumer(self): """ Test the background consumer mechanism. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock([ sout("OL\x00\x01\x03\x0c\r\n"), sin(action.create_input(1, in_fields)), sout("junkOL\x00\x02\x03\x0c\x05\x06\r\n here"), sout(action.create_output(1, out_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() got_output = {"phase": 1} def callback(output): """ Callback that check if the correct result was returned for OL. """ if got_output["phase"] == 1: self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 2 elif got_output["phase"] == 2: self.assertEquals([(3, int(12 * 10.0 / 6.0)), (5, int(6 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 3 comm.register_consumer(BackgroundConsumer(master_api.output_list(), 0, callback)) comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals(3, got_output["phase"]) self.assertEquals("junk here", comm.get_passthrough_data())
def test_maintenance_passthrough(self): """ Test the behavior of passthrough in maintenance mode. """ serial_mock = SerialMock([ sout("For passthrough"), sin(master_api.to_cli_mode().create_input(0)), sout("OK"), sin("error list\r\n"), sout("the list\n"), sin("exit\r\n"), sout("Passthrough again") ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() def passthrough_thread(): """ Background thread that reads the passthrough data. """ self.assertEquals("For passthrough", comm.get_passthrough_data()) self.assertEquals("Passthrough again", comm.get_passthrough_data()) thread = threading.Thread(target=passthrough_thread) thread.start() comm.start_maintenance_mode() self.assertEquals("OK", comm.get_maintenance_data()) comm.send_maintenance_data("error list\r\n") self.assertEquals("the list\n", comm.get_maintenance_data()) comm.stop_maintenance_mode() thread.join()
def test_maintenance_passthrough(self): """ Test the behavior of passthrough in maintenance mode. """ serial_mock = SerialMock([ sout("For passthrough"), sin(master_api.to_cli_mode().create_input(0)), sout("OK"), sin("error list\r\n"), sout("the list\n"), sin("exit\r\n"), sout("Passthrough again")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() def passthrough_thread(): """ Background thread that reads the passthrough data. """ self.assertEquals("For passthrough", comm.get_passthrough_data()) self.assertEquals("Passthrough again", comm.get_passthrough_data()) thread = threading.Thread(target=passthrough_thread) thread.start() comm.start_maintenance_mode() self.assertEquals("OK", comm.get_maintenance_data()) comm.send_maintenance_data("error list\r\n") self.assertEquals("the list\n", comm.get_maintenance_data()) comm.stop_maintenance_mode() thread.join()
def test_passthrough(self): """ Test the passthrough. """ master_mock = SerialMock([ sout("data for the passthrough"), sin("response"), sout("more data"), sin("more response")]) passthrough_mock = SerialMock([ sin("data for the passthrough"), sout("response"), sin("more data"), sout("more response")]) master_communicator = MasterCommunicator(master_mock, init_master=False) master_communicator.enable_passthrough() master_communicator.start() passthrough = PassthroughService(master_communicator, passthrough_mock) passthrough.start() time.sleep(1) self.assertEquals(33, master_communicator.get_bytes_read()) self.assertEquals(21, master_communicator.get_bytes_written()) self.assertEquals(33, master_mock.bytes_read) self.assertEquals(21, master_mock.bytes_written) self.assertEquals(21, passthrough_mock.bytes_read) self.assertEquals(33, passthrough_mock.bytes_written) passthrough.stop()
def test_passthrough_output(self): """ Test the passthrough output if no other communications are going on. """ serial_mock = SerialMock([sout("passthrough"), sout(" my "), sout("data")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() self.assertEquals("passthrough", comm.get_passthrough_data()) self.assertEquals(" my ", comm.get_passthrough_data()) self.assertEquals("data", comm.get_passthrough_data())
def test_send_passthrough_data(self): """ Test the passthrough if no other communications are going on. """ pt_input = "data from passthrough" pt_output = "got it !" serial_mock = SerialMock([sin(pt_input), sout(pt_output)]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() comm.send_passthrough_data(pt_input) self.assertEquals(pt_output, comm.get_passthrough_data())
def test_passthrough_output(self): """ Test the passthrough output if no other communications are going on. """ serial_mock = SerialMock( [sout("passthrough"), sout(" my "), sout("data")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() self.assertEquals("passthrough", comm.get_passthrough_data()) self.assertEquals(" my ", comm.get_passthrough_data()) self.assertEquals("data", comm.get_passthrough_data())
def test_passthrough(self): """ Test the passthrough. """ master_mock = SerialMock([ sout("data for the passthrough"), sin("response"), sout("more data"), sin("more response") ]) passthrough_mock = SerialMock([ sin("data for the passthrough"), sout("response"), sin("more data"), sout("more response") ]) SetUpTestInjections(controller_serial=master_mock, 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() time.sleep(1) self.assertEquals( 33, master_communicator.get_communication_statistics()['bytes_read']) self.assertEquals( 21, master_communicator.get_communication_statistics() ['bytes_written']) self.assertEquals(33, master_mock.bytes_read) self.assertEquals(21, master_mock.bytes_written) self.assertEquals(21, passthrough_mock.bytes_read) self.assertEquals(33, passthrough_mock.bytes_written) passthrough.stop()
def test_background_consumer_passthrough(self): """ Test the background consumer with passing the data to the passthrough. """ serial_mock = SerialMock([sout("OL\x00\x01"), sout("\x03\x0c\r\n")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() got_output = {"passed": False} def callback(output): """ Callback that check if the correct result was returned for OL. """ self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["passed"] = True comm.register_consumer(BackgroundConsumer(master_api.output_list(), 0, callback, True)) comm.start() MasterCommunicatorTest._wait_for_callback(True, got_output, 3) self.assertEquals(True, got_output["passed"]) self.assertEquals("OL\x00\x01\x03\x0c\r\n", comm.get_passthrough_data())
def test_bytes_counter(self): """ Test the number of bytes written and read from the serial port. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock( [sin(action.create_input(1, in_fields)), sout("hello"), sout(action.create_output(1, out_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello", comm.get_passthrough_data()) self.assertEquals(21, comm.get_bytes_written()) self.assertEquals(5 + 18, comm.get_bytes_read())
def test_background_consumer_passthrough(self): """ Test the background consumer with passing the data to the passthrough. """ serial_mock = SerialMock([sout("OL\x00\x01"), sout("\x03\x0c\r\n")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() got_output = {"passed": False} def callback(output): """ Callback that check if the correct result was returned for OL. """ self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["passed"] = True comm.register_consumer( BackgroundConsumer(master_api.output_list(), 0, callback, True)) comm.start() self.assertEquals(True, got_output["passed"]) self.assertEquals("OL\x00\x01\x03\x0c\r\n", comm.get_passthrough_data())
def test_bytes_counter(self): """ Test the number of bytes written and read from the serial port. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock([ sin(action.create_input(1, in_fields)), sout("hello"), sout(action.create_output(1, out_fields)) ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals("hello", comm.get_passthrough_data()) self.assertEquals(21, comm.get_bytes_written()) self.assertEquals(5 + 18, comm.get_bytes_read())
def test_background_consumer(self): """ Test the background consumer mechanism. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock([ sout("OL\x00\x01\x03\x0c\r\n"), sin(action.create_input(1, in_fields)), sout("junkOL\x00\x02\x03\x0c\x05\x06\r\n here"), sout(action.create_output(1, out_fields)) ]) SetUpTestInjections(controller_serial=serial_mock) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() got_output = {"phase": 1} def callback(output): """ Callback that check if the correct result was returned for OL. """ if got_output["phase"] == 1: self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 2 elif got_output["phase"] == 2: self.assertEquals([(3, int(12 * 10.0 / 6.0)), (5, int(6 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 3 comm.register_consumer( BackgroundConsumer(master_api.output_list(), 0, callback)) comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals(3, got_output["phase"]) self.assertEquals("junk here", comm.get_passthrough_data())
def test_passthrough(self): """ Test the passthrough. """ master_mock = SerialMock([ sout("data for the passthrough"), sin("response"), sout("more data"), sin("more response") ]) passthrough_mock = SerialMock([ sin("data for the passthrough"), sout("response"), sin("more data"), sout("more response") ]) master_communicator = MasterCommunicator(master_mock, init_master=False) master_communicator.enable_passthrough() master_communicator.start() passthrough = PassthroughService(master_communicator, passthrough_mock) passthrough.start() time.sleep(1) self.assertEquals(33, master_communicator.get_bytes_read()) self.assertEquals(21, master_communicator.get_bytes_written()) self.assertEquals(33, master_mock.bytes_read) self.assertEquals(21, master_mock.bytes_written) self.assertEquals(21, passthrough_mock.bytes_read) self.assertEquals(33, passthrough_mock.bytes_written) passthrough.stop()