-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SparkRateLimitWarning and use it for rate-limit test
Add a SparkRateLimitWarning class to warn users when a rate-limit event has been detected, and use this new warning (in place of the logging detection) to detect when a rate-limit event has occured in the rate-limit test.
- Loading branch information
Showing
4 changed files
with
53 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,31 +3,30 @@ | |
|
||
|
||
import logging | ||
import warnings | ||
|
||
import pytest | ||
|
||
import ciscosparkapi | ||
|
||
|
||
__author__ = "Chris Lunsford" | ||
__author_email__ = "[email protected]" | ||
__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates." | ||
__license__ = "MIT" | ||
|
||
|
||
# Helper Classes | ||
class RateLimitDetector(logging.Handler): | ||
"""Detects occurrences of rate limiting.""" | ||
|
||
def __init__(self): | ||
super(RateLimitDetector, self).__init__() | ||
|
||
self.rate_limit_detected = False | ||
logging.captureWarnings(True) | ||
|
||
def emit(self, record): | ||
"""Check record to see if it is a rate-limit message.""" | ||
assert isinstance(record, logging.LogRecord) | ||
|
||
if "Received rate-limit message" in record.msg: | ||
self.rate_limit_detected = True | ||
# Helper Functions | ||
def rate_limit_detected(w): | ||
"""Check to see if a rate-limit warning is in the warnings list.""" | ||
while w: | ||
if issubclass(w.pop().category, ciscosparkapi.SparkRateLimitWarning): | ||
return True | ||
break | ||
return False | ||
|
||
|
||
# CiscoSparkAPI Tests | ||
|
@@ -36,35 +35,16 @@ class TestRestSession: | |
|
||
@pytest.mark.ratelimit | ||
def test_rate_limit_retry(self, api, rooms_list, add_rooms): | ||
logger = logging.getLogger(__name__) | ||
|
||
# Save state and initialize test setup | ||
original_wait_on_rate_limit = api._session.wait_on_rate_limit | ||
api._session.wait_on_rate_limit = True | ||
|
||
# Add log handler | ||
root_logger = logging.getLogger() | ||
rate_limit_detector = RateLimitDetector() | ||
root_logger.addHandler(rate_limit_detector) | ||
logger = logging.getLogger(__name__) | ||
logger.info("Starting Rate Limit Testing") | ||
|
||
try: | ||
# Try and trigger a rate-limit | ||
rooms = api.rooms.list(max=1) | ||
request_count = 0 | ||
while not rate_limit_detector.rate_limit_detected: | ||
for room in rooms: | ||
request_count += 1 | ||
if rate_limit_detector.rate_limit_detected: | ||
break | ||
|
||
finally: | ||
logger.info("Rate-limit reached with approximately %s requests.", | ||
request_count) | ||
# Remove the log handler and restore the pre-test state | ||
root_logger.removeHandler(rate_limit_detector) | ||
api._session.wait_on_rate_limit = original_wait_on_rate_limit | ||
with warnings.catch_warnings(record=True) as w: | ||
rooms = api.rooms.list() | ||
while True: | ||
# Try and trigger a rate-limit | ||
list(rooms) | ||
if rate_limit_detected(w): | ||
break | ||
|
||
# Assert test condition | ||
assert rate_limit_detector.rate_limit_detected == True | ||
api._session.wait_on_rate_limit = original_wait_on_rate_limit |