test__exponential_backoff.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright 2022 Google Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import mock
  15. from google.auth import _exponential_backoff
  16. @mock.patch("time.sleep", return_value=None)
  17. def test_exponential_backoff(mock_time):
  18. eb = _exponential_backoff.ExponentialBackoff()
  19. curr_wait = eb._current_wait_in_seconds
  20. iteration_count = 0
  21. for attempt in eb:
  22. backoff_interval = mock_time.call_args[0][0]
  23. jitter = curr_wait * eb._randomization_factor
  24. assert (curr_wait - jitter) <= backoff_interval <= (curr_wait + jitter)
  25. assert attempt == iteration_count + 1
  26. assert eb.backoff_count == iteration_count + 1
  27. assert eb._current_wait_in_seconds == eb._multiplier ** (iteration_count + 1)
  28. curr_wait = eb._current_wait_in_seconds
  29. iteration_count += 1
  30. assert eb.total_attempts == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS
  31. assert eb.backoff_count == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS
  32. assert iteration_count == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS
  33. assert mock_time.call_count == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS