|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from crawlee.statistics import Statistics |
| 6 | + |
| 7 | + |
| 8 | +async def test_request_max_duration_tracks_maximum() -> None: |
| 9 | + """Test that request_max_duration correctly tracks the maximum duration, not the minimum.""" |
| 10 | + async with Statistics.with_default_state() as statistics: |
| 11 | + # Record a short request |
| 12 | + statistics.record_request_processing_start('request_1') |
| 13 | + statistics.record_request_processing_finish('request_1') |
| 14 | + first_duration = statistics.state.request_max_duration |
| 15 | + |
| 16 | + # Record a longer request |
| 17 | + statistics.record_request_processing_start('request_2') |
| 18 | + await asyncio.sleep(0.05) # 50ms delay |
| 19 | + statistics.record_request_processing_finish('request_2') |
| 20 | + second_duration = statistics.state.request_max_duration |
| 21 | + |
| 22 | + # The max duration should be updated to the longer request's duration |
| 23 | + assert second_duration is not None |
| 24 | + assert first_duration is not None |
| 25 | + assert second_duration >= first_duration |
| 26 | + assert second_duration.total_seconds() >= 0.05 |
| 27 | + |
| 28 | + # Record another short request - max should NOT decrease |
| 29 | + statistics.record_request_processing_start('request_3') |
| 30 | + statistics.record_request_processing_finish('request_3') |
| 31 | + third_duration = statistics.state.request_max_duration |
| 32 | + |
| 33 | + # The max duration should remain unchanged (still the longest request) |
| 34 | + assert third_duration == second_duration |
0 commit comments