def test_pa_client_retry(self): """ Test that the port agent client will not continually try to recover when the port agent closes the connection gracefully because it has another client connected. """ exception_raised = False self.resetTestVars() self.init_instrument_simulator() self.startPortAgent() time.sleep(2) # Start a TCP client that will connect to the data port; this sets up the # situation where the Port Agent will immediately close the connection # because it already has one self.tcp_client = TcpClient("localhost", self.data_port) time.sleep(2) pa_client = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port) try: pa_client.init_comms(self.myGotData, self.myGotRaw, self.myGotListenerError, self.myGotError) except InstrumentConnectionException: exception_raised = True # Give it some time to retry time.sleep(4) self.assertTrue(exception_raised)
def test_expect(self): client = TcpClient() client.s = mock.Mock() client.s.recv.return_value = 'abcdefghi' # true if string is found self.assertTrue(client.expect('def')) # buf should contain only the text following the expected string self.assertEqual(client.buf, 'ghi')
def test_expect_regex(self): client = TcpClient() client.s = mock.Mock() client.s.recv.return_value = 'abcdefghi' # un-compiled regex # expect_regex returns a match object self.assertEqual(client.expect_regex('[def]{3}').group(), 'def') # buf should contain only the text following the matched string self.assertEqual(client.buf, 'ghi') # pre-compiled regex with groups result = client.expect_regex(re.compile(r'(abc)(def)')) self.assertEqual(result.group(1), 'abc') self.assertEqual(result.group(2), 'def') self.assertEqual(client.buf, 'ghi')
def test_remove_from_buffer(self): """ Test the remove from buffer method. Verify that all bytes before the match string are removed. """ client = TcpClient() target = "MATCH" # Remove with an None target self.assertFalse(client.remove_from_buffer(None)) # Remove with a zero length target self.assertFalse(client.remove_from_buffer("")) # Remove from buffer when buffer empty client.buf = None self.assertFalse(client.remove_from_buffer(target)) self.assertIsNone(client.buf) # Remove from buffer when target the only thing in the list client.buf = target self.assertTrue(client.remove_from_buffer(target)) self.assertEqual(len(client.buf), 0) # Remove from buffer when target the last thing in the list client.buf = "foo" + target self.assertTrue(client.remove_from_buffer(target)) self.assertEqual(len(client.buf), 0) # Remove from buffer when target in the middle client.buf = "foo" + target + "remains" self.assertTrue(client.remove_from_buffer(target)) self.assertEqual(client.buf, "remains") # Remove from buffer when target not in the buff client.buf = "target not in the list" self.assertFalse(client.remove_from_buffer(target)) self.assertEqual(client.buf, "target not in the list") # Remove from buffer when target in the list more than once client.buf = "foo" + target + "remains" client.buf = "foo" + target + "remains" + target self.assertTrue(client.remove_from_buffer(target)) self.assertEqual(client.buf, "remains" + target)