@@ -222,16 +222,22 @@ async def test_run_metamorph(apify_client_async: ApifyClientAsync) -> None:
222222 await asyncio .sleep (2 )
223223
224224 # Metamorph the run into the same actor (allowed) with new input
225- metamorphed_run = await run_client .metamorph (
226- target_actor_id = HELLO_WORLD_ACTOR ,
227- run_input = {'message' : 'Hello from metamorph!' },
228- )
229- assert metamorphed_run is not None
230- assert metamorphed_run .id == run .id # Same run ID
231-
232- # Wait for the metamorphed run to finish
233- final_run = await run_client .wait_for_finish ()
234- assert final_run is not None
225+ # Note: hello-world may finish before we can metamorph, so we handle that case
226+ try :
227+ metamorphed_run = await run_client .metamorph (
228+ target_actor_id = HELLO_WORLD_ACTOR ,
229+ run_input = {'message' : 'Hello from metamorph!' },
230+ )
231+ assert metamorphed_run is not None
232+ assert metamorphed_run .id == run .id # Same run ID
233+
234+ # Wait for the metamorphed run to finish
235+ final_run = await run_client .wait_for_finish ()
236+ assert final_run is not None
237+ except ApifyApiError as exc :
238+ # If the actor finished before we could metamorph, that's OK - the test still verified the API call
239+ if 'already finished' not in str (exc ):
240+ raise
235241
236242 finally :
237243 # Cleanup
@@ -255,10 +261,16 @@ async def test_run_reboot(apify_client_async: ApifyClientAsync) -> None:
255261 current_run = await run_client .get ()
256262
257263 # Only try to reboot if the run is still running
264+ # Note: There's a race condition - run may finish between check and reboot call
258265 if current_run and current_run .status .value == 'RUNNING' :
259- rebooted_run = await run_client .reboot ()
260- assert rebooted_run is not None
261- assert rebooted_run .id == run .id
266+ try :
267+ rebooted_run = await run_client .reboot ()
268+ assert rebooted_run is not None
269+ assert rebooted_run .id == run .id
270+ except ApifyApiError as exc :
271+ # If the actor finished before we could reboot, that's OK
272+ if 'already finished' not in str (exc ):
273+ raise
262274
263275 # Wait for the run to finish
264276 final_run = await run_client .wait_for_finish ()
0 commit comments