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_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_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.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_output(self): """ Test the passthrough output if no other communications are going on. """ serial_mock = SerialMock( [sout("passthrough"), sout(" my "), sout("data")]) SetUpTestInjections(controller_serial=serial_mock) comm = MasterCommunicator(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_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_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.start() comm.send_passthrough_data(pt_input) self.assertEquals(pt_output, 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_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_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) 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_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.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())