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 230b9c3
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
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_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_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 230b9c3

Please sign in to comment.