From 78a7658b19ad1c745e9cc6b8033bf5f23bbd946a Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 29 Apr 2017 08:13:05 +0200 Subject: fix connection tests --- psutil/tests/__init__.py | 2 +- psutil/tests/test_connections.py | 12 ++++++++---- psutil/tests/test_misc.py | 13 +++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index fad0c3e7..99cc30a9 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -830,7 +830,7 @@ def inet_socketpair(family, type, addr=("", 0)): a, addr = ll.accept() # check that we've got the correct client if addr == caddr: - return c, a + return (a, c) a.close() except OSError: c.close() diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py index 597ead01..aec9a5e5 100644 --- a/psutil/tests/test_connections.py +++ b/psutil/tests/test_connections.py @@ -173,19 +173,23 @@ class TestConnectedSocketPairs(Base, unittest.TestCase): @staticmethod def differentiate_tcp_socks(cons, server_addr): - if cons[0].raddr == server_addr: + """Given a list of connections return a (server, client) + tuple. + """ + if cons[0].laddr == server_addr: return (cons[0], cons[1]) else: - assert cons[1].raddr == server_addr + assert cons[1].laddr == server_addr return (cons[1], cons[0]) def test_tcp(self): from psutil.tests import inet_socketpair addr = ("127.0.0.1", get_free_port()) - s_sock, c_sock = inet_socketpair(AF_INET, SOCK_STREAM, addr=addr) + server, c_sock = inet_socketpair(AF_INET, SOCK_STREAM, addr=addr) cons = psutil.Process().connections(kind='all') s_conn, c_conn = self.differentiate_tcp_socks(cons, addr) - self.check_socket(s_sock, conn=s_conn) + self.check_socket(server, conn=s_conn) + self.check_socket(c_sock, conn=c_conn) self.assertEqual(s_conn.status, psutil.CONN_ESTABLISHED) self.assertEqual(c_conn.status, psutil.CONN_ESTABLISHED) diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index 358fa795..22dfc7d2 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -34,6 +34,7 @@ from psutil.tests import APPVEYOR from psutil.tests import bind_unix_socket from psutil.tests import chdir from psutil.tests import create_proc_children_pair +from psutil.tests import get_free_port from psutil.tests import get_test_subprocess from psutil.tests import importlib from psutil.tests import inet_socketpair @@ -701,12 +702,16 @@ class TestNetUtils(unittest.TestCase): self.assertEqual(sock.type, socket.SOCK_DGRAM) def test_inet_socketpair(self): - server, client = inet_socketpair(socket.AF_INET, socket.SOCK_STREAM) + addr = ("127.0.0.1", get_free_port()) + server, client = inet_socketpair( + socket.AF_INET, socket.SOCK_STREAM, addr=addr) with contextlib.closing(server): with contextlib.closing(client): - # ensure they are connected - self.assertEqual(server.getsockname(), client.getpeername()) - self.assertEqual(server.getpeername(), client.getsockname()) + # Ensure they are connected and the positions are + # correct. + self.assertEqual(server.getsockname(), addr) + self.assertEqual(client.getpeername(), addr) + self.assertNotEqual(client.getsockname(), addr) @unittest.skipUnless(POSIX, "POSIX only") def test_unix_socketpair(self): -- cgit v1.2.1