Skip to content

Commit 26e3d25

Browse files
committed
fix unavoidable test side effects
we add a comment explaining the solution
1 parent e9b83ea commit 26e3d25

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

idom/core/hooks.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,8 @@ def sync_function() -> Optional[_EffectCleanFunc]:
139139
future = asyncio.ensure_future(async_function())
140140

141141
def clean_future() -> None:
142-
try:
142+
if not future.cancel():
143143
clean = future.result()
144-
except asyncio.InvalidStateError:
145-
future.cancel()
146-
else:
147144
if clean is not None:
148145
clean()
149146

tests/test_core/test_hooks.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,12 @@ async def effect():
472472
await asyncio.wait_for(cleanup_ran.wait(), 1)
473473

474474

475-
async def test_use_async_effect_cancel():
475+
async def test_use_async_effect_cancel(caplog):
476476
element_hook = HookCatcher()
477477
effect_ran = asyncio.Event()
478478
effect_was_cancelled = asyncio.Event()
479479

480-
event_that_is_never_set = asyncio.Event()
480+
event_that_never_occurs = asyncio.Event()
481481

482482
@idom.element
483483
@element_hook.capture
@@ -486,9 +486,10 @@ def ElementWithLongWaitingEffect():
486486
async def effect():
487487
effect_ran.set()
488488
try:
489-
await event_that_is_never_set.wait()
489+
await event_that_never_occurs.wait()
490490
except asyncio.CancelledError:
491491
effect_was_cancelled.set()
492+
raise
492493

493494
return idom.html.div()
494495

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

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

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

506513
async def test_error_in_effect_is_gracefully_handled(caplog):
507514
@idom.element

0 commit comments

Comments
 (0)