Skip to content

Commit b95a85d

Browse files
committed
Initial rate-limit test
1 parent 7965224 commit b95a85d

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ toxtest : local/environment.sh tox.ini
3636

3737
.PHONY : pytest
3838
pytest : local/environment.sh
39-
source local/environment.sh
40-
pytest
39+
source local/environment.sh && pytest -m "not ratelimit"
40+
41+
.PHONY : pytest-rate-limit
42+
pytest-rate-limit : local/environment.sh
43+
source local/environment.sh && pytest -m "ratelimit"
4144

4245
.PHONY : lint
4346
lint :

tests/test_restsession.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
"""ciscosparkapi/restsession.py Fixtures & Tests"""
3+
4+
5+
import logging
6+
7+
import pytest
8+
9+
from ciscosparkapi.exceptions import SPARK_RESPONSE_CODES
10+
11+
# Constants
12+
RATE_LIMIT_RESPONSE_CODE = 429
13+
RATE_LIMIT_RESPONSE_TEXT = SPARK_RESPONSE_CODES[RATE_LIMIT_RESPONSE_CODE]
14+
RATE_LIMIT_LOG_MESSAGE = ("Received a [%s] rate limit response. "
15+
"Attempting to retry.")
16+
17+
18+
# Helper Classes
19+
class RateLimitDetector(logging.Handler):
20+
"""Detects occurrences of rate limiting."""
21+
22+
def __init__(self):
23+
super(RateLimitDetector, self).__init__()
24+
25+
self.rate_limit_detected = False
26+
27+
def emit(self, record):
28+
"""Check record to see if it is a rate-limit message."""
29+
assert isinstance(record, logging.LogRecord)
30+
31+
if (RATE_LIMIT_RESPONSE_CODE in record.args
32+
and record.msg == RATE_LIMIT_LOG_MESSAGE):
33+
34+
self.rate_limit_detected = True
35+
36+
37+
# CiscoSparkAPI Tests
38+
class TestRestSession:
39+
"""Test edge cases of core RestSession functionality."""
40+
41+
@pytest.mark.ratelimit
42+
def test_rate_limit_support(self, api, rooms_list, add_rooms):
43+
# Add log handler
44+
root_logger = logging.getLogger()
45+
rate_limit_detector = RateLimitDetector()
46+
root_logger.addHandler(rate_limit_detector)
47+
48+
try:
49+
# Try and trigger a rate-limit
50+
rooms = api.rooms.list(max=1)
51+
while not rate_limit_detector.rate_limit_detected:
52+
for room in rooms:
53+
# Do nothing
54+
pass
55+
56+
finally:
57+
# Remove the log handler
58+
root_logger.removeHandler(rate_limit_detector)
59+
60+
assert rate_limit_detector.rate_limit_detected == True

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ envlist = py27, py36
33

44
[testenv]
55
deps = pytest
6-
commands = py.test
6+
commands = py.test -m "not ratelimit"
77
passenv = SPARK_ACCESS_TOKEN

0 commit comments

Comments
 (0)