Skip to content

Commit

Permalink
fix unavoidable test side effects
Browse files Browse the repository at this point in the history
we add a comment explaining the solution
  • Loading branch information
rmorshea committed Sep 4, 2020
1 parent e9b83ea commit 26e3d25
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 1 addition & 4 deletions idom/core/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,8 @@ def sync_function() -> Optional[_EffectCleanFunc]:
future = asyncio.ensure_future(async_function())

def clean_future() -> None:
try:
if not future.cancel():
clean = future.result()
except asyncio.InvalidStateError:
future.cancel()
else:
if clean is not None:
clean()

Expand Down
13 changes: 10 additions & 3 deletions tests/test_core/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,12 @@ async def effect():
await asyncio.wait_for(cleanup_ran.wait(), 1)


async def test_use_async_effect_cancel():
async def test_use_async_effect_cancel(caplog):
element_hook = HookCatcher()
effect_ran = asyncio.Event()
effect_was_cancelled = asyncio.Event()

event_that_is_never_set = asyncio.Event()
event_that_never_occurs = asyncio.Event()

@idom.element
@element_hook.capture
Expand All @@ -486,9 +486,10 @@ def ElementWithLongWaitingEffect():
async def effect():
effect_ran.set()
try:
await event_that_is_never_set.wait()
await event_that_never_occurs.wait()
except asyncio.CancelledError:
effect_was_cancelled.set()
raise

return idom.html.div()

Expand All @@ -502,6 +503,12 @@ async def effect():

await asyncio.wait_for(effect_was_cancelled.wait(), 1)

# So I know we said the event never occurs but... to ensure the effect's future is
# cancelled before the test is cleaned up we need to set the event. This is because
# the cancellation doesn't propogate before the test is resolved which causes
# delayed log messages that impact other tests.
event_that_never_occurs.set()


async def test_error_in_effect_is_gracefully_handled(caplog):
@idom.element
Expand Down

0 comments on commit 26e3d25

Please sign in to comment.