Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ ENV/

# Rope project settings
.ropeproject
.pytest_cache/
4 changes: 2 additions & 2 deletions a_sync/a_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def to_async(blocking_func: AnyCallable) -> AnyCallable:
* keep an async partial the same
* validate that it doesn't matter if the loop is running.
"""
if asyncio.iscoroutinefunction(getattr(blocking_func, 'func', blocking_func)):
if asyncio.iscoroutinefunction(blocking_func) or asyncio.iscoroutinefunction(getattr(blocking_func, 'func', None)):
# caller messed up - this is already async
async_func = blocking_func
else:
Expand Down Expand Up @@ -146,7 +146,7 @@ def to_blocking(async_func: AnyCallable) -> AnyCallable:
* keep blocking partial the same
* convert async partial
"""
if not asyncio.iscoroutinefunction(getattr(async_func, 'func', async_func)):
if not (asyncio.iscoroutinefunction(async_func) or asyncio.iscoroutinefunction(getattr(async_func, 'func', None))):
# caller messed up - this is already blocking
blocking = async_func
else:
Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest==3.5.1
asynctest==0.12.0
pytest-asyncio==0.8.0
67 changes: 67 additions & 0 deletions tests/test_a_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import functools

import asynctest
import pytest

import a_sync


def test_block():
async def async_func():
return 5

def sync_func():
return 4

assert a_sync.block(async_func) == 5
assert a_sync.block(sync_func) == 4


def test_block_partials():
def sync_func_to_partial(retval):
return retval

async def async_func_to_partial(retval):
return retval

sync_partial = functools.partial(sync_func_to_partial, 5)
async_partial = functools.partial(async_func_to_partial, 4)

assert a_sync.block(sync_partial) == 5
assert a_sync.block(async_partial) == 4


def test_block_works_with_CoroutineMock():
assert 3 == a_sync.block(asynctest.CoroutineMock(return_value=3))


@pytest.mark.asyncio
async def test_run():
async def async_func():
return 5

def sync_func():
return 4

assert await a_sync.run(async_func) == 5
assert await a_sync.run(sync_func) == 4


@pytest.mark.asyncio
async def test_run_works_with_CoroutineMock():
assert 3 == await a_sync.run(asynctest.CoroutineMock(return_value=3))


@pytest.mark.asyncio
async def test_run_partials():
def sync_func_to_partial(retval):
return retval

async def async_func_to_partial(retval):
return retval

sync_partial = functools.partial(sync_func_to_partial, 5)
async_partial = functools.partial(async_func_to_partial, 4)

assert await a_sync.run(sync_partial) == 5
assert await a_sync.run(async_partial) == 4
9 changes: 9 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tox]
envlist = py36

[testenv]
basepython = python3.6
deps =
--requirement={toxinidir}/requirements-dev.txt
commands =
py.test {posargs:tests}