summaryrefslogtreecommitdiff
path: root/tests/test_subprocess.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_subprocess.py')
-rw-r--r--tests/test_subprocess.py81
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__':