@@ -505,7 +505,7 @@ def test_socket_close_remove_writer(self):
505
505
s .close ()
506
506
self .assertEqual (s .fileno (), - 1 )
507
507
508
- def test_socket_cancel_sock_recv (self ):
508
+ def test_socket_cancel_sock_recv_1 (self ):
509
509
def srv_gen (sock ):
510
510
time .sleep (1.2 )
511
511
sock .send (b'helo' )
@@ -534,6 +534,46 @@ async def client(sock, addr):
534
534
w = asyncio .wait_for (c , timeout = 5.0 , loop = self .loop )
535
535
self .loop .run_until_complete (w )
536
536
537
+ def test_socket_cancel_sock_recv_2 (self ):
538
+ def srv_gen (sock ):
539
+ time .sleep (1.2 )
540
+ sock .send (b'helo' )
541
+
542
+ async def kill (fut ):
543
+ await asyncio .sleep (0.5 , loop = self .loop )
544
+ fut .cancel ()
545
+
546
+ async def recv (sock ):
547
+ fut = self .loop .sock_recv (sock , 10 )
548
+ await asyncio .sleep (0.1 , loop = self .loop )
549
+ self .loop .remove_reader (sock )
550
+ sock .close ()
551
+ try :
552
+ await fut
553
+ except asyncio .CancelledError :
554
+ raise
555
+ finally :
556
+ sock .close ()
557
+
558
+ async def client (sock , addr ):
559
+ await self .loop .sock_connect (sock , addr )
560
+
561
+ f = asyncio .ensure_future (recv (sock ), loop = self .loop )
562
+ self .loop .create_task (kill (f ))
563
+ with self .assertRaises (asyncio .CancelledError ):
564
+ await f
565
+ sock .close ()
566
+ self .assertEqual (sock .fileno (), - 1 )
567
+
568
+ with self .tcp_server (srv_gen ) as srv :
569
+
570
+ sock = socket .socket ()
571
+ with sock :
572
+ sock .setblocking (False )
573
+ c = client (sock , srv .addr )
574
+ w = asyncio .wait_for (c , timeout = 5.0 , loop = self .loop )
575
+ self .loop .run_until_complete (w )
576
+
537
577
def test_socket_cancel_sock_sendall (self ):
538
578
def srv_gen (sock ):
539
579
time .sleep (1.2 )
0 commit comments