server_process_unittest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. import sys
  29. import time
  30. import unittest2 as unittest
  31. from webkitpy.port.factory import PortFactory
  32. from webkitpy.port import server_process
  33. from webkitpy.common.system.systemhost import SystemHost
  34. from webkitpy.common.system.systemhost_mock import MockSystemHost
  35. from webkitpy.common.system.outputcapture import OutputCapture
  36. class TrivialMockPort(object):
  37. def __init__(self):
  38. self.host = MockSystemHost()
  39. self.host.executive.kill_process = lambda x: None
  40. self.host.executive.kill_process = lambda x: None
  41. def results_directory(self):
  42. return "/mock-results"
  43. def check_for_leaks(self, process_name, process_pid):
  44. pass
  45. def process_kill_time(self):
  46. return 1
  47. class MockFile(object):
  48. def __init__(self, server_process):
  49. self._server_process = server_process
  50. self.closed = False
  51. def fileno(self):
  52. return 1
  53. def write(self, line):
  54. self._server_process.broken_pipes.append(self)
  55. raise IOError
  56. def close(self):
  57. self.closed = True
  58. class MockProc(object):
  59. def __init__(self, server_process):
  60. self.stdin = MockFile(server_process)
  61. self.stdout = MockFile(server_process)
  62. self.stderr = MockFile(server_process)
  63. self.pid = 1
  64. def poll(self):
  65. return 1
  66. def wait(self):
  67. return 0
  68. class FakeServerProcess(server_process.ServerProcess):
  69. def _start(self):
  70. self._proc = MockProc(self)
  71. self.stdin = self._proc.stdin
  72. self.stdout = self._proc.stdout
  73. self.stderr = self._proc.stderr
  74. self._pid = self._proc.pid
  75. self.broken_pipes = []
  76. class TestServerProcess(unittest.TestCase):
  77. def test_basic(self):
  78. cmd = [sys.executable, '-c', 'import sys; import time; time.sleep(0.02); print "stdout"; sys.stdout.flush(); print >>sys.stderr, "stderr"']
  79. host = SystemHost()
  80. factory = PortFactory(host)
  81. port = factory.get()
  82. now = time.time()
  83. proc = server_process.ServerProcess(port, 'python', cmd)
  84. proc.write('')
  85. self.assertEqual(proc.poll(), None)
  86. self.assertFalse(proc.has_crashed())
  87. # check that doing a read after an expired deadline returns
  88. # nothing immediately.
  89. line = proc.read_stdout_line(now - 1)
  90. self.assertEqual(line, None)
  91. # FIXME: This part appears to be flaky. line should always be non-None.
  92. # FIXME: https://bugs.webkit.org/show_bug.cgi?id=88280
  93. line = proc.read_stdout_line(now + 1.0)
  94. if line:
  95. self.assertEqual(line.strip(), "stdout")
  96. line = proc.read_stderr_line(now + 1.0)
  97. if line:
  98. self.assertEqual(line.strip(), "stderr")
  99. proc.stop(0)
  100. def test_cleanup(self):
  101. port_obj = TrivialMockPort()
  102. server_process = FakeServerProcess(port_obj=port_obj, name="test", cmd=["test"])
  103. server_process._start()
  104. server_process.stop()
  105. self.assertTrue(server_process.stdin.closed)
  106. self.assertTrue(server_process.stdout.closed)
  107. self.assertTrue(server_process.stderr.closed)
  108. def test_broken_pipe(self):
  109. port_obj = TrivialMockPort()
  110. port_obj.host.platform.os_name = 'win'
  111. server_process = FakeServerProcess(port_obj=port_obj, name="test", cmd=["test"])
  112. server_process.write("should break")
  113. self.assertTrue(server_process.has_crashed())
  114. self.assertIsNotNone(server_process.pid())
  115. self.assertIsNone(server_process._proc)
  116. self.assertEqual(server_process.broken_pipes, [server_process.stdin])
  117. port_obj.host.platform.os_name = 'mac'
  118. server_process = FakeServerProcess(port_obj=port_obj, name="test", cmd=["test"])
  119. server_process.write("should break")
  120. self.assertTrue(server_process.has_crashed())
  121. self.assertIsNone(server_process._proc)
  122. self.assertEqual(server_process.broken_pipes, [server_process.stdin])