summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>2022-12-28 04:12:16 (GMT)
committerGitHub <noreply@github.com>2022-12-28 04:12:16 (GMT)
commite97afefda54b818b140b3cc905642b69d9d65f0c (patch)
tree87862a59ffbf524a5ebd4af3a786626fd216101f
parent08e5594cf3d42391a48e0311f6b9393ec2e00e1e (diff)
downloadcpython-e97afefda54b818b140b3cc905642b69d9d65f0c.zip
cpython-e97afefda54b818b140b3cc905642b69d9d65f0c.tar.gz
cpython-e97afefda54b818b140b3cc905642b69d9d65f0c.tar.bz2
GH-100192: add more `asyncio` subprocess tests (#100194)
-rw-r--r--Lib/test/test_asyncio/test_subprocess.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py
index 3830dea..1ae290a 100644
--- a/Lib/test/test_asyncio/test_subprocess.py
+++ b/Lib/test/test_asyncio/test_subprocess.py
@@ -686,6 +686,88 @@ class SubprocessMixin:
self.assertIsNone(self.loop.run_until_complete(execute()))
+ async def check_stdout_output(self, coro, output):
+ proc = await coro
+ stdout, _ = await proc.communicate()
+ self.assertEqual(stdout, output)
+ self.assertEqual(proc.returncode, 0)
+ task = asyncio.create_task(proc.wait())
+ await asyncio.sleep(0)
+ self.assertEqual(task.result(), proc.returncode)
+
+ def test_create_subprocess_env_shell(self) -> None:
+ async def main() -> None:
+ cmd = f'''{sys.executable} -c "import os, sys; sys.stdout.write(os.getenv('FOO'))"'''
+ env = {"FOO": 'bar'}
+ proc = await asyncio.create_subprocess_shell(
+ cmd, env=env, stdout=subprocess.PIPE
+ )
+ return proc
+
+ self.loop.run_until_complete(self.check_stdout_output(main(), b'bar'))
+
+ def test_create_subprocess_env_exec(self) -> None:
+ async def main() -> None:
+ cmd = [sys.executable, "-c",
+ "import os, sys; sys.stdout.write(os.getenv('FOO'))"]
+ env = {"FOO": 'baz'}
+ proc = await asyncio.create_subprocess_exec(
+ *cmd, env=env, stdout=subprocess.PIPE
+ )
+ return proc
+
+ self.loop.run_until_complete(self.check_stdout_output(main(), b'baz'))
+
+
+ def test_subprocess_concurrent_wait(self) -> None:
+ async def main() -> None:
+ proc = await asyncio.create_subprocess_exec(
+ *PROGRAM_CAT,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ )
+ stdout, _ = await proc.communicate(b'some data')
+ self.assertEqual(stdout, b"some data")
+ self.assertEqual(proc.returncode, 0)
+ self.assertEqual(await asyncio.gather(*[proc.wait() for _ in range(10)]),
+ [proc.returncode] * 10)
+
+ self.loop.run_until_complete(main())
+
+ def test_subprocess_consistent_callbacks(self):
+ events = []
+ class MyProtocol(asyncio.SubprocessProtocol):
+ def __init__(self, exit_future: asyncio.Future) -> None:
+ self.exit_future = exit_future
+
+ def pipe_data_received(self, fd, data) -> None:
+ events.append(('pipe_data_received', fd, data))
+
+ def pipe_connection_lost(self, fd, exc) -> None:
+ events.append('pipe_connection_lost')
+
+ def process_exited(self) -> None:
+ events.append('process_exited')
+ self.exit_future.set_result(True)
+
+ async def main() -> None:
+ loop = asyncio.get_running_loop()
+ exit_future = asyncio.Future()
+ code = 'import sys; sys.stdout.write("stdout"); sys.stderr.write("stderr")'
+ transport, _ = await loop.subprocess_exec(lambda: MyProtocol(exit_future),
+ sys.executable, '-c', code, stdin=None)
+ await exit_future
+ transport.close()
+ self.assertEqual(events, [
+ ('pipe_data_received', 1, b'stdout'),
+ ('pipe_data_received', 2, b'stderr'),
+ 'pipe_connection_lost',
+ 'pipe_connection_lost',
+ 'process_exited',
+ ])
+
+ self.loop.run_until_complete(main())
+
def test_subprocess_communicate_stdout(self):
# See https://github.com/python/cpython/issues/100133
async def get_command_stdout(cmd, *args):