Skip to content

Commit

Permalink
Add comments and simplify test_socket.py
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Dec 6, 2023
1 parent e054523 commit c683a7d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/trio/_tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,20 +1124,23 @@ async def receiver() -> None:
async def test_many_sockets() -> None:
total = 5000 # Must be more than MAX_AFD_GROUP_SIZE
sockets = []
for _x in range(total // 2):
# Open at most <total> socket pairs
for opened in range(0, total, 2):
try:
a, b = stdlib_socket.socketpair()
except OSError as e: # pragma: no cover
# the noqa is "Found assertion on exception `e` in `except` block"
assert e.errno in (errno.EMFILE, errno.ENFILE) # noqa: PT017
except OSError as exc: # pragma: no cover
# Semi-expecting following errors (sockets are files):
# EMFILE: "Too many open files" (reached kernel cap)
# ENFILE: "File table overflow" (beyond kernel cap)
assert exc.errno in (errno.EMFILE, errno.ENFILE) # noqa: PT017
print(f"Unable to open more than {opened} sockets.")
# Stop opening any more sockets if too many are open
break
sockets += [a, b]
async with _core.open_nursery() as nursery:
for s in sockets:
nursery.start_soon(_core.wait_readable, s)
for socket in sockets:
nursery.start_soon(_core.wait_readable, socket)
await _core.wait_all_tasks_blocked()
nursery.cancel_scope.cancel()
for sock in sockets:
sock.close()
if _x != total // 2 - 1: # pragma: no cover
print(f"Unable to open more than {(_x-1)*2} sockets.")
for socket in sockets:
socket.close()

0 comments on commit c683a7d

Please sign in to comment.