From b7ce6d97947a84c18ea6b0c110a4e7e07a198182 Mon Sep 17 00:00:00 2001 From: dhoomakethu Date: Fri, 4 Aug 2017 11:54:52 +0530 Subject: [PATCH] unit tests for #191, #149 , fix obvious server_start errors --- pymodbus/server/sync.py | 20 ++++++++++++-------- test/test_client_sync.py | 7 +++++++ test/test_server_sync.py | 27 +++++++++++++++++---------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/pymodbus/server/sync.py b/pymodbus/server/sync.py index 1dfe8ec7d..4559513f2 100644 --- a/pymodbus/server/sync.py +++ b/pymodbus/server/sync.py @@ -405,9 +405,9 @@ def __init__(self, context, framer=None, identity=None, **kwargs): self.timeout = kwargs.get('timeout', Defaults.Timeout) self.ignore_missing_slaves = kwargs.get('ignore_missing_slaves', Defaults.IgnoreMissingSlaves) self.socket = None - self._connect() - self.is_running = True - self._build_handler() + if self._connect(): + self.is_running = True + self._build_handler() def _connect(self): ''' Connect to the serial server @@ -429,6 +429,7 @@ def _build_handler(self): :returns: A patched handler ''' + request = self.socket request.send = request.write request.recv = request.read @@ -442,11 +443,14 @@ def serve_forever(self): :param request: The request to handle :param client: The address of the client ''' - _logger.debug("Started thread to serve client") - if not self.handler: - self._build_handler() - while self.is_running: - self.handler.handle() + if self._connect(): + _logger.debug("Started thread to serve client") + if not self.handler: + self._build_handler() + while self.is_running: + self.handler.handle() + else: + _logger.error("Error opening serial port , Unable to start server!!") def server_close(self): ''' Callback for stopping the running server diff --git a/test/test_client_sync.py b/test/test_client_sync.py index c88d68233..516b44298 100644 --- a/test/test_client_sync.py +++ b/test/test_client_sync.py @@ -202,6 +202,13 @@ def testSyncSerialClientInstantiation(self): self.assertTrue(isinstance(ModbusSerialClient(method='binary').framer, ModbusBinaryFramer)) self.assertRaises(ParameterException, lambda: ModbusSerialClient(method='something')) + def testSyncSerialRTUClientTimeouts(self): + client = ModbusSerialClient(method="rtu", baudrate=9600) + assert client._silent_interval == (3.5 * 11/9600) + client = ModbusSerialClient(method="rtu", baudrate=38400) + assert client._silent_interval == (1.75/1000) + + @patch("serial.Serial") def testBasicSyncSerialClient(self, mock_serial): ''' Test the basic methods for the serial sync client''' diff --git a/test/test_server_sync.py b/test/test_server_sync.py index e6a974358..348f2ff4c 100644 --- a/test/test_server_sync.py +++ b/test/test_server_sync.py @@ -33,6 +33,7 @@ def __init__(self): self.threads = [] self.context = {} + #---------------------------------------------------------------------------# # Fixture #---------------------------------------------------------------------------# @@ -280,24 +281,30 @@ def testUdpServerProcess(self): #-----------------------------------------------------------------------# def testSerialServerConnect(self): with patch.object(serial, 'Serial') as mock_serial: - mock_serial.return_value = "socket" - identity = ModbusDeviceIdentification(info={0x00: 'VendorName'}) - server = ModbusSerialServer(context=None, identity=identity) - self.assertEqual(server.socket, "socket") - self.assertEqual(server.control.Identity.VendorName, 'VendorName') - - server._connect() - self.assertEqual(server.socket, "socket") + # mock_serial.return_value = "socket" + mock_serial.write = lambda x: len(x) + mock_serial.read = lambda size: '\x00' * size + identity = ModbusDeviceIdentification(info={0x00: 'VendorName'}) + server = ModbusSerialServer(context=None, identity=identity, port="dummy") + # # mock_serial.return_value = "socket" + # self.assertEqual(server.socket.port, "dummy") + self.assertEquals(server.handler.__class__.__name__, "CustomSingleRequestHandler") + self.assertEqual(server.control.Identity.VendorName, 'VendorName') + + server._connect() + # self.assertEqual(server.socket, "socket") with patch.object(serial, 'Serial') as mock_serial: + mock_serial.write = lambda x: len(x) + mock_serial.read = lambda size: '\x00' * size mock_serial.side_effect = serial.SerialException() - server = ModbusSerialServer(None) + server = ModbusSerialServer(None, port="dummy") self.assertEqual(server.socket, None) def testSerialServerServeForever(self): ''' test that the synchronous serial server closes correctly ''' with patch.object(serial, 'Serial') as mock_serial: - with patch('pymodbus.server.sync.ModbusSingleRequestHandler') as mock_handler: + with patch('pymodbus.server.sync.CustomSingleRequestHandler') as mock_handler: server = ModbusSerialServer(None) instance = mock_handler.return_value instance.handle.side_effect = server.server_close