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.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_address_mode_timeout(self): """ Test address mode timeout. """ action = power_api.get_voltage(power_api.POWER_MODULE) sad = power_api.set_addressmode(power_api.POWER_MODULE) sad_p1c = power_api.set_addressmode(power_api.P1_CONCENTRATOR) serial_mock = RS485( SerialMock( [ sin( sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sin( sad_p1c.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.ADDRESS_MODE)), sout(''), # Timeout read after 1 second sin( sad.create_input(power_api.BROADCAST_ADDRESS, 3, power_api.NORMAL_MODE)), sin( sad_p1c.create_input(power_api.BROADCAST_ADDRESS, 4, power_api.NORMAL_MODE)), sin(action.create_input(1, 5)), sout(action.create_output(1, 5, 49.5)) ], 1)) comm = PowerCommunicatorTest._get_communicator(serial_mock, address_mode_timeout=1) comm.start() comm.start_address_mode() time.sleep(1.1) self.assertEquals((49.5, ), comm.do_command(1, action))
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_in_address_mode(self): """ Test the behavior of do_command in address mode.""" action = power_api.get_voltage(power_api.POWER_API_8_PORTS) sad = power_api.set_addressmode() serial_mock = RS485(SerialMock( [sin(sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sout(''), ## Timeout read after 1 second sin(sad.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.NORMAL_MODE)), sin(action.create_input(1, 3)), sout(action.create_output(1, 3, 49.5)) ], 1)) comm = self.__get_communicator(serial_mock) comm.start() comm.start_address_mode() try: comm.do_command(1, action) self.assertFalse(True) except InAddressModeException: pass comm.stop_address_mode() self.assertEquals((49.5, ), comm.do_command(1, action))
def test_address_mode(self): """ Test the address mode. """ sad = power_api.set_addressmode() serial_mock = RS485(SerialMock( [sin(sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sout(power_api.want_an_address(power_api.POWER_API_8_PORTS).create_output(0, 0)), sin(power_api.set_address().create_input(0, 0, 1)), sout(power_api.want_an_address(power_api.POWER_API_12_PORTS).create_output(0, 0)), sin(power_api.set_address().create_input(0, 0, 2)), sout(''), ## Timeout read after 1 second sin(sad.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.NORMAL_MODE)) ], 1)) controller = PowerController(PowerCommunicatorTest.FILE) comm = self.__get_communicator(serial_mock, power_controller=controller) comm.start() self.assertEqual(controller.get_free_address(), 1) comm.start_address_mode() self.assertTrue(comm.in_address_mode()) time.sleep(0.5) comm.stop_address_mode() self.assertEqual(controller.get_free_address(), 3) self.assertFalse(comm.in_address_mode())
def test_maintenance_mode(self): """ Test the maintenance mode. """ serial_mock = SerialMock([ sin(master_api.to_cli_mode().create_input(0)), sout("OK"), sin("error list\r\n"), sout("the list\n"), sin("exit\r\n") ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() comm.start_maintenance_mode() try: comm.send_passthrough_data("test") self.assertTrue(False) except InMaintenanceModeException: pass try: comm.do_command(None, None) self.assertTrue(False) except InMaintenanceModeException: pass 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()
def test_timekeeper(self): """ Test the TimeKeeper. """ SetUpTestInjections(power_db=PowerCommunicatorTest.FILE) power_controller = PowerController() power_controller.register_power_module(1, power_api.POWER_MODULE) time_action = power_api.set_day_night(power_api.POWER_MODULE) times = [power_api.NIGHT for _ in range(8)] action = power_api.get_voltage(power_api.POWER_MODULE) serial_mock = RS485( SerialMock([ sin(time_action.create_input(1, 1, *times)), sout(time_action.create_output(1, 1)), sin(action.create_input(1, 2)), sout(action.create_output(1, 2, 243)) ], 1)) comm = PowerCommunicatorTest._get_communicator( serial_mock, 1, power_controller=power_controller) comm.start() time.sleep(1.5) self.assertEquals((243, ), comm.do_command(1, action))
def test_do_command_in_address_mode(self): """ Test the behavior of do_command in address mode.""" action = power_api.get_voltage(power_api.POWER_API_8_PORTS) sad = power_api.set_addressmode() serial_mock = RS485(SerialMock( [sin(sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sout(''), # Timeout read after 1 second sin(sad.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.NORMAL_MODE)), sin(action.create_input(1, 3)), sout(action.create_output(1, 3, 49.5))], 1 )) comm = self.__get_communicator(serial_mock) comm.start() comm.start_address_mode() try: comm.do_command(1, action) self.assertFalse(True) except InAddressModeException: pass comm.stop_address_mode() self.assertEquals((49.5, ), comm.do_command(1, action))
def test_crc_checking(self): """ Test the crc checking in the MasterCommunciator. """ action = master_api.sensor_humidity_list() out_fields = {} for i in range(0, 32): out_fields['hum%d' % i] = master_api.Svt(master_api.Svt.RAW, i) out_fields['crc'] = [ord('C'), 1, 240] out_fields2 = {} for i in range(0, 32): out_fields2['hum%d' % i] = master_api.Svt(master_api.Svt.RAW, 2 * i) out_fields2['crc'] = [ord('C'), 0, 0] serial_mock = SerialMock([sin(action.create_input(1)), sout(action.create_output(1, out_fields)), sin(action.create_input(2)), sout(action.create_output(2, out_fields2))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() output = comm.do_command(action) self.assertEquals('\x00', output['hum0'].get_byte()) self.assertEquals('\x01', output['hum1'].get_byte()) self.assertRaises(CrcCheckFailedException, lambda: comm.do_command(action))
def test_address_mode(self): """ Test the address mode. """ sad = power_api.set_addressmode() serial_mock = RS485(SerialMock( [sin(sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sout(power_api.want_an_address(power_api.POWER_API_8_PORTS).create_output(0, 0)), sin(power_api.set_address().create_input(0, 0, 1)), sout(power_api.want_an_address(power_api.POWER_API_12_PORTS).create_output(0, 0)), sin(power_api.set_address().create_input(0, 0, 2)), sout(''), # Timeout read after 1 second sin(sad.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.NORMAL_MODE))], 1 )) SetUpTestInjections(power_db=PowerCommunicatorTest.FILE) controller = PowerController() comm = self.__get_communicator(serial_mock, power_controller=controller) comm.start() self.assertEqual(controller.get_free_address(), 1) comm.start_address_mode() self.assertTrue(comm.in_address_mode()) time.sleep(0.5) comm.stop_address_mode() self.assertEqual(controller.get_free_address(), 3) self.assertFalse(comm.in_address_mode())
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_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_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_mode(self): """ Test the maintenance mode. """ serial_mock = SerialMock([sin(master_api.to_cli_mode().create_input(0)), sout("OK"), sin("error list\r\n"), sout("the list\n"), sin("exit\r\n")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() comm.start_maintenance_mode() try: comm.send_passthrough_data("test") self.assertTrue(False) except InMaintenanceModeException: pass try: comm.do_command(None, None) self.assertTrue(False) except InMaintenanceModeException: pass 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()
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_crc_checking(self): """ Test the crc checking in the MasterCommunciator. """ action = master_api.sensor_humidity_list() out_fields = {} for i in range(0, 32): out_fields['hum%d' % i] = master_api.Svt(master_api.Svt.RAW, i) out_fields['crc'] = [ord('C'), 1, 240] out_fields2 = {} for i in range(0, 32): out_fields2['hum%d' % i] = master_api.Svt(master_api.Svt.RAW, 2 * i) out_fields2['crc'] = [ord('C'), 0, 0] serial_mock = SerialMock([ sin(action.create_input(1)), sout(action.create_output(1, out_fields)), sin(action.create_input(2)), sout(action.create_output(2, out_fields2)) ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() output = comm.do_command(action) self.assertEquals('\x00', output['hum0'].get_byte()) self.assertEquals('\x01', output['hum1'].get_byte()) self.assertRaises(CrcCheckFailedException, lambda: comm.do_command(action))
def test_watchdog(self): """ Test the watchdog. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} serial_mock = SerialMock([ sin(action.create_input(1, in_fields)), sin(action.create_input(2, in_fields)), sin(action.create_input(3, in_fields)) ]) timeout = False watchdog = {} def callback(): """ Callback for the watchdog """ watchdog['done'] = True comm = MasterCommunicator(serial_mock, init_master=False, watchdog_period=0.5, watchdog_callback=callback) comm.start() try: comm.do_command(action, in_fields, timeout=0.1) except CommunicationTimedOutException: timeout = True time.sleep(1) self.assertTrue(timeout) self.assertFalse('done' in watchdog) timeout = False try: comm.do_command(action, in_fields, timeout=0.1) except CommunicationTimedOutException: timeout = True self.assertTrue(timeout) timeout = False try: comm.do_command(action, in_fields, timeout=0.1) except CommunicationTimedOutException: timeout = True time.sleep(1.5) self.assertTrue(timeout) self.assertTrue('done' in watchdog)
def test_do_command_timeout_once(self): """ Test for timeout in PowerCommunicator.do_command. """ action = power_api.get_voltage(power_api.POWER_API_8_PORTS) serial_mock = RS485(SerialMock([sin(action.create_input(1, 1)), sout(''), sin(action.create_input(1, 2)), sout(action.create_output(1, 2, 49.5))])) comm = self.__get_communicator(serial_mock) comm.start() output = comm.do_command(1, action) self.assertEquals((49.5, ), output)
def test_do_command_timeout_twice(self): """ Test for timeout in PowerCommunicator.do_command. """ action = power_api.get_voltage(power_api.POWER_API_8_PORTS) serial_mock = RS485(SerialMock([sin(action.create_input(1, 1)), sout(''), sin(action.create_input(1, 2)), sout('')])) comm = self.__get_communicator(serial_mock) comm.start() try: comm.do_command(1, action) self.fail("Should receive timed out exception !") except CommunicationTimedOutException: pass # Ok !
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_do_command_timeout_twice(self): """ Test for timeout in PowerCommunicator.do_command. """ action = power_api.get_voltage(power_api.POWER_MODULE) serial_mock = RS485( SerialMock([ sin(action.create_input(1, 1)), sout(''), sin(action.create_input(1, 2)), sout('') ])) comm = PowerCommunicatorTest._get_communicator(serial_mock) comm.start() with self.assertRaises(CommunicationTimedOutException): comm.do_command(1, action)
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_address_mode_timeout(self): """ Test address mode timeout. """ action = power_api.get_voltage(power_api.POWER_API_8_PORTS) sad = power_api.set_addressmode() serial_mock = RS485(SerialMock( [sin(sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sout(''), ## Timeout read after 1 second sin(sad.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.NORMAL_MODE)), sin(action.create_input(1, 3)), sout(action.create_output(1, 3, 49.5)) ], 1)) comm = self.__get_communicator(serial_mock, address_mode_timeout=1) comm.start() comm.start_address_mode() time.sleep(1.1) self.assertEquals((49.5, ), comm.do_command(1, action))
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_do_command_timeout_test_ongoing(self): """ Test if communication resumes after timeout. """ 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)), sin(action.create_input(2, in_fields)), sout(action.create_output(2, out_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() try: comm.do_command(action, in_fields, timeout=0.1) self.assertTrue(False) except CommunicationTimedOutException: pass output = comm.do_command(action, in_fields) self.assertEquals("OK", output["resp"])
def test_timekeeper(self): """ Test the TimeKeeper. """ power_controller = PowerController(PowerCommunicatorTest.FILE) power_controller.register_power_module(1, power_api.POWER_API_8_PORTS) time_action = power_api.set_day_night(power_api.POWER_API_8_PORTS) times = [power_api.NIGHT for _ in range(8)] action = power_api.get_voltage(power_api.POWER_API_8_PORTS) serial_mock = RS485(SerialMock( [sin(time_action.create_input(1, 1, *times)), sout(time_action.create_output(1, 1)), sin(action.create_input(1, 2)), sout(action.create_output(1, 2, 243)) ], 1)) comm = self.__get_communicator(serial_mock, 1, power_controller=power_controller) comm.start() time.sleep(1.5) self.assertEquals((243, ), comm.do_command(1, action))
def test_do_command_in_address_mode(self): """ Test the behavior of do_command in address mode.""" action = power_api.get_voltage(power_api.POWER_MODULE) sad = power_api.set_addressmode(power_api.POWER_MODULE) sad_p1c = power_api.set_addressmode(power_api.P1_CONCENTRATOR) serial_mock = RS485( SerialMock( [ sin( sad.create_input(power_api.BROADCAST_ADDRESS, 1, power_api.ADDRESS_MODE)), sin( sad_p1c.create_input(power_api.BROADCAST_ADDRESS, 2, power_api.ADDRESS_MODE)), sout(''), # Timeout read after 1 second sin( sad.create_input(power_api.BROADCAST_ADDRESS, 3, power_api.NORMAL_MODE)), sin( sad_p1c.create_input(power_api.BROADCAST_ADDRESS, 4, power_api.NORMAL_MODE)), sin(action.create_input(1, 5)), sout(action.create_output(1, 5, 49.5)) ], 1)) comm = PowerCommunicatorTest._get_communicator(serial_mock) comm.start() comm.start_address_mode() with self.assertRaises(InAddressModeException): comm.do_command(1, action) comm.stop_address_mode() self.assertEquals((49.5, ), comm.do_command(1, action))
def test_do_command_timeout_test_ongoing(self): """ Test if communication resumes after timeout. """ 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)), sin(action.create_input(2, in_fields)), sout(action.create_output(2, out_fields)) ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() try: comm.do_command(action, in_fields, timeout=0.1) self.assertTrue(False) except CommunicationTimedOutException: pass output = comm.do_command(action, in_fields) self.assertEquals("OK", output["resp"])
def test_do_command_split_data(self): """ Test PowerCommunicator.do_command when the data is split over multiple reads. """ action = power_api.get_voltage(power_api.POWER_API_8_PORTS) out = action.create_output(1, 1, 49.5) serial_mock = RS485(SerialMock( [sin(action.create_input(1, 1)), sout(out[:5]), sout(out[5:])])) comm = self.__get_communicator(serial_mock) comm.start() output = comm.do_command(1, action) self.assertEquals((49.5, ), output)
def test_do_command_timeout(self): """ Test for timeout in MasterCommunicator.do_command. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} serial_mock = SerialMock([sin(action.create_input(1, in_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() try: comm.do_command(action, in_fields, timeout=0.1) self.assertTrue(False) except CommunicationTimedOutException: pass
def test_do_command(self): """ Test for standard behavior MasterCommunicator.do_command. """ 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(action.create_output(1, out_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() output = comm.do_command(action, in_fields) self.assertEquals("OK", output["resp"])
def test_wrong_response(self): """ Test PowerCommunicator.do_command when the power module returns a wrong response. """ action_1 = power_api.get_voltage(power_api.POWER_MODULE) action_2 = power_api.get_frequency(power_api.POWER_MODULE) serial_mock = RS485( SerialMock([ sin(action_1.create_input(1, 1)), sout(action_2.create_output(3, 2, 49.5)) ])) comm = PowerCommunicatorTest._get_communicator(serial_mock) comm.start() with self.assertRaises(Exception): comm.do_command(1, action_1)
def test_wrong_response(self): """ Test PowerCommunicator.do_command when the power module returns a wrong response. """ action_1 = power_api.get_voltage(power_api.POWER_API_8_PORTS) action_2 = power_api.get_frequency(power_api.POWER_API_8_PORTS) serial_mock = RS485(SerialMock([sin(action_1.create_input(1, 1)), sout(action_2.create_output(3, 2, 49.5))])) comm = self.__get_communicator(serial_mock) comm.start() try: comm.do_command(1, action_1) self.assertTrue(False) except Exception: pass
def test_do_command(self): """ Test for standard behavior MasterCommunicator.do_command. """ 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(action.create_output(1, out_fields)) ]) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() output = comm.do_command(action, in_fields) self.assertEquals("OK", output["resp"])
def test_do_command(self): """ Test for standard behavior PowerCommunicator.do_command. """ action = power_api.get_voltage(power_api.POWER_API_8_PORTS) serial_mock = RS485(SerialMock( [sin(action.create_input(1, 1)), sout(action.create_output(1, 1, 49.5))])) comm = self.__get_communicator(serial_mock) comm.start() output = comm.do_command(1, action) self.assertEquals((49.5, ), output) self.assertEquals(14, comm.get_bytes_written()) self.assertEquals(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_do_command_split_data(self): """ Test MasterCommunicator.do_command when the data is split over multiple reads. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} sequence = [] for i in range(1, 18): sequence.append(sin(action.create_input(i, in_fields))) output_bytes = action.create_output(i, out_fields) sequence.append(sout(output_bytes[:i])) sequence.append(sout(output_bytes[i:])) serial_mock = SerialMock(sequence) comm = MasterCommunicator(serial_mock, init_master=False) comm.start() for i in range(1, 18): self.assertEquals("OK", comm.do_command(action, in_fields)["resp"])
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_do_command(self): """ Test for standard behavior PowerCommunicator.do_command. """ action = power_api.get_voltage(power_api.POWER_MODULE) serial_mock = RS485( SerialMock([ sin(action.create_input(1, 1)), sout(action.create_output(1, 1, 49.5)) ])) comm = PowerCommunicatorTest._get_communicator(serial_mock) comm.start() output = comm.do_command(1, action) self.assertEquals((49.5, ), output) self.assertEquals(14, comm.get_communication_statistics()['bytes_written']) self.assertEquals(18, comm.get_communication_statistics()['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_pulse_counter_status(self): action = master_api.pulse_list() in_fields = {} out_fields = {'pv0': 0, 'pv1': 1, 'pv2': 2, 'pv3': 3, 'pv4': 4, 'pv5': 5, 'pv6': 6, 'pv7': 7, 'pv8': 8, 'pv9': 9, 'pv10': 10, 'pv11': 11, 'pv12': 12, 'pv13': 13, 'pv14': 14, 'pv15': 15, 'pv16': 16, 'pv17': 17, 'pv18': 18, 'pv19': 19, 'pv20': 20, 'pv21': 21, 'pv22': 22, 'pv23': 23, 'crc': [67, 1, 20]} serial_mock = SerialMock([sin(action.create_input(1, in_fields)), sout(action.create_output(1, out_fields))]) master_communicator = MasterCommunicator(serial_mock, init_master=False) master_communicator.start() controller = self._get_controller(master_communicator) controller.set_pulse_counter_amount(26) controller.set_pulse_counter_status(24, 123) controller.set_pulse_counter_status(25, 456) status = controller.get_pulse_counter_status() self.assertEquals(range(0, 24) + [123, 456], status) # Set pulse counter for unexisting pulse counter try: controller.set_pulse_counter_status(26, 789) self.fail('Exception should have been thrown') except ValueError as e: self.assertEquals('Could not find pulse counter 26', str(e)) # Set pulse counter for physical pulse counter try: controller.set_pulse_counter_status(23, 789) self.fail('Exception should have been thrown') except ValueError as e: self.assertEquals('Cannot set pulse counter status for 23 (should be > 23)', str(e))
def test_pulse_counter_status(self): action = master_api.pulse_list() in_fields = {} out_fields = { 'pv0': 0, 'pv1': 1, 'pv2': 2, 'pv3': 3, 'pv4': 4, 'pv5': 5, 'pv6': 6, 'pv7': 7, 'pv8': 8, 'pv9': 9, 'pv10': 10, 'pv11': 11, 'pv12': 12, 'pv13': 13, 'pv14': 14, 'pv15': 15, 'pv16': 16, 'pv17': 17, 'pv18': 18, 'pv19': 19, 'pv20': 20, 'pv21': 21, 'pv22': 22, 'pv23': 23, 'crc': [67, 1, 20] } serial_mock = SerialMock([ sin(action.create_input(1, in_fields)), sout(action.create_output(1, out_fields)) ]) SetUpTestInjections(controller_serial=serial_mock) master_communicator = MasterCommunicator(init_master=False) master_communicator.start() controller = self._get_controller(master_communicator) controller.set_pulse_counter_amount(26) controller.set_pulse_counter_status(24, 123) controller.set_pulse_counter_status(25, 456) status = controller.get_pulse_counter_status() self.assertEquals(range(0, 24) + [123, 456], status) # Set pulse counter for unexisting pulse counter try: controller.set_pulse_counter_status(26, 789) self.fail('Exception should have been thrown') except ValueError as e: self.assertEquals('Could not find pulse counter 26', str(e)) # Set pulse counter for physical pulse counter try: controller.set_pulse_counter_status(23, 789) self.fail('Exception should have been thrown') except ValueError as e: self.assertEquals( 'Cannot set pulse counter status for 23 (should be > 23)', str(e))