Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_acquire(event_loop, task):
current_time = 0
def mocked_time():
return current_time
# capacity released every 2 seconds
limiter = AsyncLimiter(5, 10)
with mock.patch.object(event_loop, "time", mocked_time):
tasks = [asyncio.ensure_future(task(limiter)) for _ in range(10)]
pending = await wait_for_n_done(tasks, 5)
assert len(pending) == 5
current_time = 3 # releases capacity for one and some buffer
assert limiter.has_capacity()
pending = await wait_for_n_done(pending, 1)
assert len(pending) == 4
current_time = 7 # releases capacity for two more, plus buffer
pending = await wait_for_n_done(pending, 2)
assert len(pending) == 2
def test_attributes():
limiter = AsyncLimiter(42, 81)
assert limiter.max_rate == 42
assert limiter.time_period == 81
async def test_has_capacity():
limiter = AsyncLimiter(1)
assert limiter.has_capacity()
assert not limiter.has_capacity(42)
await limiter.acquire()
assert not limiter.has_capacity()
async def test_over_acquire():
limiter = AsyncLimiter(1)
with pytest.raises(ValueError):
await limiter.acquire(42)
def test_version():
# the version is taken from metadata
from aiolimiter import __version__
assert __version__
pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
with pyproject as f:
metadata = toml.load(f)["tool"]["poetry"]
# pyproject bumps to -alpha.0, -beta.1, etc., but releases a0, b1
# We don't really need to care about those, just verify that sorta
# the right version is used.
assert __version__.startswith(metadata["version"].partition("-")[0])
def test_version():
# the version is taken from metadata
from aiolimiter import __version__
assert __version__
pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
with pyproject as f:
metadata = toml.load(f)["tool"]["poetry"]
# pyproject bumps to -alpha.0, -beta.1, etc., but releases a0, b1
# We don't really need to care about those, just verify that sorta
# the right version is used.
assert __version__.startswith(metadata["version"].partition("-")[0])