import asyncio import pytest async def handle_echo(reader, writer): # Write some data writer.write(b'somedata') # Wait for the remote to close await reader.read() # Close the writer writer.close() await writer.wait_closed() @pytest.mark.asyncio async def test_bug(event_loop): server = await asyncio.start_server(handle_echo, 'localhost', 0) address = server.sockets[0].getsockname()[:2] reader, writer = await asyncio.open_connection(*address) # Get the first half of the data assert (await reader.read(4)) == b'some' # Close the writer writer.close() await writer.wait_closed() # Get the remaining data assert (await reader.read()) == b'data' server.close() await server.wait_closed()