77 skip_tests = False
88
99import asyncio
10+ import os
1011import sys
1112import unittest
1213import weakref
1314
1415from uvloop import _testbase as tb
1516
1617
17- class _TestAioHTTP :
18+ class _TestAioHTTP ( tb . SSLTestCase ) :
1819
1920 def test_aiohttp_basic_1 (self ):
2021
@@ -95,7 +96,7 @@ async def on_shutdown(app):
9596 async def client ():
9697 async with aiohttp .ClientSession () as client :
9798 async with client .ws_connect (
98- 'http://127.0.0.1:{}' .format (port )) as ws :
99+ 'http://127.0.0.1:{}' .format (port )) as ws :
99100 await ws .send_str ("hello" )
100101 async for msg in ws :
101102 assert msg .data == "hello"
@@ -115,6 +116,59 @@ async def stop():
115116
116117 self .loop .run_until_complete (stop ())
117118
119+ def test_aiohttp_connection_lost_when_busy (self ):
120+ if self .implementation == 'asyncio' :
121+ raise unittest .SkipTest ('bug in asyncio #118950, tests in CPython.' )
122+
123+ cert = tb ._cert_fullname (__file__ , 'ssl_cert.pem' )
124+ key = tb ._cert_fullname (__file__ , 'ssl_key.pem' )
125+ ssl_context = self ._create_server_ssl_context (cert , key )
126+ client_ssl_context = self ._create_client_ssl_context ()
127+
128+ asyncio .set_event_loop (self .loop )
129+ app = aiohttp .web .Application ()
130+
131+ async def handler (request ):
132+ ws = aiohttp .web .WebSocketResponse ()
133+ await ws .prepare (request )
134+ async for msg in ws :
135+ print ("Received:" , msg .data )
136+ return ws
137+
138+ app .router .add_get ('/' , handler )
139+
140+ runner = aiohttp .web .AppRunner (app )
141+ self .loop .run_until_complete (runner .setup ())
142+ host = '0.0.0.0'
143+ site = aiohttp .web .TCPSite (runner , host , '0' , ssl_context = ssl_context )
144+ self .loop .run_until_complete (site .start ())
145+ port = site ._server .sockets [0 ].getsockname ()[1 ]
146+ session = aiohttp .ClientSession (loop = self .loop )
147+
148+ async def test ():
149+ async with session .ws_connect (f"wss://{ host } :{ port } /" , ssl = client_ssl_context ) as ws :
150+ transport = ws ._writer .transport
151+ s = transport .get_extra_info ('socket' )
152+
153+ if self .implementation == 'asyncio' :
154+ s ._sock .close ()
155+ else :
156+ os .close (s .fileno ())
157+
158+ # FLOW_CONTROL_HIGH_WATER * 1024
159+ bytes_to_send = 64 * 1024
160+ iterations = 10
161+ msg = b'Hello world, still there?'
162+
163+ # Send enough messages to trigger a socket write + one extra
164+ for _ in range (iterations + 1 ):
165+ await ws .send_bytes (msg * ((bytes_to_send // len (msg )) // iterations ))
166+
167+ self .assertRaises (ConnectionResetError , self .loop .run_until_complete , test ())
168+
169+ self .loop .run_until_complete (session .close ())
170+ self .loop .run_until_complete (runner .cleanup ())
171+
118172
119173@unittest .skipIf (skip_tests , "no aiohttp module" )
120174class Test_UV_AioHTTP (_TestAioHTTP , tb .UVTestCase ):
0 commit comments