Skip to content

Commit 5bf004c

Browse files
committed
small fix and change prefixes for tests
1 parent 61031e7 commit 5bf004c

12 files changed

+109
-73
lines changed

.github/workflows/tests.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: 3.8
20+
21+
- name: Install Poetry
22+
run: curl -sSL https://install.python-poetry.org | python3 -
23+
24+
- name: Set up Poetry environment
25+
run: poetry install --no-root
26+
27+
- name: Run tests
28+
run: |
29+
export UPSTASH_REDIST_REST_URL="${{secrets.UPSTASH_REDIS_REST_URL}}"
30+
export UPSTASH_REDIST_REST_TOKEN="${{secrets.UPSTASH_REDIS_REST_TOKEN}}"
31+
poetry run pytest

tests/algorithm/limiting/test_fixed_window_limiting.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from tests.client import rate_limit
33
from pytest import mark
44

5-
fixed_window = rate_limit.fixed_window(max_number_of_requests=1, window=5000, unit="ms")
5+
fixed_window = rate_limit.fixed_window(max_number_of_requests=1, window=10000, unit="ms")
66

77
def test_below_max() -> None:
88
assert (fixed_window.limit("fixed_window_1"))["is_allowed"] is True
@@ -12,7 +12,7 @@ def test_below_max() -> None:
1212
def test_above_max() -> None:
1313
fixed_window.limit("fixed_window_2")
1414

15-
sleep(0.5)
15+
sleep(1)
1616

1717
assert (fixed_window.limit("fixed_window_2"))["is_allowed"] is False
1818
assert (fixed_window.limit("fixed_window_2"))["is_allowed"] is False
@@ -24,27 +24,28 @@ def test_after_window() -> None:
2424
fixed_window.limit("fixed_window_3")
2525

2626
# Wait for the reset.
27-
sleep(5)
27+
sleep(10)
2828

2929
assert (fixed_window.limit("fixed_window_3"))["is_allowed"] is True
3030

3131

32-
3332
def test_with_non_ms_unit() -> None:
3433
fixed_window_with_seconds = rate_limit.fixed_window(
35-
max_number_of_requests=1, window=5, unit="s"
34+
max_number_of_requests=1, window=10, unit="s"
3635
)
3736

3837
assert (fixed_window_with_seconds.limit("fixed_window_4"))[
3938
"is_allowed"
4039
] is True
4140

41+
sleep(1)
42+
4243
# Exhaust the request limit.
4344
assert (fixed_window_with_seconds.limit("fixed_window_4"))[
4445
"is_allowed"
4546
] is False
4647

47-
sleep(5)
48+
sleep(10)
4849

4950
assert (fixed_window_with_seconds.limit("fixed_window_4"))[
5051
"is_allowed"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import pytest
12
from tests.client import rate_limit
23
from pytest import mark
3-
from time import time_ns
4+
from time import sleep, time_ns
45
from math import floor
56

67
fixed_window = rate_limit.fixed_window(max_number_of_requests=1, window=3000, unit="ms")
@@ -11,7 +12,6 @@ def test_before_the_first_request() -> None:
1112
def test_after_the_first_request() -> None:
1213
fixed_window.limit("fixed_window_reset_2")
1314

14-
assert (
15-
fixed_window.reset("fixed_window_reset_2")
16-
== floor((time_ns() / 1000000) / 3000) * 3000 + 3000
17-
)
15+
sleep(0.3)
16+
17+
assert pytest.approx(floor((time_ns() / 1000000) / 3000) * 3000 + 3000, 0.1) == fixed_window.reset("fixed_window_reset_2")

tests/asyncio/algorithm/limiting/test_async_fixed_window_limiting.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,54 @@
22
from tests.asyncio.client import rate_limit
33
from pytest import mark
44

5-
fixed_window = rate_limit.fixed_window(max_number_of_requests=1, window=5000, unit="ms")
5+
fixed_window = rate_limit.fixed_window(max_number_of_requests=1, window=10000, unit="ms")
66

77

88
@mark.asyncio
99
async def test_below_max() -> None:
10-
assert (await fixed_window.limit("fixed_window_1"))["is_allowed"] is True
10+
assert (await fixed_window.limit("async_fixed_window_1"))["is_allowed"] is True
1111

1212

1313
@mark.asyncio
1414
async def test_above_max() -> None:
15-
await fixed_window.limit("fixed_window_2")
15+
await fixed_window.limit("async_fixed_window_2")
1616

17-
await sleep(0.5)
17+
await sleep(1)
1818

19-
assert (await fixed_window.limit("fixed_window_2"))["is_allowed"] is False
20-
assert (await fixed_window.limit("fixed_window_2"))["is_allowed"] is False
19+
assert (await fixed_window.limit("async_fixed_window_2"))["is_allowed"] is False
20+
assert (await fixed_window.limit("async_fixed_window_2"))["is_allowed"] is False
2121

2222

2323
@mark.asyncio
2424
async def test_after_window() -> None:
2525
# Exhaust the request limit.
26-
await fixed_window.limit("fixed_window_3")
26+
await fixed_window.limit("async_fixed_window_3")
2727

2828
# Wait for the reset.
29-
await sleep(5)
29+
await sleep(10)
3030

31-
assert (await fixed_window.limit("fixed_window_3"))["is_allowed"] is True
31+
assert (await fixed_window.limit("async_fixed_window_3"))["is_allowed"] is True
3232

3333

3434
@mark.asyncio
3535
async def test_with_non_ms_unit() -> None:
3636
fixed_window_with_seconds = rate_limit.fixed_window(
37-
max_number_of_requests=1, window=5, unit="s"
37+
max_number_of_requests=1, window=10, unit="s"
3838
)
3939

40-
assert (await fixed_window_with_seconds.limit("fixed_window_4"))[
40+
assert (await fixed_window_with_seconds.limit("async_fixed_window_4"))[
4141
"is_allowed"
4242
] is True
4343

44+
await sleep(1)
45+
4446
# Exhaust the request limit.
45-
assert (await fixed_window_with_seconds.limit("fixed_window_4"))[
47+
assert (await fixed_window_with_seconds.limit("async_fixed_window_4"))[
4648
"is_allowed"
4749
] is False
4850

49-
await sleep(5)
51+
await sleep(10)
5052

51-
assert (await fixed_window_with_seconds.limit("fixed_window_4"))[
53+
assert (await fixed_window_with_seconds.limit("async_fixed_window_4"))[
5254
"is_allowed"
5355
] is True

tests/asyncio/algorithm/limiting/test_async_token_bucket_limiting.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010

1111
@mark.asyncio
1212
async def test_below_max() -> None:
13-
assert (await token_bucket.limit("token_bucket_1"))["is_allowed"] is True
13+
assert (await token_bucket.limit("async_token_bucket_1"))["is_allowed"] is True
1414

1515

1616
@mark.asyncio
1717
async def test_above_max() -> None:
18-
await token_bucket.limit("token_bucket_2")
18+
await token_bucket.limit("async_token_bucket_2")
1919

20-
assert (await token_bucket.limit("token_bucket_2"))["is_allowed"] is False
21-
assert (await token_bucket.limit("token_bucket_2"))["is_allowed"] is False
20+
assert (await token_bucket.limit("async_token_bucket_2"))["is_allowed"] is False
21+
assert (await token_bucket.limit("async_token_bucket_2"))["is_allowed"] is False
2222

2323

2424
@mark.asyncio
2525
async def test_after_window() -> None:
2626
# Exhaust the request limit.
27-
await token_bucket.limit("token_bucket_3")
27+
await token_bucket.limit("async_token_bucket_3")
2828

2929
# Wait for the refill.
3030
sleep(3)
3131

32-
assert (await token_bucket.limit("token_bucket_3"))["is_allowed"] is True
32+
assert (await token_bucket.limit("async_token_bucket_3"))["is_allowed"] is True
3333

3434

3535
@mark.asyncio
@@ -39,18 +39,18 @@ async def test_with_non_ms_unit() -> None:
3939
)
4040

4141
# Exhaust the request limit.
42-
assert (await token_bucket_with_seconds.limit("token_bucket_4"))[
42+
assert (await token_bucket_with_seconds.limit("async_token_bucket_4"))[
4343
"is_allowed"
4444
] is True
4545

46-
assert (await token_bucket_with_seconds.limit("token_bucket_4"))[
46+
assert (await token_bucket_with_seconds.limit("async_token_bucket_4"))[
4747
"is_allowed"
4848
] is False
4949

5050
# Wait for the refill.
5151
sleep(3)
5252

53-
assert (await token_bucket_with_seconds.limit("token_bucket_4"))[
53+
assert (await token_bucket_with_seconds.limit("async_token_bucket_4"))[
5454
"is_allowed"
5555
] is True
5656

@@ -64,60 +64,60 @@ async def test_with_non_ms_unit() -> None:
6464
@mark.asyncio
6565
async def test_burst() -> None:
6666
# Exhaust the request limit.
67-
await burst_token_bucket.limit("burst_token_bucket_1")
68-
await burst_token_bucket.limit("burst_token_bucket_1")
67+
await burst_token_bucket.limit("async_burst_token_bucket_1")
68+
await burst_token_bucket.limit("async_burst_token_bucket_1")
6969

7070
# Wait for the refill.
7171
sleep(2)
7272

73-
assert (await burst_token_bucket.limit("burst_token_bucket_1"))[
73+
assert (await burst_token_bucket.limit("async_burst_token_bucket_1"))[
7474
"is_allowed"
7575
] is True
76-
assert (await burst_token_bucket.limit("burst_token_bucket_1"))[
76+
assert (await burst_token_bucket.limit("async_burst_token_bucket_1"))[
7777
"is_allowed"
7878
] is False
79-
assert (await burst_token_bucket.limit("burst_token_bucket_1"))[
79+
assert (await burst_token_bucket.limit("async_burst_token_bucket_1"))[
8080
"is_allowed"
8181
] is False
8282

8383

8484
@mark.asyncio
8585
async def test_with_positive_number_of_tokens_before_the_refill() -> None:
86-
await burst_token_bucket.limit("burst_token_bucket_2")
86+
await burst_token_bucket.limit("async_burst_token_bucket_2")
8787

8888
"""
8989
At this point the bucket should've had 1 token.
9090
Since the refill adds another one, the next two requests should pass.
9191
"""
9292
sleep(2)
9393

94-
assert (await burst_token_bucket.limit("burst_token_bucket_2"))[
94+
assert (await burst_token_bucket.limit("async_burst_token_bucket_2"))[
9595
"is_allowed"
9696
] is True
97-
assert (await burst_token_bucket.limit("burst_token_bucket_2"))[
97+
assert (await burst_token_bucket.limit("async_burst_token_bucket_2"))[
9898
"is_allowed"
9999
] is True
100-
assert (await burst_token_bucket.limit("burst_token_bucket_2"))[
100+
assert (await burst_token_bucket.limit("async_burst_token_bucket_2"))[
101101
"is_allowed"
102102
] is False
103103

104104

105105
@mark.asyncio
106106
async def test_multiple_refills() -> None:
107107
# Exhaust the request limit.
108-
await burst_token_bucket.limit("burst_token_bucket_3")
109-
await burst_token_bucket.limit("burst_token_bucket_3")
108+
await burst_token_bucket.limit("async_burst_token_bucket_3")
109+
await burst_token_bucket.limit("async_burst_token_bucket_3")
110110

111111
# Wait for 2 refills.
112112
sleep(4)
113113

114-
assert (await burst_token_bucket.limit("burst_token_bucket_3"))[
114+
assert (await burst_token_bucket.limit("async_burst_token_bucket_3"))[
115115
"is_allowed"
116116
] is True
117-
assert (await burst_token_bucket.limit("burst_token_bucket_3"))[
117+
assert (await burst_token_bucket.limit("async_burst_token_bucket_3"))[
118118
"is_allowed"
119119
] is True
120-
assert (await burst_token_bucket.limit("burst_token_bucket_3"))[
120+
assert (await burst_token_bucket.limit("async_burst_token_bucket_3"))[
121121
"is_allowed"
122122
] is False
123123

tests/asyncio/algorithm/remaining/test_async_fixed_window_remaining.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
@mark.asyncio
88
async def test_before_the_first_request() -> None:
9-
assert await fixed_window.remaining("fixed_window_remaining_1") == 1
9+
assert await fixed_window.remaining("async_fixed_window_remaining_1") == 1
1010

1111

1212
@mark.asyncio
1313
async def test_after_the_first_request() -> None:
14-
await fixed_window.limit("fixed_window_remaining_2")
14+
await fixed_window.limit("async_fixed_window_remaining_2")
1515

16-
assert await fixed_window.remaining("fixed_window_remaining_2") == 0
16+
assert await fixed_window.remaining("async_fixed_window_remaining_2") == 0

tests/asyncio/algorithm/remaining/test_async_token_bucket_remaining.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
@mark.asyncio
1111
async def test_before_the_first_request() -> None:
12-
assert await token_bucket.remaining("token_bucket_remaining_1") == 1
12+
assert await token_bucket.remaining("async_token_bucket_remaining_1") == 1
1313

1414

1515
@mark.asyncio
1616
async def test_after_the_first_request() -> None:
17-
await token_bucket.limit("token_bucket_remaining_2")
17+
await token_bucket.limit("async_token_bucket_remaining_2")
1818

19-
assert await token_bucket.remaining("token_bucket_remaining_2") == 0
19+
assert await token_bucket.remaining("async_token_bucket_remaining_2") == 0
2020

2121

2222
# Use a client that has different maximum number of tokens and refill rate.
@@ -28,23 +28,23 @@ async def test_after_the_first_request() -> None:
2828
@mark.asyncio
2929
async def test_after_burst() -> None:
3030
# Exhaust the request limit.
31-
await burst_token_bucket.limit("burst_token_bucket_remaining_1")
32-
await burst_token_bucket.limit("burst_token_bucket_remaining_1")
31+
await burst_token_bucket.limit("async_burst_token_bucket_remaining_1")
32+
await burst_token_bucket.limit("async_burst_token_bucket_remaining_1")
3333

3434
# Wait for the refill.
3535
sleep(2)
3636

37-
assert await burst_token_bucket.remaining("burst_token_bucket_remaining_1") == 1
37+
assert await burst_token_bucket.remaining("async_burst_token_bucket_remaining_1") == 1
3838

3939

4040
@mark.asyncio
4141
async def test_after_burst_with_positive_number_of_tokens_before_the_refill() -> None:
42-
await burst_token_bucket.limit("burst_token_bucket_remaining_2")
42+
await burst_token_bucket.limit("async_burst_token_bucket_remaining_2")
4343

4444
"""
4545
At this point the bucket should've had 1 token.
4646
Since the refill adds another one, the identifier should have 2 requests left.
4747
"""
4848
sleep(2)
4949

50-
assert await burst_token_bucket.remaining("burst_token_bucket_remaining_2") == 2
50+
assert await burst_token_bucket.remaining("async_burst_token_bucket_remaining_2") == 2

tests/asyncio/algorithm/reset/test_async_fixed_window_reset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
@mark.asyncio
1010
async def test_before_the_first_request() -> None:
11-
assert await fixed_window.reset("fixed_window_reset_1") == -1
11+
assert await fixed_window.reset("async_fixed_window_reset_1") == -1
1212

1313

1414
@mark.asyncio
1515
async def test_after_the_first_request() -> None:
16-
await fixed_window.limit("fixed_window_reset_2")
16+
await fixed_window.limit("async_fixed_window_reset_2")
1717

1818
assert (
19-
await fixed_window.reset("fixed_window_reset_2")
19+
await fixed_window.reset("async_fixed_window_reset_2")
2020
== floor((time_ns() / 1000000) / 3000) * 3000 + 3000
2121
)

0 commit comments

Comments
 (0)