Visitar URL original
gh-93357: Lay the foundation for further work in `asyncio.test_streams`: port server cases to IsolatedAsyncioTestCase by arhadthedev · Pull Request #93369 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use subtests to localize errors
  • Loading branch information
arhadthedev committed May 31, 2022
commit 3205970760b83a4e7ea7eb1f7ca52d75fab6055f
127 changes: 59 additions & 68 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,32 +584,32 @@ async def handle_client(client_reader, client_writer):
client_writer.close()
await client_writer.wait_closed()

# test the server variant with a coroutine as client handler
server = await asyncio.start_server(
handle_client,
host=socket_helper.HOSTv4
)
addr = server.sockets[0].getsockname()
msg = await client(addr)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")

# test the server variant with a callback as client handler
async def handle_client_with_callback(client_reader, client_writer):
asyncio.get_running_loop().create_task(
handle_client(client_reader, client_writer)
with self.subTest(msg="coroutine"):
server = await asyncio.start_server(
handle_client,
host=socket_helper.HOSTv4
)
addr = server.sockets[0].getsockname()
msg = await client(addr)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")

server = await asyncio.start_server(
handle_client_with_callback,
host=socket_helper.HOSTv4
)
address, port = server.sockets[0].getsockname()
msg = await client(addr)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")
with self.subTest(msg="callback"):
async def handle_client_callback(client_reader, client_writer):
asyncio.get_running_loop().create_task(
handle_client(client_reader, client_writer)
)

server = await asyncio.start_server(
handle_client_callback,
host=socket_helper.HOSTv4
)
address, port = server.sockets[0].getsockname()
msg = await client(addr)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")

@socket_helper.skip_unless_bind_unix_socket
async def test_start_unix_server(self):
Expand All @@ -631,33 +631,33 @@ async def handle_client(client_reader, client_writer):
client_writer.close()
await client_writer.wait_closed()

# test the server variant with a coroutine as client handler
with test_utils.unix_socket_path() as path:
server = await asyncio.start_unix_server(
handle_client,
path=path
)
msg = await client(path)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")

# test the server variant with a callback as client handler
async def handle_client_with_callback(client_reader, client_writer):
asyncio.get_running_loop().create_task(
handle_client(client_reader, client_writer)
)

with test_utils.unix_socket_path() as path:
server = await asyncio.start_unix_server(
handle_client,
path=path
)
server.start_callback()
msg = await client(path)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")
with self.subTest(msg="coroutine"):
with test_utils.unix_socket_path() as path:
server = await asyncio.start_unix_server(
handle_client,
path=path
)
msg = await client(path)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")

with self.subTest(msg="callback"):
async def handle_client_callback(client_reader, client_writer):
asyncio.get_running_loop().create_task(
handle_client(client_reader, client_writer)
)

with test_utils.unix_socket_path() as path:
server = await asyncio.start_unix_server(
handle_client_callback,
path=path
)
server.start_callback()
msg = await client(path)
server.close()
await server.wait_closed()
self.assertEqual(msg, b"hello world!\n")

@unittest.skipIf(ssl is None, 'No ssl module')
async def test_start_tls(self):
Expand Down Expand Up @@ -918,22 +918,20 @@ def test_LimitOverrunError_pickleable(self):
self.assertEqual(str(e), str(e2))
self.assertEqual(e.consumed, e2.consumed)

def test_wait_closed_on_close(self):
with test_utils.run_test_server() as httpd:
async def test_wait_closed_on_close(self):
async with test_utils.run_test_server() as httpd:
rd, wr = self.loop.run_until_complete(
asyncio.open_connection(*httpd.address))

wr.write(b'GET / HTTP/1.0\r\n\r\n')
f = rd.readline()
data = self.loop.run_until_complete(f)
await rd.readline()
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
Comment thread
arhadthedev marked this conversation as resolved.
f = rd.read()
data = self.loop.run_until_complete(f)
await rd.read()
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
self.assertFalse(wr.is_closing())
wr.close()
self.assertTrue(wr.is_closing())
self.loop.run_until_complete(wr.wait_closed())
await wr.wait_closed()

def test_wait_closed_on_close_with_unread_data(self):
with test_utils.run_test_server() as httpd:
Expand Down Expand Up @@ -989,15 +987,10 @@ async def inner(httpd):

self.assertEqual(messages, [])

def test_eof_feed_when_closing_writer(self):
async def test_eof_feed_when_closing_writer(self):
# See http://bugs.python.org/issue35065
messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like .set_exception_handler is now missing from this test. Is it fine?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've removed all usages of messages inter-thread communication list:

  1. -        messages = []
    -        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
  2. -        self.assertEqual(messages, [])

because the server is now async, lives in a main thread, and needs no intermediary to pass exceptions into a test harness.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, thank you for noticing, I've added the clarification as the second paragraph of the first PR comment.


with test_utils.run_test_server() as httpd:
rd, wr = self.loop.run_until_complete(
asyncio.open_connection(*httpd.address))

async with test_utils.run_test_server() as httpd:
rd, wr = await asyncio.open_connection(*httpd.address)
wr.close()
f = wr.wait_closed()
self.loop.run_until_complete(f)
Expand All @@ -1006,8 +999,6 @@ def test_eof_feed_when_closing_writer(self):
data = self.loop.run_until_complete(f)
self.assertEqual(data, b'')

self.assertEqual(messages, [])


if __name__ == '__main__':
unittest.main()