Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: compatibility with reacton 2.0 #988

Merged
merged 1 commit into from
Jan 29, 2025
Merged
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
6 changes: 6 additions & 0 deletions solara/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def __init__(self, key: str):
self._progress = ref(self._result.fields.progress)
self._exception = ref(self._result.fields.exception)
self._state_ = ref(self._result.fields._state)
# used for tests only
self._start_event = threading.Event()
self._start_event.set()

@property
def result(self) -> TaskResult[R]:
Expand Down Expand Up @@ -253,6 +256,8 @@ def is_current(self):
return (self.current_task == asyncio.current_task()) and not running_task.cancelled()

async def _async_run(self, call_event_loop: asyncio.AbstractEventLoop, future: asyncio.Future, args, kwargs) -> None:
self._start_event.wait()

task_for_this_call = asyncio.current_task()
assert task_for_this_call is not None

Expand Down Expand Up @@ -351,6 +356,7 @@ def is_current(self):

def _run(self, _last_finished_event, previous_thread: Optional[threading.Thread], cancel_event, args, kwargs) -> None:
# use_thread has this as default, which can make code run 10x slower
self._start_event.wait()
intrusive_cancel = False
wait_on_previous = False
self._local.cancel_event = cancel_event
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/task_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def collect():

box, rc = solara.render(SquareButton(3, on_render=collect), handle_error=False)
button = rc.find(v.Btn, children=["Run"]).widget
# a combination of .clear/.set is needed to force the rendering of all the states
# otherwise some states are not rendered
square._start_event.clear() # type: ignore
button.click()
square._start_event.set() # type: ignore
assert square._last_finished_event # type: ignore
square._last_finished_event.wait() # type: ignore
assert results == [
Expand All @@ -98,7 +102,9 @@ def collect():
results.clear()
rc.render(SquareButton(2, on_render=collect))
button = rc.find(v.Btn, children=["Run"]).widget
square._start_event.clear()
button.click()
square._start_event.set()
square._last_finished_event.wait() # type: ignore
assert results == [
# extra finished due to the rc.render call
Expand Down Expand Up @@ -152,7 +158,9 @@ def collect():

box, rc = solara.render(SquareButtonAsync(3, on_render=collect), handle_error=False)
button = rc.find(v.Btn, children=["Run"]).widget
square_async._start_event.clear() # type: ignore
button.click()
square_async._start_event.set() # type: ignore
assert square_async.current_future # type: ignore
await square_async.current_future # type: ignore
assert results == [
Expand All @@ -164,7 +172,9 @@ def collect():
results.clear()
rc.render(SquareButtonAsync(2, on_render=collect))
button = rc.find(v.Btn, children=["Run"]).widget
square_async._start_event.clear() # type: ignore
button.click()
square_async._start_event.set() # type: ignore
await square_async.current_future # type: ignore
assert results == [
# extra finished due to the rc.render call
Expand Down Expand Up @@ -195,8 +205,9 @@ def Test():

box, rc = solara.render(Test(), handle_error=False)
button = rc.find(v.Btn, children=["Run"])[0].widget
square._start_event.clear() # type: ignore
button.click()
assert square._last_finished_event # type: ignore
square._start_event.set() # type: ignore
square._last_finished_event.wait() # type: ignore
assert (
results2
Expand All @@ -214,7 +225,9 @@ def Test():
results2.clear()
results3.clear()
button = rc.find(v.Btn, children=["Run"])[1].widget
square._start_event.clear() # type: ignore
button.click()
square._start_event.set() # type: ignore
assert square._last_finished_event # type: ignore
square._last_finished_event.wait() # type: ignore
assert (
Expand Down Expand Up @@ -244,7 +257,9 @@ def collect():
button = rc.find(v.Btn, children=["Run"]).widget
cancel_square = True
try:
square._start_event.clear() # type: ignore
button.click()
square._start_event.set() # type: ignore
assert square._last_finished_event # type: ignore
square._last_finished_event.wait() # type: ignore
assert results == [
Expand Down Expand Up @@ -284,7 +299,9 @@ def collect():
button = rc.find(v.Btn, children=["Run"]).widget
cancel_square_async = True
try:
square_async._start_event.clear() # type: ignore
button.click()
square_async._start_event.set() # type: ignore
assert square_async.current_future # type: ignore
try:
await square_async.current_future # type: ignore
Expand Down Expand Up @@ -337,7 +354,9 @@ def collect2():
button2 = rc2.find(v.Btn, children=["Run"]).widget

with context1:
something._start_event.clear() # type: ignore
button1.click()
something._start_event.set() # type: ignore
finished_event1 = something._last_finished_event # type: ignore
assert finished_event1

Expand All @@ -356,7 +375,9 @@ def collect2():
assert results2 == [(TaskState.NOTCALLED, None)]

with context2:
something._start_event.clear() # type: ignore
button2.click()
something._start_event.set() # type: ignore
finished_event2 = something._last_finished_event # type: ignore
assert finished_event2
finished_event2.wait()
Expand Down
Loading