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 testTcpExecuteSuccess(self): protocol = ModbusTcpProtocol() protocol.store = MagicMock() request = MagicMock() protocol._send = MagicMock() # tst if _send being called 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)
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.store = MagicMock() 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)