def test_client_protocol_execute(self): """ Test the client protocol execute method """ protocol = ModbusClientProtocol() protocol.connectionMade() protocol.transport = Mock() protocol.transport.write = Mock() request = ReadCoilsRequest(1, 1) d = protocol.execute(request) tid = request.transaction_id self.assertEqual(d, protocol.transaction.getTransaction(tid))
def test_client_protocol_data_received(self): """ Test the client protocol data received """ protocol = ModbusClientProtocol() protocol.connectionMade() out = [] data = "\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04" # setup existing request d = protocol._buildResponse(0x00) d.addCallback(lambda v: out.append(v)) protocol.dataReceived(data) self.assertTrue(isinstance(out[0], ReadCoilsResponse))
def test_client_protocol_data_received(self): """ Test the client protocol data received """ protocol = ModbusClientProtocol() protocol.connectionMade() out = [] data = '\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04' # setup existing request d = protocol._buildResponse(0x00) d.addCallback(lambda v: out.append(v)) protocol.dataReceived(data) self.assertTrue(isinstance(out[0], ReadCoilsResponse))
def test_client_protocol_disconnect(self): """ Test the client protocol disconnect """ protocol = ModbusClientProtocol() protocol.connectionMade() def handle_failure(failure): self.assertTrue(isinstance(failure.value, ConnectionException)) d = protocol._buildResponse(0x00) d.addErrback(handle_failure) self.assertTrue(protocol._connected) protocol.connectionLost("because") self.assertFalse(protocol._connected)
def test_client_protocol_disconnect(self): """ Test the client protocol disconnect """ protocol = ModbusClientProtocol() protocol.connectionMade() def handle_failure(failure): self.assertTrue(isinstance(failure.value, ConnectionException)) d = protocol._buildResponse(0x00) d.addErrback(handle_failure) self.assertTrue(protocol._connected) protocol.connectionLost('because') self.assertFalse(protocol._connected)
def test_client_protocol_handle_response(self): """ Test the client protocol handles responses """ protocol = ModbusClientProtocol() protocol.connectionMade() out = [] reply = ReadCoilsRequest(1, 1) reply.transaction_id = 0x00 # handle skipped cases protocol._handleResponse(None) protocol._handleResponse(reply) # handle existing cases d = protocol._buildResponse(0x00) d.addCallback(lambda v: out.append(v)) protocol._handleResponse(reply) self.assertEqual(out[0], reply)
def test_client_protocol_connect(self): """ Test the client protocol connect """ protocol = ModbusClientProtocol() self.assertFalse(protocol._connected) protocol.connectionMade() self.assertTrue(protocol._connected)