Skip to content

Commit 3b81578

Browse files
test: compatibility with reacton 2.0
See widgetti/reacton#42 Solara is already compatible with it, but the tests were not. We might want to test this in CI, before releasing Solara 2.0
1 parent 058a885 commit 3b81578

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

solara/tasks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def __init__(self, key: str):
104104
self._progress = ref(self._result.fields.progress)
105105
self._exception = ref(self._result.fields.exception)
106106
self._state_ = ref(self._result.fields._state)
107+
# used for tests only
108+
self._start_event = threading.Event()
109+
self._start_event.set()
107110

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

255258
async def _async_run(self, call_event_loop: asyncio.AbstractEventLoop, future: asyncio.Future, args, kwargs) -> None:
259+
self._start_event.wait()
260+
256261
task_for_this_call = asyncio.current_task()
257262
assert task_for_this_call is not None
258263

@@ -351,6 +356,7 @@ def is_current(self):
351356

352357
def _run(self, _last_finished_event, previous_thread: Optional[threading.Thread], cancel_event, args, kwargs) -> None:
353358
# use_thread has this as default, which can make code run 10x slower
359+
self._start_event.wait()
354360
intrusive_cancel = False
355361
wait_on_previous = False
356362
self._local.cancel_event = cancel_event

tests/unit/task_test.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ def collect():
8686

8787
box, rc = solara.render(SquareButton(3, on_render=collect), handle_error=False)
8888
button = rc.find(v.Btn, children=["Run"]).widget
89+
# a combination of .clear/.set is needed to force the rendering of all the states
90+
# otherwise some states are not rendered
91+
square._start_event.clear() # type: ignore
8992
button.click()
93+
square._start_event.set() # type: ignore
9094
assert square._last_finished_event # type: ignore
9195
square._last_finished_event.wait() # type: ignore
9296
assert results == [
@@ -98,7 +102,9 @@ def collect():
98102
results.clear()
99103
rc.render(SquareButton(2, on_render=collect))
100104
button = rc.find(v.Btn, children=["Run"]).widget
105+
square._start_event.clear()
101106
button.click()
107+
square._start_event.set()
102108
square._last_finished_event.wait() # type: ignore
103109
assert results == [
104110
# extra finished due to the rc.render call
@@ -152,7 +158,9 @@ def collect():
152158

153159
box, rc = solara.render(SquareButtonAsync(3, on_render=collect), handle_error=False)
154160
button = rc.find(v.Btn, children=["Run"]).widget
161+
square_async._start_event.clear() # type: ignore
155162
button.click()
163+
square_async._start_event.set() # type: ignore
156164
assert square_async.current_future # type: ignore
157165
await square_async.current_future # type: ignore
158166
assert results == [
@@ -164,7 +172,9 @@ def collect():
164172
results.clear()
165173
rc.render(SquareButtonAsync(2, on_render=collect))
166174
button = rc.find(v.Btn, children=["Run"]).widget
175+
square_async._start_event.clear() # type: ignore
167176
button.click()
177+
square_async._start_event.set() # type: ignore
168178
await square_async.current_future # type: ignore
169179
assert results == [
170180
# extra finished due to the rc.render call
@@ -195,8 +205,9 @@ def Test():
195205

196206
box, rc = solara.render(Test(), handle_error=False)
197207
button = rc.find(v.Btn, children=["Run"])[0].widget
208+
square._start_event.clear() # type: ignore
198209
button.click()
199-
assert square._last_finished_event # type: ignore
210+
square._start_event.set() # type: ignore
200211
square._last_finished_event.wait() # type: ignore
201212
assert (
202213
results2
@@ -214,7 +225,9 @@ def Test():
214225
results2.clear()
215226
results3.clear()
216227
button = rc.find(v.Btn, children=["Run"])[1].widget
228+
square._start_event.clear() # type: ignore
217229
button.click()
230+
square._start_event.set() # type: ignore
218231
assert square._last_finished_event # type: ignore
219232
square._last_finished_event.wait() # type: ignore
220233
assert (
@@ -244,7 +257,9 @@ def collect():
244257
button = rc.find(v.Btn, children=["Run"]).widget
245258
cancel_square = True
246259
try:
260+
square._start_event.clear() # type: ignore
247261
button.click()
262+
square._start_event.set() # type: ignore
248263
assert square._last_finished_event # type: ignore
249264
square._last_finished_event.wait() # type: ignore
250265
assert results == [
@@ -284,7 +299,9 @@ def collect():
284299
button = rc.find(v.Btn, children=["Run"]).widget
285300
cancel_square_async = True
286301
try:
302+
square_async._start_event.clear() # type: ignore
287303
button.click()
304+
square_async._start_event.set() # type: ignore
288305
assert square_async.current_future # type: ignore
289306
try:
290307
await square_async.current_future # type: ignore
@@ -337,7 +354,9 @@ def collect2():
337354
button2 = rc2.find(v.Btn, children=["Run"]).widget
338355

339356
with context1:
357+
something._start_event.clear() # type: ignore
340358
button1.click()
359+
something._start_event.set() # type: ignore
341360
finished_event1 = something._last_finished_event # type: ignore
342361
assert finished_event1
343362

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

358377
with context2:
378+
something._start_event.clear() # type: ignore
359379
button2.click()
380+
something._start_event.set() # type: ignore
360381
finished_event2 = something._last_finished_event # type: ignore
361382
assert finished_event2
362383
finished_event2.wait()

0 commit comments

Comments
 (0)