server_process_mock.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2012 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. class MockServerProcess(object):
  29. def __init__(self, port_obj=None, name=None, cmd=None, env=None, universal_newlines=False, lines=None, crashed=False):
  30. self.timed_out = False
  31. self.lines = lines or []
  32. self.crashed = crashed
  33. self.writes = []
  34. self.cmd = cmd
  35. self.env = env
  36. self.started = False
  37. self.stopped = False
  38. def write(self, bytes):
  39. self.writes.append(bytes)
  40. def has_crashed(self):
  41. return self.crashed
  42. def read_stdout_line(self, deadline):
  43. return self.lines.pop(0) + "\n"
  44. def read_stdout(self, deadline, size):
  45. first_line = self.lines[0]
  46. if size > len(first_line):
  47. self.lines.pop(0)
  48. remaining_size = size - len(first_line) - 1
  49. if not remaining_size:
  50. return first_line + "\n"
  51. return first_line + "\n" + self.read_stdout(deadline, remaining_size)
  52. result = self.lines[0][:size]
  53. self.lines[0] = self.lines[0][size:]
  54. return result
  55. def pop_all_buffered_stderr(self):
  56. return ''
  57. def read_either_stdout_or_stderr_line(self, deadline):
  58. # FIXME: We should have tests which intermix stderr and stdout lines.
  59. return self.read_stdout_line(deadline), None
  60. def start(self):
  61. self.started = True
  62. def stop(self, kill_directly=False):
  63. self.stopped = True
  64. return
  65. def kill(self):
  66. return