def test_schedule_consumption_for_multiple_requests(self): token = RequestToken() consume_time = 5 actual_wait_time = self.scheduler.schedule_consumption( 1, token, consume_time) self.assertEqual(consume_time, actual_wait_time) other_consume_time = 3 other_token = RequestToken() next_wait_time = self.scheduler.schedule_consumption( 1, other_token, other_consume_time) # This wait time should be the previous time plus its desired # wait time self.assertEqual(next_wait_time, consume_time + other_consume_time)
def test_consume_with_scheduled_retry(self): amt = 1 self.set_projected_rate(self.max_rate + 1) self.scheduler.is_scheduled.return_value = True request_token = RequestToken() self.assertEqual(self.leaky_bucket.consume(amt, request_token), amt) # Nothing new should have been scheduled but the request token # should have been processed. self.assert_nothing_scheduled() self.assert_processed_request_token(request_token)
def test_process_scheduled_consumption(self): token = RequestToken() consume_time = 5 self.scheduler.schedule_consumption(1, token, consume_time) self.scheduler.process_scheduled_consumption(token) self.assertFalse(self.scheduler.is_scheduled(token)) different_time = 7 # The previous consume time should have no affect on the next wait tim # as it has been completed. self.assertEqual( self.scheduler.schedule_consumption(1, token, different_time), different_time)
def test_consume_over_max_rate(self): amt = 1 retry_time = 2.0 self.set_projected_rate(self.max_rate + 1) self.set_retry_time(retry_time) request_token = RequestToken() try: self.leaky_bucket.consume(amt, request_token) self.fail('A RequestExceededException should have been thrown') except RequestExceededException as e: self.assertEqual(e.requested_amt, amt) self.assertEqual(e.retry_time, retry_time) self.assert_was_scheduled(amt, request_token)
def test_is_not_scheduled(self): self.assertFalse(self.scheduler.is_scheduled(RequestToken()))
def test_is_scheduled(self): token = RequestToken() consume_time = 5 self.scheduler.schedule_consumption(1, token, consume_time) self.assertTrue(self.scheduler.is_scheduled(token))
def test_schedule_consumption(self): token = RequestToken() consume_time = 5 actual_wait_time = self.scheduler.schedule_consumption( 1, token, consume_time) self.assertEqual(consume_time, actual_wait_time)
def test_consume_at_max_rate(self): amt = 1 self.set_projected_rate(self.max_rate) self.assertEqual(self.leaky_bucket.consume(amt, RequestToken()), amt) self.assert_recorded_consumed_amt(amt) self.assert_nothing_scheduled()