initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / layout_tests / port / server_process_unittest.py
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 unittest
31
32 from webkitpy.layout_tests.port import server_process
33 from webkitpy.common.system.executive import ScriptError
34 from webkitpy.common.system.executive_mock import MockExecutive2
35 from webkitpy.common.system.outputcapture import OutputCapture
36
37
38 def _logging_run_command(args):
39     print args
40
41
42 def _throwing_run_command(args):
43     raise ScriptError("MOCK script error")
44
45
46 class TrivialMockPort(object):
47     def results_directory(self):
48         return "/mock-results"
49
50     def check_for_leaks(self, process_name, process_pid):
51         pass
52
53
54 class MockFile(object):
55     def __init__(self, server_process):
56         self._server_process = server_process
57
58     def fileno(self):
59         return 1
60
61     def write(self, line):
62         self._server_process.broken_pipes.append(self)
63         raise IOError
64
65     def close(self):
66         pass
67
68
69 class MockProc(object):
70     def __init__(self, server_process):
71         self.stdin = MockFile(server_process)
72         self.stdout = MockFile(server_process)
73         self.stderr = MockFile(server_process)
74         self.pid = 1
75
76     def poll(self):
77         return 1
78
79
80 class FakeServerProcess(server_process.ServerProcess):
81     def _start(self):
82         self._proc = MockProc(self)
83         self.stdin = self._proc.stdin
84         self.broken_pipes = []
85
86
87 class TestServerProcess(unittest.TestCase):
88     def test_broken_pipe(self):
89         server_process = FakeServerProcess(port_obj=TrivialMockPort(), name="test", cmd=["test"])
90         server_process.write("should break")
91         self.assertTrue(server_process.crashed)
92         self.assertEquals(server_process._proc, None)
93         self.assertEquals(server_process.broken_pipes, [server_process.stdin])
94
95     def test_sample_process(self):
96         # Currently, sample-on-timeout only works on Darwin.
97         if sys.platform != "darwin":
98             return
99         server_process = FakeServerProcess(port_obj=TrivialMockPort(), name="test", cmd=["test"], executive=MockExecutive2(run_command_fn=_logging_run_command))
100         server_process._proc = MockProc(server_process)
101         expected_stdout = "['/usr/bin/sample', 1, 10, 10, '-file', '/mock-results/test-1.sample.txt']\n"
102         OutputCapture().assert_outputs(self, server_process._sample, expected_stdout=expected_stdout)
103
104     def test_sample_process_throws_exception(self):
105         # Currently, sample-on-timeout only works on Darwin.
106         if sys.platform != "darwin":
107             return
108         server_process = FakeServerProcess(port_obj=TrivialMockPort(), name="test", cmd=["test"], executive=MockExecutive2(run_command_fn=_throwing_run_command))
109         server_process._proc = MockProc(server_process)
110         OutputCapture().assert_outputs(self, server_process._sample)