Skip to content

Commit

Permalink
enable curio in pytest; add special handling for curio scopes; add te…
Browse files Browse the repository at this point in the history
…st to test whether callbacks may kill themselves during transitions
  • Loading branch information
aleneum committed Oct 13, 2020
1 parent 2f323a1 commit acbcdae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@pytest.fixture(params=[
pytest.param('asyncio'),
pytest.param('trio'),
# pytest.param('curio'),
pytest.param('curio'),
])
def anyio_backend(request):
return request.param
Expand Down
15 changes: 15 additions & 0 deletions tests/test_transitions_anyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ def sync_callback(event_data):
assert m.is_B() is True


async def test_async_callback_trigger(machine_cls):
mock_processed = MagicMock()

async def on_event(event_data):
await event_data.model.to_C()
mock_processed()

m = machine_cls(states=['A', 'B', 'C'],
transitions=[dict(trigger='go', source='A', dest='B', after=on_event)],
initial='A', send_event=True)
await m.go()
assert m.is_C()
assert mock_processed.called


async def test_async_invalid_triggers(m):
await m.to_B()

Expand Down
4 changes: 3 additions & 1 deletion transitions_anyio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from anyio import create_task_group, get_cancelled_exc_class, open_cancel_scope
from anyio._backends._curio import CancelScope as CurioCancelScope
from transitions.extensions import GraphMachine
from transitions.extensions.asyncio import (AsyncMachine, AsyncTransition,
HierarchicalAsyncMachine,
Expand All @@ -21,7 +22,8 @@ async def with_result(func):
return results

async def process_context(self, func, model):
if self.current_context.get() is None:
current = self.current_context.get()
if current is None or isinstance(current, CurioCancelScope):
res = False
async with open_cancel_scope() as scope:
self.current_context.set(scope)
Expand Down

0 comments on commit acbcdae

Please sign in to comment.