|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import asyncio
|
4 |
| -from typing import TYPE_CHECKING |
| 4 | +from typing import TYPE_CHECKING, cast |
5 | 5 |
|
6 | 6 | import pytest
|
7 | 7 |
|
|
14 | 14 | from crawlee.storages import RequestQueue
|
15 | 15 |
|
16 | 16 | from .conftest import MakeActorFunction, RunActorFunction
|
| 17 | + from apify.storage_clients._apify._request_queue_client import ApifyRequestQueueMetadata |
17 | 18 |
|
18 | 19 |
|
19 | 20 | async def test_add_and_fetch_requests(
|
@@ -1278,3 +1279,28 @@ async def test_request_queue_not_had_multiple_clients(
|
1278 | 1279 | api_response = await api_client.get()
|
1279 | 1280 | assert api_response
|
1280 | 1281 | assert api_response['hadMultipleClients'] is False
|
| 1282 | + |
| 1283 | + |
| 1284 | +async def test_request_queue_has_stats(request_queue_force_cloud: RequestQueue) -> None: |
| 1285 | + """Test that Apify based request queue has stats in metadata.""" |
| 1286 | + |
| 1287 | + add_request_count = 3 |
| 1288 | + read_request_count = 2 |
| 1289 | + |
| 1290 | + # Two calls to API to create situation where different `client_key` can set `had_multiple_clients` to True |
| 1291 | + |
| 1292 | + await request_queue_force_cloud.add_requests( |
| 1293 | + [Request.from_url(f'http://example.com/{i}') for i in range(add_request_count)] |
| 1294 | + ) |
| 1295 | + for _ in range(read_request_count): |
| 1296 | + await request_queue_force_cloud.get_request(Request.from_url('http://example.com/1').unique_key) |
| 1297 | + |
| 1298 | + # Wait for stats to become stable |
| 1299 | + await asyncio.sleep(10) |
| 1300 | + |
| 1301 | + metadata = await request_queue_force_cloud.get_metadata() |
| 1302 | + |
| 1303 | + assert hasattr(metadata, 'stats') |
| 1304 | + apify_metadata = cast('ApifyRequestQueueMetadata', metadata) |
| 1305 | + assert apify_metadata.stats.read_count == read_request_count |
| 1306 | + assert apify_metadata.stats.write_count == add_request_count # Added one request |
0 commit comments