Skip to content

Commit

Permalink
fix: ASyncBase._should_await (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Feb 26, 2023
1 parent 50196fe commit eaadbbc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
1 change: 1 addition & 0 deletions a_sync/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def _should_await(self, kwargs: dict) -> bool:
if flag == 'asynchronous':
# must invert
return not sync
return sync

# No flag found in kwargs, check for a flag attribute.
for flag in flags:
Expand Down
16 changes: 10 additions & 6 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from a_sync import ASyncBase
from async_property import async_property, async_cached_property

import asyncio

from a_sync import ASyncBase, async_property, async_cached_property

class TestClass(ASyncBase):
def __init__(self, sync: bool):
def __init__(self, v: int, sync: bool):
self.v = v
self.sync = sync

async def test_fn(self) -> int:
return 2
return self.v

@async_property
async def test_property(self) -> int:
return 4
return self.v * 2

@async_cached_property
async def test_cached_property(self) -> int:
return 6
await asyncio.sleep(2)
return self.v * 3

42 changes: 27 additions & 15 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@

import asyncio
import inspect
import pytest
import time

from tests.fixtures import TestClass
from a_sync._meta import ASyncMeta

def _await(coro):
return asyncio.get_event_loop().run_until_complete(coro)

def test_base_sync():
sync_instance = TestClass(True)
@pytest.mark.parametrize('i', range(10))
def test_base_sync(i: int):
sync_instance = TestClass(i, True)
assert isinstance(sync_instance.__class__, ASyncMeta)

assert sync_instance.test_fn() == 2
assert sync_instance.test_property == 4
assert sync_instance.test_cached_property == 6
assert sync_instance.test_fn() == i
assert sync_instance.test_property == i * 2
start = time.time()
assert sync_instance.test_cached_property == i * 3
assert isinstance(sync_instance.test_cached_property, int)
duration = time.time() - start
assert duration < 3, "There is a 2 second sleep in 'test_cached_property' but it should only run once."

# Can we override with kwargs?
assert inspect.isawaitable(sync_instance.test_fn(sync=False))
val = asyncio.get_event_loop().run_until_complete(sync_instance.test_fn(sync=False))
assert isinstance(val, int)

def test_base_async():
async_instance = TestClass(False)
@pytest.mark.asyncio
@pytest.mark.parametrize('i', list(range(10)))
async def test_base_async(i: int):
async_instance = TestClass(i, False)
assert isinstance(async_instance.__class__, ASyncMeta)

assert _await(async_instance.test_fn()) == 2
assert _await(async_instance.test_property) == 4
assert _await(async_instance.test_cached_property) == 6
assert await async_instance.test_fn() == i
assert await async_instance.test_property == i * 2
start = time.time()
assert await async_instance.test_cached_property == i * 3
assert isinstance(await async_instance.test_cached_property, int)
duration = time.time() - start
assert duration < 3, "There is a 2 second sleep in 'test_cached_property' but it should only run once."

# Can we override with kwargs?
assert isinstance(async_instance.test_fn(sync=True), int)
with pytest.raises(RuntimeError):
async_instance.test_fn(sync=True)

0 comments on commit eaadbbc

Please sign in to comment.