Skip to content

Commit c683a7d

Browse files
committed
Add comments and simplify test_socket.py
1 parent e054523 commit c683a7d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/trio/_tests/test_socket.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,20 +1124,23 @@ async def receiver() -> None:
11241124
async def test_many_sockets() -> None:
11251125
total = 5000 # Must be more than MAX_AFD_GROUP_SIZE
11261126
sockets = []
1127-
for _x in range(total // 2):
1127+
# Open at most <total> socket pairs
1128+
for opened in range(0, total, 2):
11281129
try:
11291130
a, b = stdlib_socket.socketpair()
1130-
except OSError as e: # pragma: no cover
1131-
# the noqa is "Found assertion on exception `e` in `except` block"
1132-
assert e.errno in (errno.EMFILE, errno.ENFILE) # noqa: PT017
1131+
except OSError as exc: # pragma: no cover
1132+
# Semi-expecting following errors (sockets are files):
1133+
# EMFILE: "Too many open files" (reached kernel cap)
1134+
# ENFILE: "File table overflow" (beyond kernel cap)
1135+
assert exc.errno in (errno.EMFILE, errno.ENFILE) # noqa: PT017
1136+
print(f"Unable to open more than {opened} sockets.")
1137+
# Stop opening any more sockets if too many are open
11331138
break
11341139
sockets += [a, b]
11351140
async with _core.open_nursery() as nursery:
1136-
for s in sockets:
1137-
nursery.start_soon(_core.wait_readable, s)
1141+
for socket in sockets:
1142+
nursery.start_soon(_core.wait_readable, socket)
11381143
await _core.wait_all_tasks_blocked()
11391144
nursery.cancel_scope.cancel()
1140-
for sock in sockets:
1141-
sock.close()
1142-
if _x != total // 2 - 1: # pragma: no cover
1143-
print(f"Unable to open more than {(_x-1)*2} sockets.")
1145+
for socket in sockets:
1146+
socket.close()

0 commit comments

Comments
 (0)