def testModbusSingleRequestHandlerHandle(self): handler = socketserver.BaseRequestHandler(None, None, None) handler.__class__ = ModbusSingleRequestHandler handler.framer = Mock() handler.framer.buildPacket.return_value = b"message" handler.request = Mock() handler.request.recv.return_value = b"\x12\x34" # exit if we are not running handler.running = False handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 0) # run forever if we are running def _callback1(a, b): handler.running = False # stop infinite loop handler.framer.processIncomingPacket.side_effect = _callback1 handler.running = True handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 1) # exceptions are simply ignored def _callback2(a, b): if handler.framer.processIncomingPacket.call_count == 2: raise Exception("example exception") else: handler.running = False # stop infinite loop handler.framer.processIncomingPacket.side_effect = _callback2 handler.running = True handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 3)
def testModbusSingleRequestHandlerSend(self): handler = socketserver.BaseRequestHandler(None, None, None) handler.__class__ = ModbusSingleRequestHandler handler.framer = Mock() handler.framer.buildPacket.return_value = b"message" handler.request = Mock() request = ReadCoilsResponse([1]) handler.send(request) self.assertEqual(handler.request.send.call_count, 1) request.should_respond = False handler.send(request) self.assertEqual(handler.request.send.call_count, 1)
def testModbusConnectedRequestHandlerHandle(self): handler = socketserver.BaseRequestHandler(None, None, None) handler.__class__ = ModbusConnectedRequestHandler handler.server = Mock() # handler.server.context.slaves = Mock() # protocol.factory.store.single = True handler.framer = Mock() handler.framer.buildPacket.return_value = b"message" handler.request = Mock() handler.request.recv.return_value = b"\x12\x34" # exit if we are not running handler.running = False handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 0) # run forever if we are running def _callback(a, b, *args, **kwargs): handler.running = False # stop infinite loop handler.framer.processIncomingPacket.side_effect = _callback handler.running = True handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 1) # socket errors cause the client to disconnect handler.framer.processIncomingPacket.side_effect = socket.error() handler.running = True handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 2) # every other exception causes the client to disconnect handler.framer.processIncomingPacket.side_effect = Exception() handler.running = True handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 3) # receiving no data causes the client to disconnect handler.request.recv.return_value = None handler.running = True handler.handle() self.assertEqual(handler.framer.processIncomingPacket.call_count, 4)
def testBaseHandlerUndefinedMethods(self): ''' Test the base handler undefined methods''' handler = socketserver.BaseRequestHandler(None, None, None) handler.__class__ = ModbusBaseRequestHandler self.assertRaises(NotImplementedException, lambda: handler.send(None)) self.assertRaises(NotImplementedException, lambda: handler.handle())