Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 1 | # Copyright (C) 2011 Google Inc. All rights reserved. |
| 2 | # |
| 3 | # Redistribution and use in source and binary forms, with or without |
| 4 | # modification, are permitted provided that the following conditions are |
| 5 | # met: |
| 6 | # |
| 7 | # * Redistributions of source code must retain the above copyright |
| 8 | # notice, this list of conditions and the following disclaimer. |
| 9 | # * Redistributions in binary form must reproduce the above |
| 10 | # copyright notice, this list of conditions and the following disclaimer |
| 11 | # in the documentation and/or other materials provided with the |
| 12 | # distribution. |
| 13 | # * Neither the name of Google Inc. nor the names of its |
| 14 | # contributors may be used to endorse or promote products derived from |
| 15 | # this software without specific prior written permission. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | import sys |
| 30 | import time |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 31 | import webkitpy.thirdparty.unittest2 as unittest |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 32 | |
| 33 | from webkitpy.layout_tests.port.factory import PortFactory |
| 34 | from webkitpy.layout_tests.port import server_process |
| 35 | from webkitpy.common.system.systemhost import SystemHost |
| 36 | from webkitpy.common.system.systemhost_mock import MockSystemHost |
| 37 | from webkitpy.common.system.outputcapture import OutputCapture |
| 38 | |
| 39 | |
| 40 | |
| 41 | class TrivialMockPort(object): |
| 42 | def __init__(self): |
| 43 | self.host = MockSystemHost() |
| 44 | self.host.executive.kill_process = lambda x: None |
| 45 | self.host.executive.kill_process = lambda x: None |
| 46 | |
| 47 | def results_directory(self): |
| 48 | return "/mock-results" |
| 49 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 50 | def process_kill_time(self): |
| 51 | return 1 |
| 52 | |
| 53 | |
| 54 | class MockFile(object): |
| 55 | def __init__(self, server_process): |
| 56 | self._server_process = server_process |
| 57 | self.closed = False |
| 58 | |
| 59 | def fileno(self): |
| 60 | return 1 |
| 61 | |
| 62 | def write(self, line): |
| 63 | self._server_process.broken_pipes.append(self) |
| 64 | raise IOError |
| 65 | |
| 66 | def close(self): |
| 67 | self.closed = True |
| 68 | |
| 69 | |
| 70 | class MockProc(object): |
| 71 | def __init__(self, server_process): |
| 72 | self.stdin = MockFile(server_process) |
| 73 | self.stdout = MockFile(server_process) |
| 74 | self.stderr = MockFile(server_process) |
| 75 | self.pid = 1 |
| 76 | |
| 77 | def poll(self): |
| 78 | return 1 |
| 79 | |
| 80 | def wait(self): |
| 81 | return 0 |
| 82 | |
| 83 | |
| 84 | class FakeServerProcess(server_process.ServerProcess): |
| 85 | def _start(self): |
| 86 | self._proc = MockProc(self) |
| 87 | self.stdin = self._proc.stdin |
| 88 | self.stdout = self._proc.stdout |
| 89 | self.stderr = self._proc.stderr |
| 90 | self._pid = self._proc.pid |
| 91 | self.broken_pipes = [] |
| 92 | |
| 93 | |
| 94 | class TestServerProcess(unittest.TestCase): |
| 95 | def test_basic(self): |
| 96 | cmd = [sys.executable, '-c', 'import sys; import time; time.sleep(0.02); print "stdout"; sys.stdout.flush(); print >>sys.stderr, "stderr"'] |
| 97 | host = SystemHost() |
| 98 | factory = PortFactory(host) |
| 99 | port = factory.get() |
| 100 | now = time.time() |
| 101 | proc = server_process.ServerProcess(port, 'python', cmd) |
| 102 | proc.write('') |
| 103 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 104 | self.assertEqual(proc.poll(), None) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 105 | self.assertFalse(proc.has_crashed()) |
| 106 | |
| 107 | # check that doing a read after an expired deadline returns |
| 108 | # nothing immediately. |
| 109 | line = proc.read_stdout_line(now - 1) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 110 | self.assertEqual(line, None) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 111 | |
| 112 | # FIXME: This part appears to be flaky. line should always be non-None. |
| 113 | # FIXME: https://bugs.webkit.org/show_bug.cgi?id=88280 |
| 114 | line = proc.read_stdout_line(now + 1.0) |
| 115 | if line: |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 116 | self.assertEqual(line.strip(), "stdout") |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 117 | |
| 118 | line = proc.read_stderr_line(now + 1.0) |
| 119 | if line: |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 120 | self.assertEqual(line.strip(), "stderr") |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 121 | |
| 122 | proc.stop(0) |
| 123 | |
| 124 | def test_cleanup(self): |
| 125 | port_obj = TrivialMockPort() |
| 126 | server_process = FakeServerProcess(port_obj=port_obj, name="test", cmd=["test"]) |
| 127 | server_process._start() |
| 128 | server_process.stop() |
| 129 | self.assertTrue(server_process.stdin.closed) |
| 130 | self.assertTrue(server_process.stdout.closed) |
| 131 | self.assertTrue(server_process.stderr.closed) |
| 132 | |
| 133 | def test_broken_pipe(self): |
| 134 | port_obj = TrivialMockPort() |
| 135 | |
| 136 | port_obj.host.platform.os_name = 'win' |
| 137 | server_process = FakeServerProcess(port_obj=port_obj, name="test", cmd=["test"]) |
| 138 | server_process.write("should break") |
| 139 | self.assertTrue(server_process.has_crashed()) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 140 | self.assertIsNotNone(server_process.pid()) |
| 141 | self.assertIsNone(server_process._proc) |
| 142 | self.assertEqual(server_process.broken_pipes, [server_process.stdin]) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 143 | |
| 144 | port_obj.host.platform.os_name = 'mac' |
| 145 | server_process = FakeServerProcess(port_obj=port_obj, name="test", cmd=["test"]) |
| 146 | server_process.write("should break") |
| 147 | self.assertTrue(server_process.has_crashed()) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 148 | self.assertIsNone(server_process._proc) |
| 149 | self.assertEqual(server_process.broken_pipes, [server_process.stdin]) |