55
66import pytest
77
8+ from asyncprawcore .const import NANOSECONDS
89from asyncprawcore .rate_limit import RateLimiter
910
1011from . import UnitTest
@@ -14,7 +15,7 @@ class TestRateLimiter(UnitTest):
1415 @pytest .fixture
1516 def rate_limiter (self ):
1617 rate_limiter = RateLimiter (window_size = 600 )
17- rate_limiter .next_request_timestamp_ns = 100 * RateLimiter . NANOSECONDS
18+ rate_limiter .next_request_timestamp_ns = 100 * NANOSECONDS
1819 return rate_limiter
1920
2021 @staticmethod
@@ -28,7 +29,7 @@ def _headers(remaining, used, reset):
2829 @patch ("time.monotonic_ns" )
2930 @patch ("asyncio.sleep" )
3031 async def test_delay (self , mock_sleep , mock_monotonic_ns , rate_limiter ):
31- mock_monotonic_ns .return_value = 1 * RateLimiter . NANOSECONDS
32+ mock_monotonic_ns .return_value = 1 * NANOSECONDS
3233 await rate_limiter .delay ()
3334 assert mock_monotonic_ns .called
3435 mock_sleep .assert_called_with (99 )
@@ -38,7 +39,7 @@ async def test_delay(self, mock_sleep, mock_monotonic_ns, rate_limiter):
3839 async def test_delay__no_sleep_when_time_in_past (
3940 self , mock_sleep , mock_monotonic_ns , rate_limiter
4041 ):
41- mock_monotonic_ns .return_value = 101 * RateLimiter . NANOSECONDS
42+ mock_monotonic_ns .return_value = 101 * NANOSECONDS
4243 await rate_limiter .delay ()
4344 assert mock_monotonic_ns .called
4445 assert not mock_sleep .called
@@ -54,7 +55,7 @@ async def test_delay__no_sleep_when_time_is_not_set(self, mock_sleep, rate_limit
5455 async def test_delay__no_sleep_when_times_match (
5556 self , mock_sleep , mock_monotonic_ns , rate_limiter
5657 ):
57- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
58+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
5859 await rate_limiter .delay ()
5960 assert mock_monotonic_ns .called
6061 assert not mock_sleep .called
@@ -63,64 +64,64 @@ async def test_delay__no_sleep_when_times_match(
6364 def test_update__compute_delay_with_no_previous_info (
6465 self , mock_monotonic_ns , rate_limiter
6566 ):
66- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
67+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
6768 rate_limiter .update (self ._headers (60 , 100 , 60 ))
6869 assert rate_limiter .remaining == 60
6970 assert rate_limiter .used == 100
70- assert rate_limiter .next_request_timestamp_ns == 100 * RateLimiter . NANOSECONDS
71+ assert rate_limiter .next_request_timestamp_ns == 100 * NANOSECONDS
7172
7273 @patch ("time.monotonic_ns" )
7374 def test_update__compute_delay_with_single_client (
7475 self , mock_monotonic_ns , rate_limiter
7576 ):
7677 rate_limiter .window_size = 150
77- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
78+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
7879 rate_limiter .update (self ._headers (50 , 100 , 60 ))
7980 assert rate_limiter .remaining == 50
8081 assert rate_limiter .used == 100
81- assert rate_limiter .next_request_timestamp_ns == 110 * RateLimiter . NANOSECONDS
82+ assert rate_limiter .next_request_timestamp_ns == 110 * NANOSECONDS
8283
8384 @patch ("time.monotonic_ns" )
8485 def test_update__compute_delay_with_six_clients (
8586 self , mock_monotonic_ns , rate_limiter
8687 ):
8788 rate_limiter .remaining = 66
8889 rate_limiter .window_size = 180
89- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
90+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
9091 rate_limiter .update (self ._headers (60 , 100 , 72 ))
9192 assert rate_limiter .remaining == 60
9293 assert rate_limiter .used == 100
93- assert rate_limiter .next_request_timestamp_ns == 104.5 * RateLimiter . NANOSECONDS
94+ assert rate_limiter .next_request_timestamp_ns == 104.5 * NANOSECONDS
9495
9596 @patch ("time.monotonic_ns" )
9697 def test_update__delay_full_time_with_negative_remaining (
9798 self , mock_monotonic_ns , rate_limiter
9899 ):
99- mock_monotonic_ns .return_value = 37 * RateLimiter . NANOSECONDS
100+ mock_monotonic_ns .return_value = 37 * NANOSECONDS
100101 rate_limiter .update (self ._headers (0 , 100 , 13 ))
101102 assert rate_limiter .remaining == 0
102103 assert rate_limiter .used == 100
103- assert rate_limiter .next_request_timestamp_ns == 50 * RateLimiter . NANOSECONDS
104+ assert rate_limiter .next_request_timestamp_ns == 50 * NANOSECONDS
104105
105106 @patch ("time.monotonic_ns" )
106107 def test_update__delay_full_time_with_zero_remaining (
107108 self , mock_monotonic_ns , rate_limiter
108109 ):
109- mock_monotonic_ns .return_value = 37 * RateLimiter . NANOSECONDS
110+ mock_monotonic_ns .return_value = 37 * NANOSECONDS
110111 rate_limiter .update (self ._headers (0 , 100 , 13 ))
111112 assert rate_limiter .remaining == 0
112113 assert rate_limiter .used == 100
113- assert rate_limiter .next_request_timestamp_ns == 50 * RateLimiter . NANOSECONDS
114+ assert rate_limiter .next_request_timestamp_ns == 50 * NANOSECONDS
114115
115116 @patch ("time.monotonic_ns" )
116117 def test_update__delay_full_time_with_zero_remaining_and_no_sleep_time (
117118 self , mock_monotonic_ns , rate_limiter
118119 ):
119- mock_monotonic_ns .return_value = 37 * RateLimiter . NANOSECONDS
120+ mock_monotonic_ns .return_value = 37 * NANOSECONDS
120121 rate_limiter .update (self ._headers (0 , 100 , 0 ))
121122 assert rate_limiter .remaining == 0
122123 assert rate_limiter .used == 100
123- assert rate_limiter .next_request_timestamp_ns == 37.5 * RateLimiter . NANOSECONDS
124+ assert rate_limiter .next_request_timestamp_ns == 37.5 * NANOSECONDS
124125
125126 def test_update__no_change_without_headers (self , rate_limiter ):
126127 prev = copy (rate_limiter )
0 commit comments