Skip to content

Commit fa24c1c

Browse files
authored
[3.7] bpo-33353: Fix test_asyncio on FreeBSD (GH-7087)
* bpo-33353: test_asyncio uses smaller sendfile data (#7083) bpo-32622, bpo-33353: sendfile() tests of test_asyncio use socket buffers of 1 kB "to test on relative small data sets". Send only 160 KiB rather 10 MB to make the test much faster. Shrink also SendfileBase.DATA from 1600 KiB to 160 KiB. On Linux, 3 test_sock_sendfile_mix_with_regular_send() runs now take less than 1 second, instead of 18 seconds. On FreeBSD, the 3 tests didn't hang, but took 3 minutes. Now the 3 tests pass in less than 1 seconds. (cherry picked from commit 2932755) * bpo-33353: test_asyncio set SO_SNDBUF after connect (GH-7086) bpo-32622, bpo-33353: On macOS, sock.connect() changes the SO_SNDBUF value. Only set SO_SNDBUF and SO_RCVBUF buffer sizes once a socket is connected or binded, not before. (cherry picked from commit b97de3d)
1 parent 72ef4fc commit fa24c1c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Lib/test/test_asyncio/test_events.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ async def connect(cmd=None, **kwds):
20952095

20962096
class SendfileBase:
20972097

2098-
DATA = b"12345abcde" * 160 * 1024 # 160 KiB
2098+
DATA = b"12345abcde" * 16 * 1024 # 160 KiB
20992099

21002100
@classmethod
21012101
def setUpClass(cls):
@@ -2142,11 +2142,15 @@ def connection_lost(self, exc):
21422142
async def wait_closed(self):
21432143
await self.fut
21442144

2145+
def set_socket_opts(self, sock):
2146+
# On macOS, SO_SNDBUF is reset by connect(). So this method
2147+
# should be called after the socket is connected.
2148+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
2149+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
2150+
21452151
def make_socket(self, cleanup=True):
21462152
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
21472153
sock.setblocking(False)
2148-
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
2149-
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
21502154
if cleanup:
21512155
self.addCleanup(sock.close)
21522156
return sock
@@ -2159,7 +2163,9 @@ def prepare_socksendfile(self):
21592163
srv_sock.bind((support.HOST, port))
21602164
server = self.run_loop(self.loop.create_server(
21612165
lambda: proto, sock=srv_sock))
2166+
self.set_socket_opts(srv_sock)
21622167
self.run_loop(self.loop.sock_connect(sock, ('127.0.0.1', port)))
2168+
self.set_socket_opts(sock)
21632169

21642170
def cleanup():
21652171
if proto.transport is not None:
@@ -2208,7 +2214,7 @@ def test_sock_sendfile_zero_size(self):
22082214
self.assertEqual(self.file.tell(), 0)
22092215

22102216
def test_sock_sendfile_mix_with_regular_send(self):
2211-
buf = b'1234567890' * 1024 * 1024 # 10 MB
2217+
buf = b"X" * 160 * 1024 # 160 KiB
22122218
sock, proto = self.prepare_socksendfile()
22132219
self.run_loop(self.loop.sock_sendall(sock, buf))
22142220
ret = self.run_loop(self.loop.sock_sendfile(sock, self.file))
@@ -2264,9 +2270,9 @@ def prepare_sendfile(self, *, is_ssl=False, close_after=0):
22642270
else:
22652271
server_hostname = None
22662272
cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2273+
cli_sock.connect((support.HOST, port))
22672274
# reduce send socket buffer size to test on relative small data sets
22682275
cli_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
2269-
cli_sock.connect((support.HOST, port))
22702276
cli_proto = self.MySendfileProto(loop=self.loop)
22712277
tr, pr = self.run_loop(self.loop.create_connection(
22722278
lambda: cli_proto, sock=cli_sock,

0 commit comments

Comments
 (0)