Example #1
0
class TestRateLimiter(ScalyrTestCase):
    def setUp(self):
        super(TestRateLimiter, self).setUp()
        self.__test_rate = RateLimiter(100, 10, current_time=0)
        self.__current_time = 0

    def advance_time(self, delta):
        self.__current_time += delta

    def charge_if_available(self, num_bytes):
        return self.__test_rate.charge_if_available(
            num_bytes, current_time=self.__current_time)

    def test_basic_use(self):
        self.assertTrue(self.charge_if_available(20))
        self.assertTrue(self.charge_if_available(80))
        self.assertFalse(self.charge_if_available(1))

    def test_refill(self):
        self.assertTrue(self.charge_if_available(60))
        self.assertFalse(self.charge_if_available(60))
        self.advance_time(1)
        self.assertFalse(self.charge_if_available(60))
        self.advance_time(1)
        self.assertTrue(self.charge_if_available(60))
Example #2
0
class TestRateLimiter(ScalyrTestCase):
    def setUp(self):
        self.__test_rate = RateLimiter(100, 10, current_time=0)
        self.__current_time = 0

    def advance_time(self, delta):
        self.__current_time += delta

    def charge_if_available(self, num_bytes):
        return self.__test_rate.charge_if_available(num_bytes, current_time=self.__current_time)

    def test_basic_use(self):
        self.assertTrue(self.charge_if_available(20))
        self.assertTrue(self.charge_if_available(80))
        self.assertFalse(self.charge_if_available(1))

    def test_refill(self):
        self.assertTrue(self.charge_if_available(60))
        self.assertFalse(self.charge_if_available(60))
        self.advance_time(1)
        self.assertFalse(self.charge_if_available(60))
        self.advance_time(1)
        self.assertTrue(self.charge_if_available(60))
class TestRateLimiter(ScalyrTestCase):
    def setUp(self):
        super(TestRateLimiter, self).setUp()
        self.__test_rate = RateLimiter(100, 10, current_time=0)
        self.__current_time = 0
        self.__last_sleep_amount = -1

    def advance_time(self, delta):
        self.__current_time += delta

    def charge_if_available(self, num_bytes):
        return self.__test_rate.charge_if_available(
            num_bytes, current_time=self.__current_time)

    def block_until_charge_succeeds(self, num_bytes):
        return self.__test_rate.block_until_charge_succeeds(
            num_bytes, current_time=self.__current_time)

    def test_basic_use(self):
        self.assertTrue(self.charge_if_available(20))
        self.assertTrue(self.charge_if_available(80))
        self.assertFalse(self.charge_if_available(1))

    def test_custom_bucket_size_and_rate(self):
        self.__test_rate = RateLimiter(10, 1, current_time=0)
        self.assertTrue(self.charge_if_available(10))
        self.assertFalse(self.charge_if_available(10))
        self.advance_time(1)
        self.assertFalse(self.charge_if_available(10))
        self.advance_time(5)
        self.assertFalse(self.charge_if_available(10))

    def test_zero_bucket_fill_rate(self):
        self.__test_rate = RateLimiter(100, 0, current_time=0)
        self.assertTrue(self.charge_if_available(20))
        self.assertTrue(self.charge_if_available(80))
        self.assertFalse(self.charge_if_available(1))
        self.advance_time(1)
        self.assertFalse(self.charge_if_available(20))
        self.advance_time(5)
        self.assertFalse(self.charge_if_available(20))

    def test_refill(self):
        self.assertTrue(self.charge_if_available(60))
        self.assertFalse(self.charge_if_available(60))
        self.advance_time(1)
        self.assertFalse(self.charge_if_available(60))
        self.advance_time(1)
        self.assertTrue(self.charge_if_available(60))

    def fake_sleep(self, seconds):
        self.__last_sleep_amount = seconds
        self.advance_time(seconds)

    def test_basic_use_sleep(self):
        with mock.patch("scalyr_agent.util.time.sleep", self.fake_sleep):
            self.__last_sleep_amount = -1
            self.block_until_charge_succeeds(20)
            self.assertEqual(self.__last_sleep_amount, -1)
            self.block_until_charge_succeeds(80)
            self.assertEqual(self.__last_sleep_amount, -1)
            self.block_until_charge_succeeds(1)
            self.assertEqual(self.__last_sleep_amount, 0.1)

    def test_custom_bucket_size_and_rate_sleep(self):
        with mock.patch("scalyr_agent.util.time.sleep", self.fake_sleep):
            self.__last_sleep_amount = -1
            self.__test_rate = RateLimiter(10, 1, current_time=0)
            self.block_until_charge_succeeds(10)
            self.assertEqual(self.__last_sleep_amount, -1)
            self.block_until_charge_succeeds(10)
            self.assertEqual(self.__last_sleep_amount, 10)
            self.advance_time(15)
            self.block_until_charge_succeeds(20)
            self.assertEqual(self.__last_sleep_amount, 10)

    def test_zero_bucket_fill_rate_sleep(self):
        self.__test_rate = RateLimiter(100, 0, current_time=0)
        self.assertRaises(ValueError, self.block_until_charge_succeeds, 20)

    def test_refill_sleep(self):
        with mock.patch("scalyr_agent.util.time.sleep", self.fake_sleep):
            self.__last_sleep_amount = -1
            self.block_until_charge_succeeds(60)
            self.assertEqual(self.__last_sleep_amount, -1)
            self.block_until_charge_succeeds(60)
            self.assertEqual(self.__last_sleep_amount, 2)
            self.advance_time(1)
            self.block_until_charge_succeeds(60)
            self.assertEqual(self.__last_sleep_amount, 5)

    def test_charge_greater_than_bucket_size_sleep(self):
        with mock.patch("scalyr_agent.util.time.sleep", self.fake_sleep):
            self.__last_sleep_amount = -1
            self.__test_rate = RateLimiter(10, 1, current_time=0)
            self.block_until_charge_succeeds(20)
            self.assertEqual(self.__last_sleep_amount, 10)