5
5
6
6
import pytest
7
7
8
+ from prawcore .const import NANOSECONDS
8
9
from prawcore .rate_limit import RateLimiter
9
10
10
11
from . import UnitTest
@@ -14,7 +15,7 @@ class TestRateLimiter(UnitTest):
14
15
@pytest .fixture
15
16
def rate_limiter (self ):
16
17
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
18
19
return rate_limiter
19
20
20
21
@staticmethod
@@ -28,7 +29,7 @@ def _headers(remaining, used, reset):
28
29
@patch ("time.monotonic_ns" )
29
30
@patch ("time.sleep" )
30
31
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
32
33
rate_limiter .delay ()
33
34
assert mock_monotonic_ns .called
34
35
mock_sleep .assert_called_with (99 )
@@ -38,7 +39,7 @@ def test_delay(self, mock_sleep, mock_monotonic_ns, rate_limiter):
38
39
def test_delay__no_sleep_when_time_in_past (
39
40
self , mock_sleep , mock_monotonic_ns , rate_limiter
40
41
):
41
- mock_monotonic_ns .return_value = 101 * RateLimiter . NANOSECONDS
42
+ mock_monotonic_ns .return_value = 101 * NANOSECONDS
42
43
rate_limiter .delay ()
43
44
assert mock_monotonic_ns .called
44
45
assert not mock_sleep .called
@@ -54,7 +55,7 @@ def test_delay__no_sleep_when_time_is_not_set(self, mock_sleep, rate_limiter):
54
55
def test_delay__no_sleep_when_times_match (
55
56
self , mock_sleep , mock_monotonic_ns , rate_limiter
56
57
):
57
- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
58
+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
58
59
rate_limiter .delay ()
59
60
assert mock_monotonic_ns .called
60
61
assert not mock_sleep .called
@@ -63,64 +64,64 @@ def test_delay__no_sleep_when_times_match(
63
64
def test_update__compute_delay_with_no_previous_info (
64
65
self , mock_monotonic_ns , rate_limiter
65
66
):
66
- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
67
+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
67
68
rate_limiter .update (self ._headers (60 , 100 , 60 ))
68
69
assert rate_limiter .remaining == 60
69
70
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
71
72
72
73
@patch ("time.monotonic_ns" )
73
74
def test_update__compute_delay_with_single_client (
74
75
self , mock_monotonic_ns , rate_limiter
75
76
):
76
77
rate_limiter .window_size = 150
77
- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
78
+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
78
79
rate_limiter .update (self ._headers (50 , 100 , 60 ))
79
80
assert rate_limiter .remaining == 50
80
81
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
82
83
83
84
@patch ("time.monotonic_ns" )
84
85
def test_update__compute_delay_with_six_clients (
85
86
self , mock_monotonic_ns , rate_limiter
86
87
):
87
88
rate_limiter .remaining = 66
88
89
rate_limiter .window_size = 180
89
- mock_monotonic_ns .return_value = 100 * RateLimiter . NANOSECONDS
90
+ mock_monotonic_ns .return_value = 100 * NANOSECONDS
90
91
rate_limiter .update (self ._headers (60 , 100 , 72 ))
91
92
assert rate_limiter .remaining == 60
92
93
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
94
95
95
96
@patch ("time.monotonic_ns" )
96
97
def test_update__delay_full_time_with_negative_remaining (
97
98
self , mock_monotonic_ns , rate_limiter
98
99
):
99
- mock_monotonic_ns .return_value = 37 * RateLimiter . NANOSECONDS
100
+ mock_monotonic_ns .return_value = 37 * NANOSECONDS
100
101
rate_limiter .update (self ._headers (0 , 100 , 13 ))
101
102
assert rate_limiter .remaining == 0
102
103
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
104
105
105
106
@patch ("time.monotonic_ns" )
106
107
def test_update__delay_full_time_with_zero_remaining (
107
108
self , mock_monotonic_ns , rate_limiter
108
109
):
109
- mock_monotonic_ns .return_value = 37 * RateLimiter . NANOSECONDS
110
+ mock_monotonic_ns .return_value = 37 * NANOSECONDS
110
111
rate_limiter .update (self ._headers (0 , 100 , 13 ))
111
112
assert rate_limiter .remaining == 0
112
113
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
114
115
115
116
@patch ("time.monotonic_ns" )
116
117
def test_update__delay_full_time_with_zero_remaining_and_no_sleep_time (
117
118
self , mock_monotonic_ns , rate_limiter
118
119
):
119
- mock_monotonic_ns .return_value = 37 * RateLimiter . NANOSECONDS
120
+ mock_monotonic_ns .return_value = 37 * NANOSECONDS
120
121
rate_limiter .update (self ._headers (0 , 100 , 0 ))
121
122
assert rate_limiter .remaining == 0
122
123
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
124
125
125
126
def test_update__no_change_without_headers (self , rate_limiter ):
126
127
prev = copy (rate_limiter )
0 commit comments