diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-04-28 09:41:02 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-04-28 09:41:02 +0200 |
commit | a5c59e18707fb19dc1e838421daea34b51f2838f (patch) | |
tree | 005d745fde67985cc257d76d79b738f960c4fdb8 | |
parent | adbeb13b4fdc6d324aa35395ebd9285c3f3d3282 (diff) | |
download | psutil-a5c59e18707fb19dc1e838421daea34b51f2838f.tar.gz |
1032: add utiliy test function to bind 2 unix sockets
-rw-r--r-- | psutil/tests/__init__.py | 27 | ||||
-rwxr-xr-x | psutil/tests/test_misc.py | 16 |
2 files changed, 38 insertions, 5 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 69ef2ba6..f96bfbf0 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -851,14 +851,14 @@ def check_connection_ntuple(conn): assert dupsock.type == conn.type -def bind_unix_socket(type=socket.SOCK_STREAM, name=None, suffix="", - mode=0o600): +def bind_unix_socket(type=socket.SOCK_STREAM, name=None, suffix=""): """Creates a listening unix socket. Return a (sock, filemame) tuple. """ # TODO: for some reason on OSX a UNIX socket cannot be # deleted once created (EACCES) so we create a temp file # which will remain around. :-\ + assert psutil.POSIX, "not a POSIX system" if not name: if OSX: name = tempfile.mktemp(prefix=TESTFILE_PREFIX, suffix=suffix) @@ -867,19 +867,36 @@ def bind_unix_socket(type=socket.SOCK_STREAM, name=None, suffix="", else: if suffix: raise ValueError("name and suffix aregs are mutually exclusive") + safe_rmpath(name) assert not os.path.exists(name), name sock = socket.socket(socket.AF_UNIX, type) - sock.settimeout(GLOBAL_TIMEOUT) try: sock.bind(name) except Exception: sock.close() raise - if mode is not None: - os.chmod(name, mode) + os.chmod(name, 0o600) return (sock, name) +def unix_socketpair(name=None, suffix=""): + """Build a pair of UNIX sockets connected to each other through + the same UNIX file name. + Return a (server_sock, client_sock, filename) tuple. + """ + assert psutil.POSIX, "not a POSIX system" + listener, name = bind_unix_socket(name=name, suffix=suffix) + listener.setblocking(0) + listener.listen(1) + client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + client.setblocking(0) + # XXX - for some reason I don't have to select() even if they + # are non-blocking sockets. Why doesn't this raise EAGAIN? + client.connect(name) + # new = listener.accept() + return (listener, client, name) + + # =================================================================== # --- others # =================================================================== diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index ee796d81..5b5f365a 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -46,6 +46,7 @@ from psutil.tests import TESTFN from psutil.tests import TOX from psutil.tests import TRAVIS from psutil.tests import unittest +from psutil.tests import unix_socketpair from psutil.tests import wait_for_file from psutil.tests import wait_for_pid import psutil @@ -677,5 +678,20 @@ class TestProcessUtils(unittest.TestCase): assert not psutil.tests._subprocesses_started +class TestNetUtils(unittest.TestCase): + + @unittest.skipUnless(POSIX, "POSIX only") + def test_unix_socketpair(self): + p = psutil.Process() + num_fds = p.num_fds() + assert not p.connections(kind='unix') + ssock, csock, name = unix_socketpair() + self.addCleanup(safe_rmpath, name) + assert os.path.exists(name) + assert stat.S_ISSOCK(os.stat(name).st_mode) + self.assertEqual(p.num_fds() - num_fds, 2) + self.assertEqual(len(p.connections(kind='unix')), 2) + + if __name__ == '__main__': run_test_module_by_name(__file__) |