Skip to content

Commit

Permalink
Bugs (#19)
Browse files Browse the repository at this point in the history
* fix: make arg optional

* fix: unpacking syntax err

* feat(test): test semaphore
  • Loading branch information
BobTheBuidler authored Mar 1, 2023
1 parent d450bf7 commit a498dac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion a_sync/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def some_fn():
some_fn() == True
await some_fn(sync=False) == True
"""
modifiers: ModifierManager = ModifierManager(**modifiers)
modifiers: ModifierManager = ModifierManager(modifiers)

# If the dev tried passing a default as an arg instead of a kwarg, ie: @a_sync('sync')...
if coro_fn in ['async', 'sync']:
Expand Down
2 changes: 1 addition & 1 deletion a_sync/modifiers/cache/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def apply_async_memory_cache(
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:...

def apply_async_memory_cache(
coro_fn: Optional[Union[Callable[P, Awaitable[T]], int]],
coro_fn: Optional[Union[Callable[P, Awaitable[T]], int]] = None,
maxsize: Optional[int] = None,
ttl: Optional[int] = None,
typed: bool = False,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

import pytest
from time import time
from tests.fixtures import TestSemaphore, increment


# ISSUES
# - We are unable to pass in an existing semaphore object, it attaches to a different loop.
# Maybe a problem with test suite interaction?
# - semaphore modifier works fine with integer inputs

@increment
@pytest.mark.asyncio_cooperative
async def test_semaphore(i: int):
instance = TestSemaphore(i, sync=False)

start = time()
assert await instance.test_fn() == i
duration = time() - start
assert i < 3 or duration > i # There is a 1 second sleep in this fn. If the semaphore is not working, all tests will complete in 1 second.


@increment
@pytest.mark.asyncio_cooperative
async def test_semaphore_property(i: int):
instance = TestSemaphore(i, sync=False)
start = time()
assert await instance.test_property == i * 2
duration = time() - start
assert i < 3 or duration > i # There is a 1 second sleep in this fn. If the semaphore is not working, all tests will complete in 1 second.



@increment
@pytest.mark.asyncio_cooperative
async def test_semaphore_cached_property(i: int):
instance = TestSemaphore(i, sync=False)
start = time()
assert await instance.test_cached_property == i * 3
duration = time() - start
# There is a 1 second sleep in this fn but a semaphore override with a value of 50.
# You can tell it worked correctly because the class-defined semaphore value is just one, whch would cause this test to fail if it were used.
# If the override is not working, all tests will complete in just over 1 second.
assert i < 3 or duration < 1.5

0 comments on commit a498dac

Please sign in to comment.