Skip to content

Commit 33feb2e

Browse files
authored
bpo-35998: Avoid TimeoutError in test_asyncio: test_start_tls_server_1() (GH-14080) (GH-14086)
(cherry picked from commit f0749da)
1 parent 0bd1469 commit 33feb2e

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
@@ -491,17 +491,14 @@ async def client(addr):
491491

492492
def test_start_tls_server_1(self):
493493
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
494+
ANSWER = b'answer'
494495

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

504500
def client(sock, addr):
501+
nonlocal answer
505502
sock.settimeout(self.TIMEOUT)
506503

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

511508
sock.start_tls(client_context)
512509
sock.sendall(HELLO_MSG)
513-
514-
sock.shutdown(socket.SHUT_RDWR)
510+
answer = sock.recv_all(len(ANSWER))
515511
sock.close()
516512

517513
class ServerProto(asyncio.Protocol):
518-
def __init__(self, on_con, on_eof, on_con_lost):
514+
def __init__(self, on_con, on_con_lost):
519515
self.on_con = on_con
520-
self.on_eof = on_eof
521516
self.on_con_lost = on_con_lost
522517
self.data = b''
518+
self.transport = None
523519

524520
def connection_made(self, tr):
521+
self.transport = tr
525522
self.on_con.set_result(tr)
526523

524+
def replace_transport(self, tr):
525+
self.transport = tr
526+
527527
def data_received(self, data):
528528
self.data += data
529-
530-
def eof_received(self):
531-
self.on_eof.set_result(1)
529+
if len(self.data) >= len(HELLO_MSG):
530+
self.transport.write(ANSWER)
532531

533532
def connection_lost(self, exc):
533+
self.transport = None
534534
if exc is None:
535535
self.on_con_lost.set_result(None)
536536
else:
537537
self.on_con_lost.set_exception(exc)
538538

539-
async def main(proto, on_con, on_eof, on_con_lost):
539+
async def main(proto, on_con, on_con_lost):
540540
tr = await on_con
541541
tr.write(HELLO_MSG)
542542

@@ -547,16 +547,16 @@ async def main(proto, on_con, on_eof, on_con_lost):
547547
server_side=True,
548548
ssl_handshake_timeout=self.TIMEOUT)
549549

550-
await on_eof
550+
proto.replace_transport(new_tr)
551+
551552
await on_con_lost
552553
self.assertEqual(proto.data, HELLO_MSG)
553554
new_tr.close()
554555

555556
async def run_main():
556557
on_con = self.loop.create_future()
557-
on_eof = self.loop.create_future()
558558
on_con_lost = self.loop.create_future()
559-
proto = ServerProto(on_con, on_eof, on_con_lost)
559+
proto = ServerProto(on_con, on_con_lost)
560560

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

571571
server.close()
572572
await server.wait_closed()
573+
self.assertEqual(answer, ANSWER)
573574

574575
self.loop.run_until_complete(run_main())
575576

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)