def testConnectionMade(self): protocol = ModbusTcpProtocol() protocol.transport = MagicMock() protocol.factory = MagicMock() protocol.factory.framer = ModbusSocketFramer protocol.connectionMade() self.assertIsInstance(protocol.framer, ModbusSocketFramer)
def testTcpExecuteFailure(self): protocol = ModbusTcpProtocol() protocol.factory = MagicMock() protocol.factory.store = MagicMock() protocol.store = MagicMock() protocol.factory.ignore_missing_slaves = False request = MagicMock() protocol._send = MagicMock() # CASE-1: test NoSuchSlaveException exceptions request.execute.side_effect = NoSuchSlaveException() self.assertRaises( NoSuchSlaveException, protocol._execute(request) ) self.assertTrue(request.doException.called) # CASE-2: NoSuchSlaveException with ignore_missing_slaves = true protocol.ignore_missing_slaves = True request.execute.side_effect = NoSuchSlaveException() self.assertEqual(protocol._execute(request), None) # test other exceptions request.execute.side_effect = ModbusIOException() self.assertRaises( ModbusIOException, protocol._execute(request) ) self.assertTrue(protocol._send.called)
def testDataReceived(self): protocol = ModbusTcpProtocol() # mock_data = "Hellow world!" mock_data = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34" protocol.factory = MagicMock() protocol.factory.control.ListenOnly = False protocol.factory.store = [byte2int(mock_data[0])] protocol.framer = protocol._execute = MagicMock() protocol.dataReceived(mock_data) self.assertTrue(protocol.framer.processIncomingPacket.called) # test datareceived returns None protocol.factory.control.ListenOnly = False self.assertEqual(protocol.dataReceived(mock_data), None)
def testSendTcp(self): class MockMsg(object): def __init__(self, msg, resp=False): self.should_respond = resp self.msg = msg mock_msg = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34" protocol = ModbusTcpProtocol() mock_data = MockMsg(resp=True, msg=mock_msg) protocol.control = MagicMock() protocol.framer = MagicMock() protocol.factory = MagicMock() protocol.framer.buildPacket = MagicMock(return_value=mock_msg) protocol.transport = MagicMock() protocol._send(mock_data) self.assertTrue(protocol.framer.buildPacket.called) self.assertTrue(protocol.transport.write.called) mock_data = MockMsg(resp=False, msg="helloworld") self.assertEqual(protocol._send(mock_data), None)
def testTcpExecuteFailure(self): protocol = ModbusTcpProtocol() protocol.factory = MagicMock() protocol.factory.store = MagicMock() protocol.store = MagicMock() protocol.factory.ignore_missing_slaves = False request = MagicMock() protocol._send = MagicMock() # CASE-1: test NoSuchSlaveException exceptions request.execute.side_effect = NoSuchSlaveException() self.assertRaises(NoSuchSlaveException, protocol._execute(request)) self.assertTrue(request.doException.called) # CASE-2: NoSuchSlaveException with ignore_missing_slaves = true protocol.ignore_missing_slaves = True request.execute.side_effect = NoSuchSlaveException() self.assertEqual(protocol._execute(request), None) # test other exceptions request.execute.side_effect = ModbusIOException() self.assertRaises(ModbusIOException, protocol._execute(request)) self.assertTrue(protocol._send.called)
def testSendTcp(self): class MockMsg(object): def __init__(self, msg, resp=False): self.should_respond = resp self.msg = msg mock_msg = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34" protocol = ModbusTcpProtocol() mock_data = MockMsg(resp=True, msg=mock_msg) protocol.control = MagicMock() protocol.framer = MagicMock() protocol.factory = MagicMock() protocol.framer.buildPacket = MagicMock(return_value=mock_msg) protocol.transport= MagicMock() protocol._send(mock_data) self.assertTrue(protocol.framer.buildPacket.called) self.assertTrue(protocol.transport.write.called) mock_data =MockMsg(resp=False, msg="helloworld") self.assertEqual(protocol._send(mock_data), None)