Skip to content

Commit f0749da

Browse files
asvetlovvstinner
authored andcommitted
bpo-35998: Avoid TimeoutError in test_asyncio: test_start_tls_server_1() (GH-14080)
1 parent 431478d commit f0749da

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

Lib/test/test_asyncio/test_sslproto.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,14 @@ async def client(addr):
494494

495495
def test_start_tls_server_1(self):
496496
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
497+
ANSWER = b'answer'
497498

498499
server_context = test_utils.simple_server_sslcontext()
499500
client_context = test_utils.simple_client_sslcontext()
500-
if sys.platform.startswith('freebsd') or sys.platform.startswith('win'):
501-
# bpo-35031: Some FreeBSD and Windows buildbots fail to run this test
502-
# as the eof was not being received by the server if the payload
503-
# size is not big enough. This behaviour only appears if the
504-
# client is using TLS1.3.
505-
client_context.options |= ssl.OP_NO_TLSv1_3
501+
answer = None
506502

507503
def client(sock, addr):
504+
nonlocal answer
508505
sock.settimeout(self.TIMEOUT)
509506

510507
sock.connect(addr)
@@ -513,33 +510,36 @@ def client(sock, addr):
513510

514511
sock.start_tls(client_context)
515512
sock.sendall(HELLO_MSG)
516-
517-
sock.shutdown(socket.SHUT_RDWR)
513+
answer = sock.recv_all(len(ANSWER))
518514
sock.close()
519515

520516
class ServerProto(asyncio.Protocol):
521-
def __init__(self, on_con, on_eof, on_con_lost):
517+
def __init__(self, on_con, on_con_lost):
522518
self.on_con = on_con
523-
self.on_eof = on_eof
524519
self.on_con_lost = on_con_lost
525520
self.data = b''
521+
self.transport = None
526522

527523
def connection_made(self, tr):
524+
self.transport = tr
528525
self.on_con.set_result(tr)
529526

527+
def replace_transport(self, tr):
528+
self.transport = tr
529+
530530
def data_received(self, data):
531531
self.data += data
532-
533-
def eof_received(self):
534-
self.on_eof.set_result(1)
532+
if len(self.data) >= len(HELLO_MSG):
533+
self.transport.write(ANSWER)
535534

536535
def connection_lost(self, exc):
536+
self.transport = None
537537
if exc is None:
538538
self.on_con_lost.set_result(None)
539539
else:
540540
self.on_con_lost.set_exception(exc)
541541

542-
async def main(proto, on_con, on_eof, on_con_lost):
542+
async def main(proto, on_con, on_con_lost):
543543
tr = await on_con
544544
tr.write(HELLO_MSG)
545545

@@ -550,16 +550,16 @@ async def main(proto, on_con, on_eof, on_con_lost):
550550
server_side=True,
551551
ssl_handshake_timeout=self.TIMEOUT)
552552

553-
await on_eof
553+
proto.replace_transport(new_tr)
554+
554555
await on_con_lost
555556
self.assertEqual(proto.data, HELLO_MSG)
556557
new_tr.close()
557558

558559
async def run_main():
559560
on_con = self.loop.create_future()
560-
on_eof = self.loop.create_future()
561561
on_con_lost = self.loop.create_future()
562-
proto = ServerProto(on_con, on_eof, on_con_lost)
562+
proto = ServerProto(on_con, on_con_lost)
563563

564564
server = await self.loop.create_server(
565565
lambda: proto, '127.0.0.1', 0)
@@ -568,11 +568,12 @@ async def run_main():
568568
with self.tcp_client(lambda sock: client(sock, addr),
569569
timeout=self.TIMEOUT):
570570
await asyncio.wait_for(
571-
main(proto, on_con, on_eof, on_con_lost),
571+
main(proto, on_con, on_con_lost),
572572
timeout=self.TIMEOUT)
573573

574574
server.close()
575575
await server.wait_closed()
576+
self.assertEqual(answer, ANSWER)
576577

577578
self.loop.run_until_complete(run_main())
578579

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid TimeoutError in test_asyncio: test_start_tls_server_1()

0 commit comments

Comments
 (0)