def testReadlineLimitedBasic(self): num_bytes = 1024 * 8 + 512 bps = 384000 request_counter = lambda: 1 f = StringIO.StringIO(' ' * num_bytes) limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps) start = proxyshaper.TIMER() self.assertEqual(num_bytes, len(limited_f.readline())) expected_ms = 8.0 * num_bytes / bps * 1000.0 actual_ms = (proxyshaper.TIMER() - start) * 1000.0 self.assertValuesAlmostEqual(expected_ms, actual_ms)
def testWriteLimitedBasic(self): num_bytes = 1024 * 10 + 350 bps = 384000 request_counter = lambda: 1 f = StringIO.StringIO() limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps) start = proxyshaper.TIMER() limited_f.write(' ' * num_bytes) self.assertEqual(num_bytes, len(limited_f.getvalue())) expected_ms = 8.0 * num_bytes / bps * 1000.0 actual_ms = (proxyshaper.TIMER() - start) * 1000.0 self.assertAlmostEqual(expected_ms, actual_ms)
def testReadLimitedSlowedByMultipleRequests(self): num_bytes = 1024 bps = 384000 request_count = 2 request_counter = lambda: request_count f = StringIO.StringIO(' ' * num_bytes) limited_f = proxyshaper.RateLimitedFile(request_counter, f, bps) start = proxyshaper.TIMER() num_read_bytes = limited_f.read() self.assertEqual(num_bytes, len(num_read_bytes)) expected_ms = 8.0 * num_bytes / (bps / float(request_count)) * 1000.0 actual_ms = (proxyshaper.TIMER() - start) * 1000.0 self.assertValuesAlmostEqual(expected_ms, actual_ms)