From 1695c8420a1c43ff6f4ff442ce5fa0cdd2f6e78f Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 2 Nov 2020 16:11:47 +0100 Subject: [PATCH] [lldb] Generalize an deflake gdb-remote *client* tests This is similar in spirit to what D90313 did for server tests. --- .../TestGDBRemoteDiskFileCompletion.py | 4 +-- .../gdb_remote_client/TestPlatformClient.py | 3 +-- .../gdb_remote_client/TestProcessConnect.py | 12 ++++----- .../gdb_remote_client/gdbclientutils.py | 31 +++++++++------------- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py b/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py index 90f830c..c0afce0 100644 --- a/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py +++ b/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py @@ -16,8 +16,8 @@ class TestGDBRemoteDiskFileCompletion(GDBRemoteTestBase): try: self.runCmd("platform select remote-gdb-server") - self.runCmd("platform connect connect://localhost:%d" % - self.server.port) + self.runCmd("platform connect connect://" + + self.server.get_connect_address()) self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected()) self.complete_from_to('platform get-size ', ['test', '123']) diff --git a/lldb/test/API/functionalities/gdb_remote_client/TestPlatformClient.py b/lldb/test/API/functionalities/gdb_remote_client/TestPlatformClient.py index 2da8dd5..e05089e 100644 --- a/lldb/test/API/functionalities/gdb_remote_client/TestPlatformClient.py +++ b/lldb/test/API/functionalities/gdb_remote_client/TestPlatformClient.py @@ -49,8 +49,7 @@ class TestPlatformClient(GDBRemoteTestBase): try: self.runCmd("platform select remote-linux") - self.runCmd("platform connect connect://localhost:%d" % - self.server.port) + self.runCmd("platform connect connect://" + self.server.get_connect_address()) self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected()) self.expect("platform process list -x", substrs=["2 matching processes were found", "test_process", "another_test_process"]) diff --git a/lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py b/lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py index 32a585d..af9f212 100644 --- a/lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py +++ b/lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py @@ -16,7 +16,7 @@ class TestProcessConnect(GDBRemoteTestBase): """Test the gdb-remote command in synchronous mode""" try: self.dbg.SetAsync(False) - self.expect("gdb-remote %d" % self.server.port, + self.expect("gdb-remote " + self.server.get_connect_address(), substrs=['Process', 'stopped']) finally: self.dbg.GetSelectedPlatform().DisconnectRemote() @@ -27,7 +27,7 @@ class TestProcessConnect(GDBRemoteTestBase): """Test the gdb-remote command in asynchronous mode""" try: self.dbg.SetAsync(True) - self.expect("gdb-remote %d" % self.server.port, + self.expect("gdb-remote " + self.server.get_connect_address(), matching=False, substrs=['Process', 'stopped']) lldbutil.expect_state_changes(self, self.dbg.GetListener(), @@ -40,8 +40,8 @@ class TestProcessConnect(GDBRemoteTestBase): """Test the gdb-remote command in synchronous mode""" try: self.dbg.SetAsync(False) - self.expect("process connect connect://localhost:%d" % - self.server.port, + self.expect("process connect connect://" + + self.server.get_connect_address(), substrs=['Process', 'stopped']) finally: self.dbg.GetSelectedPlatform().DisconnectRemote() @@ -52,8 +52,8 @@ class TestProcessConnect(GDBRemoteTestBase): """Test the gdb-remote command in asynchronous mode""" try: self.dbg.SetAsync(True) - self.expect("process connect connect://localhost:%d" % - self.server.port, + self.expect("process connect connect://" + + self.server.get_connect_address(), matching=False, substrs=['Process', 'stopped']) lldbutil.expect_state_changes(self, self.dbg.GetListener(), diff --git a/lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py b/lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py index dabe942..4c5ff03 100644 --- a/lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py +++ b/lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py @@ -307,7 +307,6 @@ class MockGDBServer: """ responder = None - port = 0 _socket = None _client = None _thread = None @@ -315,26 +314,19 @@ class MockGDBServer: _receivedDataOffset = None _shouldSendAck = True - def __init__(self, port = 0): + def __init__(self): self.responder = MockGDBServerResponder() - self.port = port - try: - self._socket = socket.socket(family=socket.AF_INET) - except OSError as e: - if e.errno != errno.EAFNOSUPPORT: - raise - self._socket = socket.socket(family=socket.AF_INET6) def start(self): - # Block until the socket is up, so self.port is available immediately. - # Then start a thread that waits for a client connection. - if self._socket.family == socket.AF_INET: - addr = ("127.0.0.1", self.port) - elif self._socket.family == socket.AF_INET6: - addr = ("::1", self.port) + family, type, proto, _, addr = socket.getaddrinfo("localhost", 0, + proto=socket.IPPROTO_TCP)[0] + self._socket = socket.socket(family, type, proto) + + self._socket.bind(addr) - self.port = self._socket.getsockname()[1] self._socket.listen(1) + + # Start a thread that waits for a client connection. self._thread = threading.Thread(target=self._run) self._thread.start() @@ -343,6 +335,9 @@ class MockGDBServer: self._thread.join() self._thread = None + def get_connect_address(self): + return "[{}]:{}".format(*self._socket.getsockname()) + def _run(self): # For testing purposes, we only need to worry about one client # connecting just one time. @@ -528,8 +523,8 @@ class GDBRemoteTestBase(TestBase): """ listener = self.dbg.GetListener() error = lldb.SBError() - url = "connect://localhost:%d" % self.server.port - process = target.ConnectRemote(listener, url, "gdb-remote", error) + process = target.ConnectRemote(listener, + "connect://" + self.server.get_connect_address(), "gdb-remote", error) self.assertTrue(error.Success(), error.description) self.assertTrue(process, PROCESS_IS_VALID) return process -- 2.7.4