summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio/test_server.py
diff options
context:
space:
mode:
authorPierre Ossman (ThinLinc team) <ossman@cendio.se>2023-11-08 16:10:10 (GMT)
committerGitHub <noreply@github.com>2023-11-08 16:10:10 (GMT)
commit74b868f636a8af9e5540e3315de666500147d47a (patch)
treee36a68b54fee50c800155c22ba796dcdbaab498c /Lib/test/test_asyncio/test_server.py
parentf88caab467eb57cfe293cdf9fb7cce29b24fda7f (diff)
downloadcpython-74b868f636a8af9e5540e3315de666500147d47a.zip
cpython-74b868f636a8af9e5540e3315de666500147d47a.tar.gz
cpython-74b868f636a8af9e5540e3315de666500147d47a.tar.bz2
gh-111246: Remove listening Unix socket on close (#111483)
Try to clean up the socket file we create so we don't add unused noise to the file system.
Diffstat (limited to 'Lib/test/test_asyncio/test_server.py')
-rw-r--r--Lib/test/test_asyncio/test_server.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_server.py b/Lib/test/test_asyncio/test_server.py
index 7ff3f55..f22cf30 100644
--- a/Lib/test/test_asyncio/test_server.py
+++ b/Lib/test/test_asyncio/test_server.py
@@ -1,4 +1,6 @@
import asyncio
+import os
+import socket
import time
import threading
import unittest
@@ -177,6 +179,80 @@ class TestServer2(unittest.IsolatedAsyncioTestCase):
+# Test the various corner cases of Unix server socket removal
+class UnixServerCleanupTests(unittest.IsolatedAsyncioTestCase):
+ @socket_helper.skip_unless_bind_unix_socket
+ async def test_unix_server_addr_cleanup(self):
+ # Default scenario
+ with test_utils.unix_socket_path() as addr:
+ async def serve(*args):
+ pass
+
+ srv = await asyncio.start_unix_server(serve, addr)
+
+ srv.close()
+ self.assertFalse(os.path.exists(addr))
+
+ @socket_helper.skip_unless_bind_unix_socket
+ async def test_unix_server_sock_cleanup(self):
+ # Using already bound socket
+ with test_utils.unix_socket_path() as addr:
+ async def serve(*args):
+ pass
+
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.bind(addr)
+
+ srv = await asyncio.start_unix_server(serve, sock=sock)
+
+ srv.close()
+ self.assertFalse(os.path.exists(addr))
+
+ @socket_helper.skip_unless_bind_unix_socket
+ async def test_unix_server_cleanup_gone(self):
+ # Someone else has already cleaned up the socket
+ with test_utils.unix_socket_path() as addr:
+ async def serve(*args):
+ pass
+
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.bind(addr)
+
+ srv = await asyncio.start_unix_server(serve, sock=sock)
+
+ os.unlink(addr)
+
+ srv.close()
+
+ @socket_helper.skip_unless_bind_unix_socket
+ async def test_unix_server_cleanup_replaced(self):
+ # Someone else has replaced the socket with their own
+ with test_utils.unix_socket_path() as addr:
+ async def serve(*args):
+ pass
+
+ srv = await asyncio.start_unix_server(serve, addr)
+
+ os.unlink(addr)
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.bind(addr)
+
+ srv.close()
+ self.assertTrue(os.path.exists(addr))
+
+ @socket_helper.skip_unless_bind_unix_socket
+ async def test_unix_server_cleanup_prevented(self):
+ # Automatic cleanup explicitly disabled
+ with test_utils.unix_socket_path() as addr:
+ async def serve(*args):
+ pass
+
+ srv = await asyncio.start_unix_server(serve, addr, cleanup_socket=False)
+
+ srv.close()
+ self.assertTrue(os.path.exists(addr))
+
+
@unittest.skipUnless(hasattr(asyncio, 'ProactorEventLoop'), 'Windows only')
class ProactorStartServerTests(BaseStartServer, unittest.TestCase):