diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-11-13 00:00:22 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-11-13 00:00:22 +0100 |
commit | ee60171deba6b3f817c5517c593de49c843fd298 (patch) | |
tree | 03ca0cbc9010bf6e07a238a1932f3f70cb021ee4 /tests/test_subprocess.py | |
parent | ac10ccd53e4bc6a20f72c5dd8afb3ea7c1a98179 (diff) | |
parent | 9dd8580f105f3999e2f5f4283d2033a74051dbc6 (diff) | |
download | trollius-ee60171deba6b3f817c5517c593de49c843fd298.tar.gz |
Merge Tulip into Trollius
Diffstat (limited to 'tests/test_subprocess.py')
-rw-r--r-- | tests/test_subprocess.py | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/tests/test_subprocess.py b/tests/test_subprocess.py index 0e9e1ce..e0ed2c5 100644 --- a/tests/test_subprocess.py +++ b/tests/test_subprocess.py @@ -1,45 +1,52 @@ -from asyncio import subprocess -from asyncio import test_utils -import asyncio +from trollius import subprocess +from trollius import test_utils +import trollius as asyncio +import os import signal import sys import unittest -from test import support +from trollius import From, Return +from trollius import test_support as support if sys.platform != 'win32': - from asyncio import unix_events + from trollius import unix_events +from trollius.py33_exceptions import BrokenPipeError, ConnectionResetError # Program blocking PROGRAM_BLOCKED = [sys.executable, '-c', 'import time; time.sleep(3600)'] # Program copying input to output -PROGRAM_CAT = [ - sys.executable, '-c', - ';'.join(('import sys', - 'data = sys.stdin.buffer.read()', - 'sys.stdout.buffer.write(data)'))] +if sys.version_info >= (3,): + PROGRAM_CAT = ';'.join(('import sys', + 'data = sys.stdin.buffer.read()', + 'sys.stdout.buffer.write(data)')) +else: + PROGRAM_CAT = ';'.join(('import sys', + 'data = sys.stdin.read()', + 'sys.stdout.write(data)')) +PROGRAM_CAT = [sys.executable, '-c', PROGRAM_CAT] -class SubprocessMixin: +class SubprocessMixin(object): def test_stdin_stdout(self): args = PROGRAM_CAT @asyncio.coroutine def run(data): - proc = yield from asyncio.create_subprocess_exec( - *args, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - loop=self.loop) + proc = yield From(asyncio.create_subprocess_exec( + *args, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + loop=self.loop)) # feed data proc.stdin.write(data) - yield from proc.stdin.drain() + yield From(proc.stdin.drain()) proc.stdin.close() # get output and exitcode - data = yield from proc.stdout.read() - exitcode = yield from proc.wait() - return (exitcode, data) + data = yield From(proc.stdout.read()) + exitcode = yield From(proc.wait()) + raise Return(exitcode, data) task = run(b'some data') task = asyncio.wait_for(task, 60.0, loop=self.loop) @@ -52,13 +59,13 @@ class SubprocessMixin: @asyncio.coroutine def run(data): - proc = yield from asyncio.create_subprocess_exec( + proc = yield From(asyncio.create_subprocess_exec( *args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, - loop=self.loop) - stdout, stderr = yield from proc.communicate(data) - return proc.returncode, stdout + loop=self.loop)) + stdout, stderr = yield From(proc.communicate(data)) + raise Return(proc.returncode, stdout) task = run(b'some data') task = asyncio.wait_for(task, 60.0, loop=self.loop) @@ -73,10 +80,14 @@ class SubprocessMixin: exitcode = self.loop.run_until_complete(proc.wait()) self.assertEqual(exitcode, 7) + @test_utils.skipUnless(hasattr(os, 'setsid'), "need os.setsid()") def test_start_new_session(self): + def start_new_session(): + os.setsid() + # start the new process in a new session create = asyncio.create_subprocess_shell('exit 8', - start_new_session=True, + preexec_fn=start_new_session, loop=self.loop) proc = self.loop.run_until_complete(create) exitcode = self.loop.run_until_complete(proc.wait()) @@ -106,9 +117,13 @@ class SubprocessMixin: else: self.assertEqual(-signal.SIGTERM, returncode) - @unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP") + @test_utils.skipIf(sys.platform == 'win32', "Don't have SIGHUP") def test_send_signal(self): - code = 'import time; print("sleeping", flush=True); time.sleep(3600)' + code = '; '.join(( + 'import sys, time', + 'print("sleeping")', + 'sys.stdout.flush()', + 'time.sleep(3600)')) args = [sys.executable, '-c', code] create = asyncio.create_subprocess_exec(*args, loop=self.loop, stdout=subprocess.PIPE) proc = self.loop.run_until_complete(create) @@ -116,12 +131,12 @@ class SubprocessMixin: @asyncio.coroutine def send_signal(proc): # basic synchronization to wait until the program is sleeping - line = yield from proc.stdout.readline() + line = yield From(proc.stdout.readline()) self.assertEqual(line, b'sleeping\n') proc.send_signal(signal.SIGHUP) - returncode = (yield from proc.wait()) - return returncode + returncode = yield From(proc.wait()) + raise Return(returncode) returncode = self.loop.run_until_complete(send_signal(proc)) self.assertEqual(-signal.SIGHUP, returncode) @@ -144,7 +159,7 @@ class SubprocessMixin: @asyncio.coroutine def write_stdin(proc, data): proc.stdin.write(data) - yield from proc.stdin.drain() + yield From(proc.stdin.drain()) coro = write_stdin(proc, large_data) # drain() must raise BrokenPipeError or ConnectionResetError @@ -183,7 +198,7 @@ if sys.platform != 'win32': policy = asyncio.get_event_loop_policy() policy.set_child_watcher(None) self.loop.close() - super().tearDown() + super(SubprocessWatcherMixin, self).tearDown() class SubprocessSafeWatcherTests(SubprocessWatcherMixin, test_utils.TestCase): @@ -210,7 +225,7 @@ else: policy = asyncio.get_event_loop_policy() self.loop.close() policy.set_event_loop(None) - super().tearDown() + super(SubprocessProactorTests, self).tearDown() if __name__ == '__main__': |