Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
germaniuss committed Jan 22, 2024
1 parent c1845da commit fa4a0e0
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_method(self):

# Check __self__ has been copied
assert method.__self__ == instance


@pytest.mark.asyncio
async def test_async_to_sync_to_async():
Expand Down Expand Up @@ -223,6 +223,56 @@ def sync_function():
assert result["thread"] == threading.current_thread()


@pytest.mark.asyncio
async def test_async_to_sync_to_async_method_decorator():
"""
Test async_to_sync as a function decorator uses the outer thread
when used inside sync_to_async.
"""
result = {}

# Define async function
@async_to_sync
async def inner_async_function():
result["worked"] = True
result["thread"] = threading.current_thread()
return 42

# Define sync function
@sync_to_async
def sync_function():
return inner_async_function()

# Check it works right
number = await sync_function()
assert number == 42
assert result["worked"]
# Make sure that it didn't needlessly make a new async loop
assert result["thread"] == threading.current_thread()

@pytest.mark.asyncio
async def test_async_to_sync_to_thread_method_decorator():
"""
Test async_to_sync as a function decorator uses the outer thread
when used inside another sync thread.
"""
result = {}

# Define async function
@async_to_sync
async def inner_async_function():
result["worked"] = True
result["thread"] = threading.current_thread()
return 42

# Check it works right
number = await asyncio.to_thread(inner_async_function)
assert number == 42
assert result["worked"]
# Make sure that it didn't needlessly make a new async loop
assert result["thread"] == threading.current_thread()


def test_async_to_sync_fail_non_function():
"""
async_to_sync raises a TypeError when applied to a non-function.
Expand Down

0 comments on commit fa4a0e0

Please sign in to comment.