11from __future__ import annotations
22
3+ import asyncio
34import warnings
45from datetime import timedelta
56from typing import TYPE_CHECKING
@@ -130,19 +131,21 @@ async def test_metamorph_fails_locally(caplog: pytest.LogCaptureFixture) -> None
130131 async with Actor :
131132 await Actor .metamorph ('random-id' )
132133
133- assert len (caplog .records ) == 1
134- assert caplog .records [0 ].levelname == 'ERROR'
135- assert 'Actor.metamorph() is only supported when running on the Apify platform.' in caplog .records [0 ].message
134+ matching = [r for r in caplog .records if 'Actor.metamorph()' in r .message ]
135+ assert len (matching ) == 1
136+ assert matching [0 ].levelname == 'ERROR'
137+ assert 'only supported when running on the Apify platform' in matching [0 ].message
136138
137139
138140async def test_reboot_fails_locally (caplog : pytest .LogCaptureFixture ) -> None :
139141 caplog .set_level ('WARNING' )
140142 async with Actor :
141143 await Actor .reboot ()
142144
143- assert len (caplog .records ) == 1
144- assert caplog .records [0 ].levelname == 'ERROR'
145- assert 'Actor.reboot() is only supported when running on the Apify platform.' in caplog .records [0 ].message
145+ matching = [r for r in caplog .records if 'Actor.reboot()' in r .message ]
146+ assert len (matching ) == 1
147+ assert matching [0 ].levelname == 'ERROR'
148+ assert 'only supported when running on the Apify platform' in matching [0 ].message
146149
147150
148151async def test_add_webhook_fails_locally (caplog : pytest .LogCaptureFixture ) -> None :
@@ -152,9 +155,10 @@ async def test_add_webhook_fails_locally(caplog: pytest.LogCaptureFixture) -> No
152155 Webhook (event_types = [WebhookEventType .ACTOR_BUILD_ABORTED ], request_url = 'https://example.com' )
153156 )
154157
155- assert len (caplog .records ) == 1
156- assert caplog .records [0 ].levelname == 'ERROR'
157- assert 'Actor.add_webhook() is only supported when running on the Apify platform.' in caplog .records [0 ].message
158+ matching = [r for r in caplog .records if 'Actor.add_webhook()' in r .message ]
159+ assert len (matching ) == 1
160+ assert matching [0 ].levelname == 'ERROR'
161+ assert 'only supported when running on the Apify platform' in matching [0 ].message
158162
159163
160164async def test_set_status_message_locally (caplog : pytest .LogCaptureFixture ) -> None :
@@ -189,8 +193,9 @@ async def test_push_data_with_empty_data() -> None:
189193 assert result is None
190194
191195
192- async def test_off_removes_event_listener () -> None :
196+ async def test_off_removes_event_listener (monkeypatch : pytest . MonkeyPatch ) -> None :
193197 """Test that Actor.off() removes an event listener."""
198+ monkeypatch .setenv (ApifyEnvVars .PERSIST_STATE_INTERVAL_MILLIS , '50' )
194199 called = False
195200
196201 async def listener (_data : object ) -> None :
@@ -200,6 +205,10 @@ async def listener(_data: object) -> None:
200205 async with Actor :
201206 Actor .on (Event .PERSIST_STATE , listener )
202207 Actor .off (Event .PERSIST_STATE , listener )
208+ # Wait long enough for at least one PERSIST_STATE event to fire
209+ await asyncio .sleep (0.2 )
210+ # Verify the listener was NOT called because it was removed
211+ assert called is False
203212
204213
205214async def test_start_actor_with_webhooks (
@@ -214,7 +223,11 @@ async def test_start_actor_with_webhooks(
214223 webhooks = [Webhook (event_types = [WebhookEventType .ACTOR_RUN_SUCCEEDED ], request_url = 'https://example.com' )],
215224 )
216225
217- assert len (apify_client_async_patcher .calls ['actor' ]['start' ]) == 1
226+ calls = apify_client_async_patcher .calls ['actor' ]['start' ]
227+ assert len (calls ) == 1
228+ _ , kwargs = calls [0 ][0 ], calls [0 ][1 ]
229+ assert 'webhooks' in kwargs
230+ assert kwargs ['webhooks' ] is not None
218231
219232
220233async def test_start_actor_with_timedelta_timeout (
@@ -226,7 +239,10 @@ async def test_start_actor_with_timedelta_timeout(
226239 async with Actor :
227240 await Actor .start ('some-actor-id' , timeout = timedelta (seconds = 120 ))
228241
229- assert len (apify_client_async_patcher .calls ['actor' ]['start' ]) == 1
242+ calls = apify_client_async_patcher .calls ['actor' ]['start' ]
243+ assert len (calls ) == 1
244+ _ , kwargs = calls [0 ][0 ], calls [0 ][1 ]
245+ assert kwargs .get ('timeout_secs' ) == 120
230246
231247
232248async def test_start_actor_with_invalid_timeout (
@@ -252,7 +268,11 @@ async def test_call_actor_with_webhooks(
252268 webhooks = [Webhook (event_types = [WebhookEventType .ACTOR_RUN_SUCCEEDED ], request_url = 'https://example.com' )],
253269 )
254270
255- assert len (apify_client_async_patcher .calls ['actor' ]['call' ]) == 1
271+ calls = apify_client_async_patcher .calls ['actor' ]['call' ]
272+ assert len (calls ) == 1
273+ _ , kwargs = calls [0 ][0 ], calls [0 ][1 ]
274+ assert 'webhooks' in kwargs
275+ assert kwargs ['webhooks' ] is not None
256276
257277
258278async def test_call_actor_with_timedelta_timeout (
@@ -264,7 +284,10 @@ async def test_call_actor_with_timedelta_timeout(
264284 async with Actor :
265285 await Actor .call ('some-actor-id' , timeout = timedelta (seconds = 120 ))
266286
267- assert len (apify_client_async_patcher .calls ['actor' ]['call' ]) == 1
287+ calls = apify_client_async_patcher .calls ['actor' ]['call' ]
288+ assert len (calls ) == 1
289+ _ , kwargs = calls [0 ][0 ], calls [0 ][1 ]
290+ assert kwargs .get ('timeout_secs' ) == 120
268291
269292
270293async def test_call_actor_with_remaining_time_deprecation (
@@ -305,7 +328,11 @@ async def test_call_task_with_webhooks(
305328 webhooks = [Webhook (event_types = [WebhookEventType .ACTOR_RUN_SUCCEEDED ], request_url = 'https://example.com' )],
306329 )
307330
308- assert len (apify_client_async_patcher .calls ['task' ]['call' ]) == 1
331+ calls = apify_client_async_patcher .calls ['task' ]['call' ]
332+ assert len (calls ) == 1
333+ _ , kwargs = calls [0 ][0 ], calls [0 ][1 ]
334+ assert 'webhooks' in kwargs
335+ assert kwargs ['webhooks' ] is not None
309336
310337
311338async def test_call_task_with_timedelta_timeout (
@@ -317,7 +344,10 @@ async def test_call_task_with_timedelta_timeout(
317344 async with Actor :
318345 await Actor .call_task ('some-task-id' , timeout = timedelta (seconds = 120 ))
319346
320- assert len (apify_client_async_patcher .calls ['task' ]['call' ]) == 1
347+ calls = apify_client_async_patcher .calls ['task' ]['call' ]
348+ assert len (calls ) == 1
349+ _ , kwargs = calls [0 ][0 ], calls [0 ][1 ]
350+ assert kwargs .get ('timeout_secs' ) == 120
321351
322352
323353async def test_call_task_with_invalid_timeout (
0 commit comments