proxyshaper_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. # Copyright 2012 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Unit tests for proxyshaper.
  16. Usage:
  17. $ ./proxyshaper_test.py
  18. """
  19. import proxyshaper
  20. import StringIO
  21. import unittest
  22. # pylint: disable=bad-whitespace
  23. VALID_RATES = (
  24. # input, expected_bps
  25. ( '384Kbit/s', 384000),
  26. ('1536Kbit/s', 1536000),
  27. ( '1Mbit/s', 1000000),
  28. ( '5Mbit/s', 5000000),
  29. ( '2MByte/s', 16000000),
  30. ( '0', 0),
  31. ( '5', 5),
  32. ( 384000, 384000),
  33. )
  34. ERROR_RATES = (
  35. '1536KBit/s', # Older versions of dummynet used capital 'B' for bytes.
  36. '1Mbyte/s', # Require capital 'B' for bytes.
  37. '5bps',
  38. )
  39. class TimedTestCase(unittest.TestCase):
  40. def assertValuesAlmostEqual(self, expected, actual, tolerance=0.05):
  41. """Like the following with nicer default message:
  42. assertTrue(expected <= actual + tolerance &&
  43. expected >= actual - tolerance)
  44. """
  45. delta = tolerance * expected
  46. if actual > expected + delta or actual < expected - delta:
  47. self.fail('%s is not equal to expected %s +/- %s%%' % (
  48. actual, expected, 100 * tolerance))
  49. class RateLimitedFileTest(TimedTestCase):
  50. def testReadLimitedBasic(self):
  51. num_bytes = 1024
  52. bps = 384000
  53. request_counter = lambda: 1
  54. f = StringIO.StringIO(' ' * num_bytes)
  55. limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps)
  56. start = proxyshaper.TIMER()
  57. self.assertEqual(num_bytes, len(limited_f.read()))
  58. expected_ms = 8.0 * num_bytes / bps * 1000.0
  59. actual_ms = (proxyshaper.TIMER() - start) * 1000.0
  60. self.assertValuesAlmostEqual(expected_ms, actual_ms)
  61. def testReadlineLimitedBasic(self):
  62. num_bytes = 1024 * 8 + 512
  63. bps = 384000
  64. request_counter = lambda: 1
  65. f = StringIO.StringIO(' ' * num_bytes)
  66. limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps)
  67. start = proxyshaper.TIMER()
  68. self.assertEqual(num_bytes, len(limited_f.readline()))
  69. expected_ms = 8.0 * num_bytes / bps * 1000.0
  70. actual_ms = (proxyshaper.TIMER() - start) * 1000.0
  71. self.assertValuesAlmostEqual(expected_ms, actual_ms)
  72. def testReadLimitedSlowedByMultipleRequests(self):
  73. num_bytes = 1024
  74. bps = 384000
  75. request_count = 2
  76. request_counter = lambda: request_count
  77. f = StringIO.StringIO(' ' * num_bytes)
  78. limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps)
  79. start = proxyshaper.TIMER()
  80. num_read_bytes = limited_f.read()
  81. self.assertEqual(num_bytes, len(num_read_bytes))
  82. expected_ms = 8.0 * num_bytes / (bps / float(request_count)) * 1000.0
  83. actual_ms = (proxyshaper.TIMER() - start) * 1000.0
  84. self.assertValuesAlmostEqual(expected_ms, actual_ms)
  85. def testWriteLimitedBasic(self):
  86. num_bytes = 1024 * 10 + 350
  87. bps = 384000
  88. request_counter = lambda: 1
  89. f = StringIO.StringIO()
  90. limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps)
  91. start = proxyshaper.TIMER()
  92. limited_f.write(' ' * num_bytes)
  93. self.assertEqual(num_bytes, len(limited_f.getvalue()))
  94. expected_ms = 8.0 * num_bytes / bps * 1000.0
  95. actual_ms = (proxyshaper.TIMER() - start) * 1000.0
  96. self.assertValuesAlmostEqual(expected_ms, actual_ms)
  97. def testWriteLimitedSlowedByMultipleRequests(self):
  98. num_bytes = 1024 * 10
  99. bps = 384000
  100. request_count = 2
  101. request_counter = lambda: request_count
  102. f = StringIO.StringIO(' ' * num_bytes)
  103. limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps)
  104. start = proxyshaper.TIMER()
  105. limited_f.write(' ' * num_bytes)
  106. self.assertEqual(num_bytes, len(limited_f.getvalue()))
  107. expected_ms = 8.0 * num_bytes / (bps / float(request_count)) * 1000.0
  108. actual_ms = (proxyshaper.TIMER() - start) * 1000.0
  109. self.assertValuesAlmostEqual(expected_ms, actual_ms)
  110. class GetBitsPerSecondTest(unittest.TestCase):
  111. def testConvertsValidValues(self):
  112. for dummynet_option, expected_bps in VALID_RATES:
  113. bps = proxyshaper.GetBitsPerSecond(dummynet_option)
  114. self.assertEqual(
  115. expected_bps, bps, 'Unexpected result for %s: %s != %s' % (
  116. dummynet_option, expected_bps, bps))
  117. def testRaisesOnUnexpectedValues(self):
  118. for dummynet_option in ERROR_RATES:
  119. self.assertRaises(proxyshaper.BandwidthValueError,
  120. proxyshaper.GetBitsPerSecond, dummynet_option)
  121. if __name__ == '__main__':
  122. unittest.main()