4
4
import contextlib
5
5
import json
6
6
import sys
7
+ from datetime import datetime , timezone
7
8
from typing import Any , Callable , cast
9
+ from unittest .mock import AsyncMock , Mock
8
10
9
11
import pytest
10
12
import websockets .server
11
13
from lazy_object_proxy import Proxy
12
14
13
- from apify_shared .consts import ApifyEnvVars
15
+ from apify_shared .consts import ActorEnvVars , ApifyEnvVars
14
16
from crawlee .events ._types import Event , EventPersistStateData
15
17
16
18
import apify ._actor
@@ -129,6 +131,7 @@ async def test_actor_handles_migrating_event_correctly(monkeypatch: pytest.Monke
129
131
# the Actor automatically emits the PERSIST_STATE event with data `{'isMigrating': True}`
130
132
monkeypatch .setenv (ApifyEnvVars .PERSIST_STATE_INTERVAL_MILLIS , '500' )
131
133
monkeypatch .setenv (ApifyEnvVars .IS_AT_HOME , '1' )
134
+ monkeypatch .setenv (ActorEnvVars .RUN_ID , 'asdf' )
132
135
133
136
persist_state_events_data = []
134
137
@@ -143,6 +146,37 @@ async def handler(websocket: websockets.server.WebSocketServerProtocol) -> None:
143
146
port : int = ws_server .sockets [0 ].getsockname ()[1 ] # type: ignore[index]
144
147
monkeypatch .setenv (ApifyEnvVars .ACTOR_EVENTS_WS_URL , f'ws://localhost:{ port } ' )
145
148
149
+ mock_run_client = Mock ()
150
+ mock_run_client .run .return_value .get = AsyncMock (
151
+ side_effect = lambda : {
152
+ 'id' : 'asdf' ,
153
+ 'actId' : 'asdf' ,
154
+ 'userId' : 'adsf' ,
155
+ 'startedAt' : datetime .now (timezone .utc ),
156
+ 'status' : 'RUNNING' ,
157
+ 'meta' : {'origin' : 'DEVELOPMENT' },
158
+ 'stats' : {
159
+ 'inputBodyLen' : 99 ,
160
+ 'restartCount' : 0 ,
161
+ 'resurrectCount' : 0 ,
162
+ 'computeUnits' : 1 ,
163
+ },
164
+ 'options' : {
165
+ 'build' : 'asdf' ,
166
+ 'timeoutSecs' : 4 ,
167
+ 'memoryMbytes' : 1024 ,
168
+ 'diskMbytes' : 1024 ,
169
+ },
170
+ 'buildId' : 'hjkl' ,
171
+ 'defaultDatasetId' : 'hjkl' ,
172
+ 'defaultKeyValueStoreId' : 'hjkl' ,
173
+ 'defaultRequestQueueId' : 'hjkl' ,
174
+ 'containerUrl' : 'https://hjkl' ,
175
+ }
176
+ )
177
+
178
+ monkeypatch .setattr (Actor ._charging_manager , '_client' , mock_run_client )
179
+
146
180
async with Actor :
147
181
Actor .on (Event .PERSIST_STATE , log_persist_state )
148
182
await asyncio .sleep (2 )
0 commit comments