Skip to content

Commit 1695c84

Browse files
committed
[lldb] Generalize an deflake gdb-remote *client* tests
This is similar in spirit to what D90313 did for server tests.
1 parent c883904 commit 1695c84

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def qPathComplete(self):
1616

1717
try:
1818
self.runCmd("platform select remote-gdb-server")
19-
self.runCmd("platform connect connect://localhost:%d" %
20-
self.server.port)
19+
self.runCmd("platform connect connect://" +
20+
self.server.get_connect_address())
2121
self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected())
2222

2323
self.complete_from_to('platform get-size ', ['test', '123'])

lldb/test/API/functionalities/gdb_remote_client/TestPlatformClient.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def qsProcessInfo(self):
4949

5050
try:
5151
self.runCmd("platform select remote-linux")
52-
self.runCmd("platform connect connect://localhost:%d" %
53-
self.server.port)
52+
self.runCmd("platform connect connect://" + self.server.get_connect_address())
5453
self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected())
5554
self.expect("platform process list -x",
5655
substrs=["2 matching processes were found", "test_process", "another_test_process"])

lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_gdb_remote_sync(self):
1616
"""Test the gdb-remote command in synchronous mode"""
1717
try:
1818
self.dbg.SetAsync(False)
19-
self.expect("gdb-remote %d" % self.server.port,
19+
self.expect("gdb-remote " + self.server.get_connect_address(),
2020
substrs=['Process', 'stopped'])
2121
finally:
2222
self.dbg.GetSelectedPlatform().DisconnectRemote()
@@ -27,7 +27,7 @@ def test_gdb_remote_async(self):
2727
"""Test the gdb-remote command in asynchronous mode"""
2828
try:
2929
self.dbg.SetAsync(True)
30-
self.expect("gdb-remote %d" % self.server.port,
30+
self.expect("gdb-remote " + self.server.get_connect_address(),
3131
matching=False,
3232
substrs=['Process', 'stopped'])
3333
lldbutil.expect_state_changes(self, self.dbg.GetListener(),
@@ -40,8 +40,8 @@ def test_process_connect_sync(self):
4040
"""Test the gdb-remote command in synchronous mode"""
4141
try:
4242
self.dbg.SetAsync(False)
43-
self.expect("process connect connect://localhost:%d" %
44-
self.server.port,
43+
self.expect("process connect connect://" +
44+
self.server.get_connect_address(),
4545
substrs=['Process', 'stopped'])
4646
finally:
4747
self.dbg.GetSelectedPlatform().DisconnectRemote()
@@ -52,8 +52,8 @@ def test_process_connect_async(self):
5252
"""Test the gdb-remote command in asynchronous mode"""
5353
try:
5454
self.dbg.SetAsync(True)
55-
self.expect("process connect connect://localhost:%d" %
56-
self.server.port,
55+
self.expect("process connect connect://" +
56+
self.server.get_connect_address(),
5757
matching=False,
5858
substrs=['Process', 'stopped'])
5959
lldbutil.expect_state_changes(self, self.dbg.GetListener(),

lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -307,34 +307,26 @@ class MockGDBServer:
307307
"""
308308

309309
responder = None
310-
port = 0
311310
_socket = None
312311
_client = None
313312
_thread = None
314313
_receivedData = None
315314
_receivedDataOffset = None
316315
_shouldSendAck = True
317316

318-
def __init__(self, port = 0):
317+
def __init__(self):
319318
self.responder = MockGDBServerResponder()
320-
self.port = port
321-
try:
322-
self._socket = socket.socket(family=socket.AF_INET)
323-
except OSError as e:
324-
if e.errno != errno.EAFNOSUPPORT:
325-
raise
326-
self._socket = socket.socket(family=socket.AF_INET6)
327319

328320
def start(self):
329-
# Block until the socket is up, so self.port is available immediately.
330-
# Then start a thread that waits for a client connection.
331-
if self._socket.family == socket.AF_INET:
332-
addr = ("127.0.0.1", self.port)
333-
elif self._socket.family == socket.AF_INET6:
334-
addr = ("::1", self.port)
321+
family, type, proto, _, addr = socket.getaddrinfo("localhost", 0,
322+
proto=socket.IPPROTO_TCP)[0]
323+
self._socket = socket.socket(family, type, proto)
324+
325+
335326
self._socket.bind(addr)
336-
self.port = self._socket.getsockname()[1]
337327
self._socket.listen(1)
328+
329+
# Start a thread that waits for a client connection.
338330
self._thread = threading.Thread(target=self._run)
339331
self._thread.start()
340332

@@ -343,6 +335,9 @@ def stop(self):
343335
self._thread.join()
344336
self._thread = None
345337

338+
def get_connect_address(self):
339+
return "[{}]:{}".format(*self._socket.getsockname())
340+
346341
def _run(self):
347342
# For testing purposes, we only need to worry about one client
348343
# connecting just one time.
@@ -528,8 +523,8 @@ def connect(self, target):
528523
"""
529524
listener = self.dbg.GetListener()
530525
error = lldb.SBError()
531-
url = "connect://localhost:%d" % self.server.port
532-
process = target.ConnectRemote(listener, url, "gdb-remote", error)
526+
process = target.ConnectRemote(listener,
527+
"connect://" + self.server.get_connect_address(), "gdb-remote", error)
533528
self.assertTrue(error.Success(), error.description)
534529
self.assertTrue(process, PROCESS_IS_VALID)
535530
return process

0 commit comments

Comments
 (0)