|
1 | 1 | """This test module is used to verify integrity of the Ratelimited session.""" |
2 | | -#TODO@bensteUEM: inquiry to CT Team 147987 for configured threshold |
3 | | -# https://github.com/bensteUEM/ChurchToolsAPI/issues/37 |
4 | 2 |
|
5 | 3 | import json |
6 | 4 | import logging |
7 | 5 | import logging.config |
8 | | -import time |
9 | 6 | from pathlib import Path |
10 | 7 |
|
11 | 8 | import pytest |
12 | | -import requests |
13 | 9 |
|
14 | | -from churchtools_api.ratelimitedsession import RateLimitedSession |
| 10 | +from tests.test_churchtools_api_abstract import TestsChurchToolsApiAbstract |
15 | 11 |
|
16 | 12 | logger = logging.getLogger(__name__) |
17 | 13 |
|
|
23 | 19 | log_directory.mkdir(parents=True) |
24 | 20 | logging.config.dictConfig(config=logging_config) |
25 | 21 |
|
26 | | -# Make sure test values are adjusted |
27 | | -# - e.g. 250* google.com with 10 req per 2seconds ~= 50 sec |
28 | | -# - e.g. 250* google.com with no threshold ~= 30 sec |
29 | | -SAMPLE_URL = "https://google.com" |
30 | | -EXPECTED_TIME = 30 |
31 | 22 |
|
| 23 | +class TestsRateLimitedSession(TestsChurchToolsApiAbstract): |
| 24 | + """Test for Rate limits.""" |
32 | 25 |
|
33 | | -@pytest.mark.skip("execute only if ratelimit is decreased") |
34 | | -def test_no_rate_limit() -> None: |
35 | | - """Reference time for 250 requests without throttle using google.com. |
| 26 | + def test_no_rate_limit(self, caplog: pytest.LogCaptureFixture) -> None: |
| 27 | + """Reference time for 250 requests without throttle using google.com.""" |
| 28 | + SAMPLE_SONG_ID = 408 |
36 | 29 |
|
37 | | - Actual time might depend on internet speed! |
| 30 | + with caplog.at_level(logging.INFO, logger="ratelimitedsession"): |
| 31 | + for _i in range(750): |
| 32 | + self.api.get_songs(song_id=SAMPLE_SONG_ID) |
| 33 | + EXPECTED_MESSAGES = [ |
| 34 | + "rate limit reached - waiting 15 sec before repeating request" |
| 35 | + ] |
38 | 36 |
|
39 | | - Only checks GET request for simplification. |
40 | | - """ |
41 | | - session = requests.Session() |
42 | | - |
43 | | - start = time.perf_counter() |
44 | | - for _i in range(250): |
45 | | - session.get(url=SAMPLE_URL) |
46 | | - end = time.perf_counter() |
47 | | - |
48 | | - assert end - start < EXPECTED_TIME |
49 | | - |
50 | | -@pytest.mark.skip("execute only if ratelimit is decreased") |
51 | | -def test_rate_limited() -> None: |
52 | | - """Reference time for 250 requests with throttle using google.com. |
53 | | -
|
54 | | - Only checks GET request for simplification. |
55 | | - """ |
56 | | - session = RateLimitedSession() |
57 | | - |
58 | | - start = time.perf_counter() |
59 | | - for _i in range(250): |
60 | | - session.get(url=SAMPLE_URL) |
61 | | - end = time.perf_counter() |
62 | | - |
63 | | - assert end-start > EXPECTED_TIME |
| 37 | + assert caplog.messages == EXPECTED_MESSAGES |
0 commit comments