MCPcopy Create free account
hub / github.com/apify/crawlee-python / test_get_cpu_sample

Function test_get_cpu_sample

tests/unit/_autoscaling/test_snapshotter.py:159–193  ·  view source on GitHub ↗
(
    snapshotter: Snapshotter, event_manager: LocalEventManager, default_memory_info: MemoryInfo
)

Source from the content-addressed store, hash-verified

157
158@pytest.mark.run_alone
159async def test_get_cpu_sample(
160 snapshotter: Snapshotter, event_manager: LocalEventManager, default_memory_info: MemoryInfo
161) -> None:
162 now = datetime.now(timezone.utc)
163 snapshotter._SNAPSHOT_HISTORY = timedelta(hours=10) # Extend history for testing
164
165 events_data = [
166 EventSystemInfoData(
167 cpu_info=CpuInfo(
168 used_ratio=0.5,
169 created_at=now - timedelta(hours=delta),
170 ),
171 memory_info=default_memory_info,
172 )
173 for delta in range(5, 0, -1)
174 ]
175 for event_data in events_data:
176 event_manager.emit(event=Event.SYSTEM_INFO, event_data=event_data)
177 await event_manager.wait_for_all_listeners_to_complete()
178
179 # When no sample duration is provided it should return all snapshots
180 samples = snapshotter.get_cpu_sample()
181 assert len(samples) == len(events_data)
182
183 duration = timedelta(hours=0.5)
184 samples = snapshotter.get_cpu_sample(duration)
185 assert len(samples) == 1
186
187 duration = timedelta(hours=2.5)
188 samples = snapshotter.get_cpu_sample(duration)
189 assert len(samples) == 3
190
191 duration = timedelta(hours=10)
192 samples = snapshotter.get_cpu_sample(duration)
193 assert len(samples) == len(events_data)
194
195
196async def test_methods_raise_error_when_not_active() -> None:

Callers

nothing calls this directly

Calls 5

EventSystemInfoDataClass · 0.90
CpuInfoClass · 0.90
get_cpu_sampleMethod · 0.80
emitMethod · 0.45

Tested by

no test coverage detected