Skip to content

Commit

Permalink
Test refactoring (#166)
Browse files Browse the repository at this point in the history
* update docker-compose config

* update action config

* clean up

* disable sqliteClock
  • Loading branch information
vutran1710 committed Mar 18, 2024
1 parent 9919d5a commit 0e4895f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/poetry-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
env:
ALLOW_EMPTY_PASSWORD: yes
POSTGRESQL_PASSWORD: postgres
POSTGRESQL_MAX_CONNECTIONS: 1000
ports:
- 5432:5432
strategy:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ services:
- "5432:5432"
environment:
- POSTGRESQL_PASSWORD=postgres
- POSTGRESQL_MAX_CONNECTIONS=1000
networks:
- pyrate-bay

Expand Down
7 changes: 1 addition & 6 deletions pyrate_limiter/abstracts/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def __init__(self, leak_interval: int):
self.async_buckets = defaultdict()
self.clocks = defaultdict()
self.leak_interval = leak_interval
self._task = None
super().__init__()

def register(self, bucket: AbstractBucket, clock: AbstractClock):
Expand Down Expand Up @@ -172,7 +171,7 @@ async def _leak(self, sync=True) -> None:
def leak_async(self):
if self.async_buckets and not self.is_async_leak_started:
self.is_async_leak_started = True
self._task = asyncio.create_task(self._leak(sync=False))
asyncio.create_task(self._leak(sync=False))

def run(self) -> None:
assert self.sync_buckets
Expand All @@ -182,10 +181,6 @@ def start(self) -> None:
if self.sync_buckets and not self.is_alive():
super().start()

def cancel(self) -> None:
if self._task:
self._task.cancel()


class BucketFactory(ABC):
"""Asbtract BucketFactory class.
Expand Down
3 changes: 0 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from pyrate_limiter import Rate
from pyrate_limiter import RedisBucket
from pyrate_limiter import SQLiteBucket
from pyrate_limiter import SQLiteClock
from pyrate_limiter import SQLiteQueries as Queries
from pyrate_limiter import TimeAsyncClock
from pyrate_limiter import TimeClock
Expand All @@ -36,14 +35,12 @@
clocks = [
MonotonicClock(),
TimeClock(),
SQLiteClock.default(),
TimeAsyncClock(),
]

ClockSet = Union[
MonotonicClock,
TimeClock,
SQLiteClock,
TimeAsyncClock,
]

Expand Down
3 changes: 0 additions & 3 deletions tests/test_bucket_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ async def test_factory_01(clock, create_bucket):
bucket = factory.get(item)

assert isinstance(bucket, AbstractBucket)
if factory._leaker:
factory._leaker.cancel()


@pytest.mark.asyncio
Expand Down Expand Up @@ -84,4 +82,3 @@ async def test_factory_leak(clock, create_bucket):
assert await async_count(factory.buckets[item_name]) == 0

assert len(factory.buckets) == 3
factory._leaker.cancel()
14 changes: 9 additions & 5 deletions tests/test_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async def test_limiter_constructor_01(clock):

@pytest.mark.asyncio
async def test_limiter_constructor_02(
clock,
create_bucket,
limiter_should_raise,
limiter_delay,
Expand All @@ -55,7 +56,7 @@ async def test_limiter_constructor_02(

limiter = Limiter(
bucket,
clock=TimeClock(),
clock=clock,
raise_when_fail=limiter_should_raise,
max_delay=limiter_delay,
)
Expand All @@ -71,7 +72,7 @@ async def test_limiter_constructor_02(

assert acquire_ok

factory = DemoBucketFactory(TimeClock(), demo=bucket)
factory = DemoBucketFactory(clock, demo=bucket)
limiter = Limiter(
factory,
raise_when_fail=limiter_should_raise,
Expand All @@ -84,12 +85,13 @@ async def test_limiter_constructor_02(

@pytest.mark.asyncio
async def test_limiter_01(
clock,
create_bucket,
limiter_should_raise,
limiter_delay,
):
bucket = await create_bucket(DEFAULT_RATES)
factory = DemoBucketFactory(TimeClock(), demo=bucket)
factory = DemoBucketFactory(clock, demo=bucket)
limiter = Limiter(
factory,
raise_when_fail=limiter_should_raise,
Expand Down Expand Up @@ -167,12 +169,13 @@ async def test_limiter_01(

@pytest.mark.asyncio
async def test_limiter_concurrency(
clock,
create_bucket,
limiter_should_raise,
limiter_delay,
):
bucket: AbstractBucket = await create_bucket(DEFAULT_RATES)
factory = DemoBucketFactory(TimeClock(), demo=bucket)
factory = DemoBucketFactory(clock, demo=bucket)
limiter = Limiter(
factory,
raise_when_fail=limiter_should_raise,
Expand Down Expand Up @@ -214,12 +217,13 @@ async def test_limiter_concurrency(

@pytest.mark.asyncio
async def test_limiter_decorator(
clock,
create_bucket,
limiter_should_raise,
limiter_delay,
):
bucket = await create_bucket(DEFAULT_RATES)
factory = DemoBucketFactory(TimeClock(), demo=bucket)
factory = DemoBucketFactory(clock, demo=bucket)
limiter = Limiter(
factory,
raise_when_fail=limiter_should_raise,
Expand Down

0 comments on commit 0e4895f

Please sign in to comment.