Skip to content

Commit

Permalink
unit tests for #191, #149 , fix obvious server_start errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoomakethu committed Aug 4, 2017
1 parent 99a2524 commit b7ce6d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
20 changes: 12 additions & 8 deletions pymodbus/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -429,6 +429,7 @@ def _build_handler(self):
:returns: A patched handler
'''

request = self.socket
request.send = request.write
request.recv = request.read
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/test_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'''
Expand Down
27 changes: 17 additions & 10 deletions test/test_server_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self):
self.threads = []
self.context = {}


#---------------------------------------------------------------------------#
# Fixture
#---------------------------------------------------------------------------#
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b7ce6d9

Please sign in to comment.