Commit 35890abb authored by Andrew Svetlov's avatar Andrew Svetlov Committed by Yury Selivanov

Make StreamServer.close() tests more robust (GH-13790)

parent 7f9a2ae7
...@@ -1507,10 +1507,14 @@ os.close(fd) ...@@ -1507,10 +1507,14 @@ os.close(fd)
def test_stream_server_abort(self): def test_stream_server_abort(self):
server_stream_aborted = False server_stream_aborted = False
fut = self.loop.create_future() fut1 = self.loop.create_future()
fut2 = self.loop.create_future()
async def handle_client(stream): async def handle_client(stream):
await fut data = await stream.readexactly(4)
self.assertEqual(b'data', data)
fut1.set_result(None)
await fut2
self.assertEqual(b'', await stream.readline()) self.assertEqual(b'', await stream.readline())
nonlocal server_stream_aborted nonlocal server_stream_aborted
server_stream_aborted = True server_stream_aborted = True
...@@ -1518,7 +1522,8 @@ os.close(fd) ...@@ -1518,7 +1522,8 @@ os.close(fd)
async def client(srv): async def client(srv):
addr = srv.sockets[0].getsockname() addr = srv.sockets[0].getsockname()
stream = await asyncio.connect(*addr) stream = await asyncio.connect(*addr)
fut.set_result(None) await stream.write(b'data')
await fut2
self.assertEqual(b'', await stream.readline()) self.assertEqual(b'', await stream.readline())
await stream.close() await stream.close()
...@@ -1526,7 +1531,8 @@ os.close(fd) ...@@ -1526,7 +1531,8 @@ os.close(fd)
async with asyncio.StreamServer(handle_client, '127.0.0.1', 0) as server: async with asyncio.StreamServer(handle_client, '127.0.0.1', 0) as server:
await server.start_serving() await server.start_serving()
task = asyncio.create_task(client(server)) task = asyncio.create_task(client(server))
await fut await fut1
fut2.set_result(None)
await server.abort() await server.abort()
await task await task
...@@ -1534,21 +1540,31 @@ os.close(fd) ...@@ -1534,21 +1540,31 @@ os.close(fd)
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
self.loop.run_until_complete(test()) self.loop.run_until_complete(test())
self.assertEqual(messages, []) self.assertEqual(messages, [])
self.assertTrue(fut.done()) self.assertTrue(fut1.done())
self.assertTrue(fut2.done())
self.assertTrue(server_stream_aborted) self.assertTrue(server_stream_aborted)
def test_stream_shutdown_hung_task(self): def test_stream_shutdown_hung_task(self):
fut1 = self.loop.create_future() fut1 = self.loop.create_future()
fut2 = self.loop.create_future() fut2 = self.loop.create_future()
cancelled = self.loop.create_future()
async def handle_client(stream): async def handle_client(stream):
data = await stream.readexactly(4)
self.assertEqual(b'data', data)
fut1.set_result(None)
await fut2
try:
while True: while True:
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
except asyncio.CancelledError:
cancelled.set_result(None)
raise
async def client(srv): async def client(srv):
addr = srv.sockets[0].getsockname() addr = srv.sockets[0].getsockname()
stream = await asyncio.connect(*addr) stream = await asyncio.connect(*addr)
fut1.set_result(None) await stream.write(b'data')
await fut2 await fut2
self.assertEqual(b'', await stream.readline()) self.assertEqual(b'', await stream.readline())
await stream.close() await stream.close()
...@@ -1561,9 +1577,10 @@ os.close(fd) ...@@ -1561,9 +1577,10 @@ os.close(fd)
await server.start_serving() await server.start_serving()
task = asyncio.create_task(client(server)) task = asyncio.create_task(client(server))
await fut1 await fut1
await server.close()
fut2.set_result(None) fut2.set_result(None)
await server.close()
await task await task
await cancelled
messages = [] messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
...@@ -1571,21 +1588,28 @@ os.close(fd) ...@@ -1571,21 +1588,28 @@ os.close(fd)
self.assertEqual(messages, []) self.assertEqual(messages, [])
self.assertTrue(fut1.done()) self.assertTrue(fut1.done())
self.assertTrue(fut2.done()) self.assertTrue(fut2.done())
self.assertTrue(cancelled.done())
def test_stream_shutdown_hung_task_prevents_cancellation(self): def test_stream_shutdown_hung_task_prevents_cancellation(self):
fut1 = self.loop.create_future() fut1 = self.loop.create_future()
fut2 = self.loop.create_future() fut2 = self.loop.create_future()
cancelled = self.loop.create_future()
do_handle_client = True do_handle_client = True
async def handle_client(stream): async def handle_client(stream):
data = await stream.readexactly(4)
self.assertEqual(b'data', data)
fut1.set_result(None)
await fut2
while do_handle_client: while do_handle_client:
with contextlib.suppress(asyncio.CancelledError): with contextlib.suppress(asyncio.CancelledError):
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
cancelled.set_result(None)
async def client(srv): async def client(srv):
addr = srv.sockets[0].getsockname() addr = srv.sockets[0].getsockname()
stream = await asyncio.connect(*addr) stream = await asyncio.connect(*addr)
fut1.set_result(None) await stream.write(b'data')
await fut2 await fut2
self.assertEqual(b'', await stream.readline()) self.assertEqual(b'', await stream.readline())
await stream.close() await stream.close()
...@@ -1598,11 +1622,12 @@ os.close(fd) ...@@ -1598,11 +1622,12 @@ os.close(fd)
await server.start_serving() await server.start_serving()
task = asyncio.create_task(client(server)) task = asyncio.create_task(client(server))
await fut1 await fut1
fut2.set_result(None)
await server.close() await server.close()
nonlocal do_handle_client nonlocal do_handle_client
do_handle_client = False do_handle_client = False
fut2.set_result(None)
await task await task
await cancelled
messages = [] messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
...@@ -1612,6 +1637,7 @@ os.close(fd) ...@@ -1612,6 +1637,7 @@ os.close(fd)
"<Task pending .+ ignored cancellation request") "<Task pending .+ ignored cancellation request")
self.assertTrue(fut1.done()) self.assertTrue(fut1.done())
self.assertTrue(fut2.done()) self.assertTrue(fut2.done())
self.assertTrue(cancelled.done())
def test_sendfile(self): def test_sendfile(self):
messages = [] messages = []
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment