metered_stream_unittest.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2010, 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. import logging
  29. import re
  30. import StringIO
  31. import unittest2 as unittest
  32. from webkitpy.layout_tests.views.metered_stream import MeteredStream
  33. class RegularTest(unittest.TestCase):
  34. verbose = False
  35. isatty = False
  36. def setUp(self):
  37. self.stream = StringIO.StringIO()
  38. self.buflist = self.stream.buflist
  39. self.stream.isatty = lambda: self.isatty
  40. # configure a logger to test that log calls do normally get included.
  41. self.logger = logging.getLogger(__name__)
  42. self.logger.setLevel(logging.DEBUG)
  43. self.logger.propagate = False
  44. # add a dummy time counter for a default behavior.
  45. self.times = range(10)
  46. self.meter = MeteredStream(self.stream, self.verbose, self.logger, self.time_fn, 8675)
  47. def tearDown(self):
  48. if self.meter:
  49. self.meter.cleanup()
  50. self.meter = None
  51. def time_fn(self):
  52. return self.times.pop(0)
  53. def test_logging_not_included(self):
  54. # This tests that if we don't hand a logger to the MeteredStream,
  55. # nothing is logged.
  56. logging_stream = StringIO.StringIO()
  57. handler = logging.StreamHandler(logging_stream)
  58. root_logger = logging.getLogger()
  59. orig_level = root_logger.level
  60. root_logger.addHandler(handler)
  61. root_logger.setLevel(logging.DEBUG)
  62. try:
  63. self.meter = MeteredStream(self.stream, self.verbose, None, self.time_fn, 8675)
  64. self.meter.write_throttled_update('foo')
  65. self.meter.write_update('bar')
  66. self.meter.write('baz')
  67. self.assertEqual(logging_stream.buflist, [])
  68. finally:
  69. root_logger.removeHandler(handler)
  70. root_logger.setLevel(orig_level)
  71. def _basic(self, times):
  72. self.times = times
  73. self.meter.write_update('foo')
  74. self.meter.write_update('bar')
  75. self.meter.write_throttled_update('baz')
  76. self.meter.write_throttled_update('baz 2')
  77. self.meter.writeln('done')
  78. self.assertEqual(self.times, [])
  79. return self.buflist
  80. def test_basic(self):
  81. buflist = self._basic([0, 1, 2, 13, 14])
  82. self.assertEqual(buflist, ['foo\n', 'bar\n', 'baz 2\n', 'done\n'])
  83. def _log_after_update(self):
  84. self.meter.write_update('foo')
  85. self.logger.info('bar')
  86. return self.buflist
  87. def test_log_after_update(self):
  88. buflist = self._log_after_update()
  89. self.assertEqual(buflist, ['foo\n', 'bar\n'])
  90. def test_log_args(self):
  91. self.logger.info('foo %s %d', 'bar', 2)
  92. self.assertEqual(self.buflist, ['foo bar 2\n'])
  93. class TtyTest(RegularTest):
  94. verbose = False
  95. isatty = True
  96. def test_basic(self):
  97. buflist = self._basic([0, 1, 1.05, 1.1, 2])
  98. self.assertEqual(buflist, ['foo',
  99. MeteredStream._erasure('foo'), 'bar',
  100. MeteredStream._erasure('bar'), 'baz 2',
  101. MeteredStream._erasure('baz 2'), 'done\n'])
  102. def test_log_after_update(self):
  103. buflist = self._log_after_update()
  104. self.assertEqual(buflist, ['foo',
  105. MeteredStream._erasure('foo'), 'bar\n'])
  106. class VerboseTest(RegularTest):
  107. isatty = False
  108. verbose = True
  109. def test_basic(self):
  110. buflist = self._basic([0, 1, 2.1, 13, 14.1234])
  111. # We don't bother to match the hours and minutes of the timestamp since
  112. # the local timezone can vary and we can't set that portably and easily.
  113. self.assertTrue(re.match('\d\d:\d\d:00.000 8675 foo\n', buflist[0]))
  114. self.assertTrue(re.match('\d\d:\d\d:01.000 8675 bar\n', buflist[1]))
  115. self.assertTrue(re.match('\d\d:\d\d:13.000 8675 baz 2\n', buflist[2]))
  116. self.assertTrue(re.match('\d\d:\d\d:14.123 8675 done\n', buflist[3]))
  117. self.assertEqual(len(buflist), 4)
  118. def test_log_after_update(self):
  119. buflist = self._log_after_update()
  120. self.assertTrue(re.match('\d\d:\d\d:00.000 8675 foo\n', buflist[0]))
  121. # The second argument should have a real timestamp and pid, so we just check the format.
  122. self.assertTrue(re.match('\d\d:\d\d:\d\d.\d\d\d \d+ bar\n', buflist[1]))
  123. self.assertEqual(len(buflist), 2)
  124. def test_log_args(self):
  125. self.logger.info('foo %s %d', 'bar', 2)
  126. self.assertEqual(len(self.buflist), 1)
  127. self.assertTrue(self.buflist[0].endswith('foo bar 2\n'))