2018-01-25 19:08:09 -04:00
|
|
|
import asyncio
|
2023-11-08 12:10:10 -04:00
|
|
|
import os
|
|
|
|
import socket
|
2018-01-26 02:30:57 -04:00
|
|
|
import time
|
2018-01-25 19:08:09 -04:00
|
|
|
import threading
|
|
|
|
import unittest
|
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
from test.support import socket_helper
|
2018-01-25 19:08:09 -04:00
|
|
|
from test.test_asyncio import utils as test_utils
|
|
|
|
from test.test_asyncio import functional as func_tests
|
|
|
|
|
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def tearDownModule():
|
|
|
|
asyncio.set_event_loop_policy(None)
|
|
|
|
|
|
|
|
|
2018-01-25 19:08:09 -04:00
|
|
|
class BaseStartServer(func_tests.FunctionalTestCaseMixin):
|
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def test_start_server_1(self):
|
|
|
|
HELLO_MSG = b'1' * 1024 * 5 + b'\n'
|
|
|
|
|
|
|
|
def client(sock, addr):
|
2018-01-26 02:30:57 -04:00
|
|
|
for i in range(10):
|
|
|
|
time.sleep(0.2)
|
|
|
|
if srv.is_serving():
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
sock.settimeout(2)
|
2018-01-25 19:08:09 -04:00
|
|
|
sock.connect(addr)
|
|
|
|
sock.send(HELLO_MSG)
|
|
|
|
sock.recv_all(1)
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
async def serve(reader, writer):
|
|
|
|
await reader.readline()
|
|
|
|
main_task.cancel()
|
|
|
|
writer.write(b'1')
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
|
|
|
|
async def main(srv):
|
|
|
|
async with srv:
|
|
|
|
await srv.serve_forever()
|
|
|
|
|
2020-11-26 03:36:37 -04:00
|
|
|
srv = self.loop.run_until_complete(asyncio.start_server(
|
|
|
|
serve, socket_helper.HOSTv4, 0, start_serving=False))
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
|
|
|
|
main_task = self.loop.create_task(main(srv))
|
|
|
|
|
|
|
|
addr = srv.sockets[0].getsockname()
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
|
|
with self.tcp_client(lambda sock: client(sock, addr)):
|
|
|
|
self.loop.run_until_complete(main_task)
|
|
|
|
|
2019-05-27 10:57:20 -03:00
|
|
|
self.assertEqual(srv.sockets, ())
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
self.assertIsNone(srv._sockets)
|
|
|
|
self.assertIsNone(srv._waiters)
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(RuntimeError, r'is closed'):
|
|
|
|
self.loop.run_until_complete(srv.serve_forever())
|
|
|
|
|
|
|
|
|
|
|
|
class SelectorStartServerTests(BaseStartServer, unittest.TestCase):
|
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
return asyncio.SelectorEventLoop()
|
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
@socket_helper.skip_unless_bind_unix_socket
|
2018-01-25 19:08:09 -04:00
|
|
|
def test_start_unix_server_1(self):
|
|
|
|
HELLO_MSG = b'1' * 1024 * 5 + b'\n'
|
|
|
|
started = threading.Event()
|
|
|
|
|
|
|
|
def client(sock, addr):
|
2018-01-26 02:30:57 -04:00
|
|
|
sock.settimeout(2)
|
2018-01-25 19:08:09 -04:00
|
|
|
started.wait(5)
|
|
|
|
sock.connect(addr)
|
|
|
|
sock.send(HELLO_MSG)
|
|
|
|
sock.recv_all(1)
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
async def serve(reader, writer):
|
|
|
|
await reader.readline()
|
|
|
|
main_task.cancel()
|
|
|
|
writer.write(b'1')
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
|
|
|
|
async def main(srv):
|
|
|
|
async with srv:
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
await srv.start_serving()
|
|
|
|
self.assertTrue(srv.is_serving())
|
|
|
|
started.set()
|
|
|
|
await srv.serve_forever()
|
|
|
|
|
|
|
|
with test_utils.unix_socket_path() as addr:
|
2020-11-26 03:36:37 -04:00
|
|
|
srv = self.loop.run_until_complete(asyncio.start_unix_server(
|
|
|
|
serve, addr, start_serving=False))
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
main_task = self.loop.create_task(main(srv))
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
|
|
with self.unix_client(lambda sock: client(sock, addr)):
|
|
|
|
self.loop.run_until_complete(main_task)
|
|
|
|
|
2019-05-27 10:57:20 -03:00
|
|
|
self.assertEqual(srv.sockets, ())
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
self.assertIsNone(srv._sockets)
|
|
|
|
self.assertIsNone(srv._waiters)
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(RuntimeError, r'is closed'):
|
|
|
|
self.loop.run_until_complete(srv.serve_forever())
|
|
|
|
|
|
|
|
|
2022-11-24 11:32:58 -04:00
|
|
|
class TestServer2(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
2023-10-28 15:04:29 -03:00
|
|
|
async def test_wait_closed_basic(self):
|
2024-03-18 17:15:53 -03:00
|
|
|
async def serve(rd, wr):
|
|
|
|
try:
|
|
|
|
await rd.read()
|
|
|
|
finally:
|
|
|
|
wr.close()
|
|
|
|
await wr.wait_closed()
|
2022-11-24 11:32:58 -04:00
|
|
|
|
|
|
|
srv = await asyncio.start_server(serve, socket_helper.HOSTv4, 0)
|
2023-10-28 15:04:29 -03:00
|
|
|
self.addCleanup(srv.close)
|
2022-11-24 11:32:58 -04:00
|
|
|
|
2023-10-28 15:04:29 -03:00
|
|
|
# active count = 0, not closed: should block
|
2022-11-24 11:32:58 -04:00
|
|
|
task1 = asyncio.create_task(srv.wait_closed())
|
|
|
|
await asyncio.sleep(0)
|
2023-10-28 15:04:29 -03:00
|
|
|
self.assertFalse(task1.done())
|
2022-11-24 11:32:58 -04:00
|
|
|
|
2023-10-28 15:04:29 -03:00
|
|
|
# active count != 0, not closed: should block
|
2024-03-18 17:15:53 -03:00
|
|
|
addr = srv.sockets[0].getsockname()
|
|
|
|
(rd, wr) = await asyncio.open_connection(addr[0], addr[1])
|
2022-11-24 11:32:58 -04:00
|
|
|
task2 = asyncio.create_task(srv.wait_closed())
|
|
|
|
await asyncio.sleep(0)
|
2023-10-28 15:04:29 -03:00
|
|
|
self.assertFalse(task1.done())
|
2022-11-24 11:32:58 -04:00
|
|
|
self.assertFalse(task2.done())
|
|
|
|
|
|
|
|
srv.close()
|
|
|
|
await asyncio.sleep(0)
|
2023-10-28 15:04:29 -03:00
|
|
|
# active count != 0, closed: should block
|
|
|
|
task3 = asyncio.create_task(srv.wait_closed())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertFalse(task1.done())
|
2022-11-24 11:32:58 -04:00
|
|
|
self.assertFalse(task2.done())
|
2023-10-28 15:04:29 -03:00
|
|
|
self.assertFalse(task3.done())
|
2022-11-24 11:32:58 -04:00
|
|
|
|
2024-03-18 17:15:53 -03:00
|
|
|
wr.close()
|
|
|
|
await wr.wait_closed()
|
2023-10-28 15:04:29 -03:00
|
|
|
# active count == 0, closed: should unblock
|
|
|
|
await task1
|
2022-11-24 11:32:58 -04:00
|
|
|
await task2
|
2023-10-28 15:04:29 -03:00
|
|
|
await task3
|
|
|
|
await srv.wait_closed() # Return immediately
|
|
|
|
|
|
|
|
async def test_wait_closed_race(self):
|
|
|
|
# Test a regression in 3.12.0, should be fixed in 3.12.1
|
2024-03-18 17:15:53 -03:00
|
|
|
async def serve(rd, wr):
|
|
|
|
try:
|
|
|
|
await rd.read()
|
|
|
|
finally:
|
|
|
|
wr.close()
|
|
|
|
await wr.wait_closed()
|
2023-10-28 15:04:29 -03:00
|
|
|
|
|
|
|
srv = await asyncio.start_server(serve, socket_helper.HOSTv4, 0)
|
|
|
|
self.addCleanup(srv.close)
|
|
|
|
|
|
|
|
task = asyncio.create_task(srv.wait_closed())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertFalse(task.done())
|
2024-03-18 17:15:53 -03:00
|
|
|
addr = srv.sockets[0].getsockname()
|
|
|
|
(rd, wr) = await asyncio.open_connection(addr[0], addr[1])
|
2023-10-28 15:04:29 -03:00
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
loop.call_soon(srv.close)
|
2024-03-18 17:15:53 -03:00
|
|
|
loop.call_soon(wr.close)
|
2023-10-28 15:04:29 -03:00
|
|
|
await srv.wait_closed()
|
|
|
|
|
2024-03-18 17:15:53 -03:00
|
|
|
async def test_close_clients(self):
|
|
|
|
async def serve(rd, wr):
|
|
|
|
try:
|
|
|
|
await rd.read()
|
|
|
|
finally:
|
|
|
|
wr.close()
|
|
|
|
await wr.wait_closed()
|
|
|
|
|
|
|
|
srv = await asyncio.start_server(serve, socket_helper.HOSTv4, 0)
|
|
|
|
self.addCleanup(srv.close)
|
|
|
|
|
|
|
|
addr = srv.sockets[0].getsockname()
|
|
|
|
(rd, wr) = await asyncio.open_connection(addr[0], addr[1])
|
|
|
|
self.addCleanup(wr.close)
|
|
|
|
|
|
|
|
task = asyncio.create_task(srv.wait_closed())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertFalse(task.done())
|
|
|
|
|
|
|
|
srv.close()
|
|
|
|
srv.close_clients()
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertTrue(task.done())
|
|
|
|
|
|
|
|
async def test_abort_clients(self):
|
|
|
|
async def serve(rd, wr):
|
|
|
|
fut.set_result((rd, wr))
|
|
|
|
await wr.wait_closed()
|
|
|
|
|
|
|
|
fut = asyncio.Future()
|
|
|
|
srv = await asyncio.start_server(serve, socket_helper.HOSTv4, 0)
|
|
|
|
self.addCleanup(srv.close)
|
|
|
|
|
|
|
|
addr = srv.sockets[0].getsockname()
|
|
|
|
(c_rd, c_wr) = await asyncio.open_connection(addr[0], addr[1], limit=4096)
|
|
|
|
self.addCleanup(c_wr.close)
|
|
|
|
|
|
|
|
(s_rd, s_wr) = await fut
|
|
|
|
|
2024-08-28 17:36:42 -03:00
|
|
|
# Limit the socket buffers so we can more reliably overfill them
|
2024-03-18 17:15:53 -03:00
|
|
|
s_sock = s_wr.get_extra_info('socket')
|
|
|
|
s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65536)
|
|
|
|
c_sock = c_wr.get_extra_info('socket')
|
|
|
|
c_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
|
|
|
|
|
|
|
|
# Get the reader in to a paused state by sending more than twice
|
|
|
|
# the configured limit
|
|
|
|
s_wr.write(b'a' * 4096)
|
|
|
|
s_wr.write(b'a' * 4096)
|
|
|
|
s_wr.write(b'a' * 4096)
|
|
|
|
while c_wr.transport.is_reading():
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
|
|
|
# Get the writer in a waiting state by sending data until the
|
2024-08-28 17:36:42 -03:00
|
|
|
# kernel stops accepting more data in the send buffer.
|
|
|
|
# gh-122136: getsockopt() does not reliably report the buffer size
|
|
|
|
# available for message content.
|
|
|
|
# We loop until we start filling up the asyncio buffer.
|
|
|
|
# To avoid an infinite loop we cap at 10 times the expected value
|
|
|
|
c_bufsize = c_sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
|
|
|
|
s_bufsize = s_sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
|
|
|
|
for i in range(10):
|
|
|
|
s_wr.write(b'a' * c_bufsize)
|
|
|
|
s_wr.write(b'a' * s_bufsize)
|
|
|
|
if s_wr.transport.get_write_buffer_size() > 0:
|
|
|
|
break
|
2024-03-18 17:15:53 -03:00
|
|
|
self.assertNotEqual(s_wr.transport.get_write_buffer_size(), 0)
|
|
|
|
|
|
|
|
task = asyncio.create_task(srv.wait_closed())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertFalse(task.done())
|
2023-10-28 15:04:29 -03:00
|
|
|
|
2024-03-18 17:15:53 -03:00
|
|
|
srv.close()
|
|
|
|
srv.abort_clients()
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertTrue(task.done())
|
2022-11-24 11:32:58 -04:00
|
|
|
|
|
|
|
|
2023-11-08 12:10:10 -04:00
|
|
|
# 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
|
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
|
|
|
sock.bind(addr)
|
2023-11-08 12:10:10 -04:00
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
srv = await asyncio.start_unix_server(serve, sock=sock)
|
2023-11-08 12:10:10 -04:00
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
srv.close()
|
|
|
|
self.assertFalse(os.path.exists(addr))
|
2023-11-08 12:10:10 -04:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
|
|
|
sock.bind(addr)
|
2023-11-08 12:10:10 -04:00
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
srv = await asyncio.start_unix_server(serve, sock=sock)
|
2023-11-08 12:10:10 -04:00
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
os.unlink(addr)
|
2023-11-08 12:10:10 -04:00
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
srv.close()
|
2023-11-08 12:10:10 -04:00
|
|
|
|
|
|
|
@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)
|
2024-01-10 06:38:36 -04:00
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
|
|
|
sock.bind(addr)
|
2023-11-08 12:10:10 -04:00
|
|
|
|
2024-01-10 06:38:36 -04:00
|
|
|
srv.close()
|
|
|
|
self.assertTrue(os.path.exists(addr))
|
2023-11-08 12:10:10 -04:00
|
|
|
|
|
|
|
@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))
|
|
|
|
|
|
|
|
|
2018-01-25 19:08:09 -04:00
|
|
|
@unittest.skipUnless(hasattr(asyncio, 'ProactorEventLoop'), 'Windows only')
|
|
|
|
class ProactorStartServerTests(BaseStartServer, unittest.TestCase):
|
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
return asyncio.ProactorEventLoop()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|