mirror of
https://github.com/HeyPuter/firefox.git
synced 2026-11-26 04:22:30 -10:00
A new parameter called 'processStderrLine' is added. When specified, stdout gets processed by the 'processOutputLine' callbacks and stderr is processed by the 'processStderrLine' callbacks. When not specified, stderr is redirected to stdout which is the same default behaviour. A side effect of this is that mozprocess now uses three threads to process output. One thread each for stdout and stderr that reads output lines and stores them in a Queue as fast as possible, this makes sure there is no blocking in stdout.write(). A third thread executes the callbacks.
96 lines
3.3 KiB
Python
96 lines
3.3 KiB
Python
import unittest
|
|
import subprocess
|
|
import sys
|
|
from mozprocess.processhandler import ProcessReader, StoreOutput
|
|
|
|
def run_python(str_code, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
|
|
cmd = [sys.executable, '-c', str_code]
|
|
return subprocess.Popen(cmd, stdout=stdout, stderr=stderr)
|
|
|
|
class TestProcessReader(unittest.TestCase):
|
|
def setUp(self):
|
|
self.out = StoreOutput()
|
|
self.err = StoreOutput()
|
|
self.finished = False
|
|
def on_finished():
|
|
self.finished = True
|
|
self.timeout = False
|
|
def on_timeout():
|
|
self.timeout = True
|
|
self.reader = ProcessReader(stdout_callback=self.out,
|
|
stderr_callback=self.err,
|
|
finished_callback=on_finished,
|
|
timeout_callback=on_timeout)
|
|
|
|
def test_stdout_callback(self):
|
|
proc = run_python('print 1; print 2')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
|
|
self.assertEqual(self.out.output, ['1', '2'])
|
|
self.assertEqual(self.err.output, [])
|
|
|
|
def test_stderr_callback(self):
|
|
proc = run_python('import sys; sys.stderr.write("hello world\\n")')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
|
|
self.assertEqual(self.out.output, [])
|
|
self.assertEqual(self.err.output, ['hello world'])
|
|
|
|
def test_stdout_and_stderr_callbacks(self):
|
|
proc = run_python('import sys; sys.stderr.write("hello world\\n"); print 1; print 2')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
|
|
self.assertEqual(self.out.output, ['1', '2'])
|
|
self.assertEqual(self.err.output, ['hello world'])
|
|
|
|
def test_finished_callback(self):
|
|
self.assertFalse(self.finished)
|
|
proc = run_python('')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
self.assertTrue(self.finished)
|
|
|
|
def test_timeout(self):
|
|
self.reader.timeout = 0.05
|
|
self.assertFalse(self.timeout)
|
|
proc = run_python('import time; time.sleep(0.1)')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
self.assertTrue(self.timeout)
|
|
self.assertFalse(self.finished)
|
|
|
|
def test_output_timeout(self):
|
|
self.reader.output_timeout = 0.05
|
|
self.assertFalse(self.timeout)
|
|
proc = run_python('import time; time.sleep(0.1)')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
self.assertTrue(self.timeout)
|
|
self.assertFalse(self.finished)
|
|
|
|
def test_read_without_eol(self):
|
|
proc = run_python('import sys; sys.stdout.write("1")')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
self.assertEqual(self.out.output, ['1'])
|
|
|
|
def test_read_with_strange_eol(self):
|
|
proc = run_python('import sys; sys.stdout.write("1\\r\\r\\r\\n")')
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
self.assertEqual(self.out.output, ['1'])
|
|
|
|
def test_mixed_stdout_stderr(self):
|
|
proc = run_python('import sys; sys.stderr.write("hello world\\n"); print 1; print 2', stderr=subprocess.STDOUT)
|
|
self.reader.start(proc)
|
|
self.reader.thread.join()
|
|
|
|
self.assertEqual(sorted(self.out.output), sorted(['1', '2', 'hello world']))
|
|
self.assertEqual(self.err.output, [])
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|