File: test_ratelimitqueue.py

package info (click to toggle)
python-ratelimitqueue 0.2.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: python: 463; makefile: 15
file content (266 lines) | stat: -rw-r--r-- 6,295 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Tests for ratelimitqueue/ratelimitqueue.py"""
import pytest
from unittest import mock
from .utils import almost

from ratelimitqueue.ratelimitqueue import RateLimitQueue, RateLimitException
from ratelimitqueue.ratelimitqueue import RateLimitLifoQueue, RateLimitGetMixin
from ratelimitqueue.ratelimitqueue import RateLimitPriorityQueue

from queue import Empty

import time
import random

# take randomness out of fuzzing
random.uniform = mock.Mock(side_effect=lambda a, b: a + (b - a) / 2)


class GetMixinTester:
    # tests of __init__()
    def test_no_calls(self):
        rlq = self.QueueClass()
        del rlq.calls
        rlq.put(1)
        with pytest.raises(AttributeError):
            rlq.get()

    def test_no_per(self):
        rlq = self.QueueClass()
        del rlq.per
        rlq.put(1)
        with pytest.raises(AttributeError):
            rlq.get()

    def test_no_fuzz(self):
        rlq = self.QueueClass()
        del rlq.fuzz
        rlq.put(1)
        with pytest.raises(AttributeError):
            rlq.get()

    def test_no__call_log(self):
        rlq = self.QueueClass()
        del rlq._call_log
        rlq.put(1)
        with pytest.raises(AttributeError):
            rlq.get()

    def test_no_get(self):
        class DummyParent:
            fuzz = 0
            per = 0
            calls = 0
            _call_log = 0

        class DummyChild(RateLimitGetMixin, DummyParent):
            pass

        dc = DummyChild()
        with pytest.raises(AttributeError):
            dc.get()

    def test_maxsize(self):
        rlq = self.QueueClass(0)
        assert rlq.maxsize == 0

        rlq = self.QueueClass(3)
        assert rlq.maxsize == 3

    def test_calls_not_less_than_1(self):
        with pytest.raises(ValueError):
            rlq = self.QueueClass(1, 0, 10)

        with pytest.raises(ValueError):
            rlq = self.QueueClass(1, -1, 10)

    # tests of get()

    def test_timeout_not_less_than_0(self):
        rlq = self.QueueClass()
        rlq.put(1)
        with pytest.raises(ValueError):
            rlq.get(timeout=-1)

    def test_item_in_queue(self):
        rlq = self.QueueClass()
        rlq.put(1)
        assert rlq.get() == 1

    def test_first_put_fast(self):
        rlq = self.QueueClass()
        start = time.time()
        rlq.put(1)
        rlq.get()

        assert almost(0, time.time() - start)

    def test_default_rate_limit(self):
        rlq = self.QueueClass()
        start = time.time()

        rlq.put(1)
        rlq.put(1)
        rlq.get()
        rlq.get()

        assert almost(1, time.time() - start)

    def test_rate_limit_calls(self):
        rlq = self.QueueClass(calls=2)
        start = time.time()

        rlq.put(1)
        rlq.put(1)
        rlq.put(1)
        rlq.put(1)

        rlq.get()
        rlq.get()
        rlq.get()
        assert almost(1, time.time() - start)

        rlq.get()
        assert almost(1, time.time() - start)

    def test_rate_limit_per(self):
        rlq = self.QueueClass(per=0.5)
        start = time.time()

        rlq.put(1)
        rlq.put(1)
        rlq.put(1)

        rlq.get()
        rlq.get()
        rlq.get()

        assert almost(1, time.time() - start)

    def test_rate_limit_calls_per(self):
        rlq = self.QueueClass(calls=2, per=0.5)
        start = time.time()

        rlq.put(1)
        rlq.put(1)
        rlq.put(1)
        rlq.put(1)

        rlq.get()
        rlq.get()
        rlq.get()
        assert almost(0.5, time.time() - start)

        rlq.get()
        assert almost(0.5, time.time() - start)

    def test_not_block_raises_rate_limit(self):
        rlq = self.QueueClass(calls=1, per=3)
        rlq.put(1)
        rlq.put(1)

        rlq.get()

        with pytest.raises(RateLimitException):
            rlq.get(block=False)

    def test_not_block_raises_empty(self):
        rlq = self.QueueClass(calls=1, per=0)

        with pytest.raises(Empty):
            rlq.get(block=False)

    def test_timeout_on_rate_limit_raises_rate_limit(self):
        rlq = self.QueueClass(per=10)
        rlq.put(1)
        rlq.put(1)

        rlq.get()
        with pytest.raises(RateLimitException):
            rlq.get(timeout=1)

    def test_timeout_on_queue_size_raises_empty(self):
        rlq = self.QueueClass(maxsize=1, per=0)

        with pytest.raises(Empty):
            rlq.get(timeout=0.001)

    def test_timeout_on_queue_size_timing(self):
        rlq = self.QueueClass(maxsize=1, per=0)

        with pytest.raises(Empty):
            start = time.time()
            rlq.get(timeout=0.5)

        assert almost(0.5, time.time() - start)

    def test_no_fuzz_when_at_rate_limit(self):
        rlq = self.QueueClass(per=0.5)
        rlq.put(1)
        rlq.get()

        rlq.fuzz = 1000

        start = time.time()
        rlq.put(1)
        rlq.get()

        assert almost(0.5, time.time() - start)

    def test_fuzz(self):
        rlq = self.QueueClass(per=0.5, fuzz=1)
        start = time.time()
        rlq.put(1)
        rlq.get()

        end = time.time()

        assert almost(0.5, end - start)

    def test_fuzz_less_than_timeout(self):
        rlq = self.QueueClass(fuzz=10000)
        start = time.time()
        rlq.put(1)
        rlq.get(timeout=0.5)
        end = time.time()
        elapsed = end - start
        assert almost(0.5, elapsed)


class TestRateLimitQueue(GetMixinTester):
    QueueClass = RateLimitQueue

    def test_fifo(self):
        rlq = self.QueueClass(per=0)
        rlq.put(1)
        rlq.put(2)
        assert rlq.get() == 1
        assert rlq.get() == 2


class TestRateLimitLifoQueue(GetMixinTester):
    QueueClass = RateLimitLifoQueue

    def test_lifo(self):
        rlq = self.QueueClass(per=0)
        rlq.put(1)
        rlq.put(2)
        assert rlq.get() == 2
        assert rlq.get() == 1


class TestRateLimitPriorityQueue(GetMixinTester):
    QueueClass = RateLimitPriorityQueue

    def test_priority(self):
        rlq = self.QueueClass(per=0)

        rlq.put((4, "fourth"))
        rlq.put((2, "second"))
        rlq.put((1, "first"))
        rlq.put((3, "third"))

        assert rlq.get() == (1, "first")
        assert rlq.get() == (2, "second")
        assert rlq.get() == (3, "third")
        assert rlq.get() == (4, "fourth")