@@ -1303,6 +1303,34 @@ def resurrect(run_client: Any, **_: Any) -> Run:
13031303 assert charge_result .charged_count == 0
13041304
13051305
1306+ async def test_resurrection_in_flight_reserves_its_limit_once (
1307+ parent_budget : dict [str , Run ], apify_client_async_patcher : ApifyClientAsyncPatcher
1308+ ) -> None :
1309+ """While a resurrection is in flight, its limit is reserved once, including the charge its run made before."""
1310+ parent_budget ['old-run' ] = finish (make_run ('old-run' , 'RUNNING' ), 'ABORTED' , 1 , finished_ago = timedelta (minutes = 5 ))
1311+ started = asyncio .Event ()
1312+ release = asyncio .Event ()
1313+
1314+ async def resurrect (run_client : Any , ** _ : Any ) -> Run :
1315+ started .set ()
1316+ await release .wait ()
1317+ return parent_budget [run_client ._resource_id ].model_copy (update = {'status' : 'RUNNING' , 'finished_at' : None })
1318+
1319+ async with Actor :
1320+ await seed_budget_record ('child' , 'old-run' , maxTotalChargeUsd = '6' )
1321+
1322+ apify_client_async_patcher .patch ('run' , 'resurrect' , replacement_method = resurrect , is_async = True )
1323+
1324+ async with _ActorType () as actor :
1325+ start_task = asyncio .create_task (actor .start ('some-actor' , name = 'child' , max_total_charge_usd = Decimal (4 )))
1326+ await started .wait ()
1327+ charge_result = await actor .charge ('some-event' , count = 10 )
1328+ release .set ()
1329+ await start_task
1330+
1331+ assert charge_result .charged_count == 6
1332+
1333+
13061334async def test_failed_resurrection_leaves_the_charge_of_its_run_to_settle (
13071335 parent_budget : dict [str , Run ], apify_client_async_patcher : ApifyClientAsyncPatcher , monkeypatch : pytest .MonkeyPatch
13081336) -> None :
0 commit comments