|
| 1 | +from datetime import datetime, timedelta, UTC |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from tests.test_telemetry_heartbeat_utils import heartbeat_delays_by_runtime |
| 7 | + |
| 8 | + |
| 9 | +pytestmark = pytest.mark.scenario("TEST_THE_TEST") |
| 10 | + |
| 11 | +BASE_TIME = datetime(2026, 1, 1, tzinfo=UTC) |
| 12 | + |
| 13 | + |
| 14 | +def _heartbeat(runtime_id: str, seq_id: int, offset: float) -> dict[str, Any]: |
| 15 | + timestamp = BASE_TIME + timedelta(seconds=offset) |
| 16 | + return { |
| 17 | + "request": { |
| 18 | + "timestamp_start": timestamp.isoformat(), |
| 19 | + "content": { |
| 20 | + "request_type": "app-heartbeat", |
| 21 | + "runtime_id": runtime_id, |
| 22 | + "seq_id": seq_id, |
| 23 | + }, |
| 24 | + }, |
| 25 | + "log_filename": f"{runtime_id}-{seq_id}-{offset}.json", |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +def test_fork_duplicate_does_not_affect_parent_cadence() -> None: |
| 30 | + messages = [ |
| 31 | + _heartbeat("parent", 1, 0), |
| 32 | + _heartbeat("parent", 2, 2), |
| 33 | + _heartbeat("parent", 2, 2.05), |
| 34 | + _heartbeat("parent", 3, 4), |
| 35 | + _heartbeat("parent", 4, 6), |
| 36 | + _heartbeat("child", 1, 2.1), |
| 37 | + _heartbeat("child", 2, 4.1), |
| 38 | + ] |
| 39 | + delays, heartbeat_counts = heartbeat_delays_by_runtime(messages) |
| 40 | + |
| 41 | + assert heartbeat_counts == {"parent": 4, "child": 2} |
| 42 | + assert set(delays) == {"parent"} |
| 43 | + assert delays["parent"] == pytest.approx([2.0, 2.0, 2.0]) |
| 44 | + |
| 45 | + |
| 46 | +def test_distinct_fast_heartbeats_remain_measurable() -> None: |
| 47 | + messages = [ |
| 48 | + _heartbeat("parent", 1, 0), |
| 49 | + _heartbeat("parent", 2, 1), |
| 50 | + _heartbeat("parent", 3, 2), |
| 51 | + _heartbeat("parent", 4, 3), |
| 52 | + ] |
| 53 | + delays, heartbeat_counts = heartbeat_delays_by_runtime(messages) |
| 54 | + |
| 55 | + assert heartbeat_counts == {"parent": 4} |
| 56 | + assert delays["parent"] == pytest.approx([1.0, 1.0, 1.0]) |
| 57 | + |
| 58 | + |
| 59 | +def test_short_lived_runtime_excluded_by_min_lifespan() -> None: |
| 60 | + """A short-lived runtime with too few samples shouldn't be measured at all: a shutdown |
| 61 | + heartbeat right before exit isn't a duplicate, so dedup can't drop it, and with only |
| 62 | + 2-3 samples it would skew the average. |
| 63 | + """ |
| 64 | + messages = [ |
| 65 | + _heartbeat("parent", 1, 0), |
| 66 | + _heartbeat("parent", 2, 2), |
| 67 | + _heartbeat("parent", 3, 4), |
| 68 | + _heartbeat("parent", 4, 6), |
| 69 | + _heartbeat("parent", 5, 8), |
| 70 | + # child lives ~2.05s: two normal-ish heartbeats, then an out-of-cadence one |
| 71 | + # right before it exits |
| 72 | + _heartbeat("child", 1, 0.1), |
| 73 | + _heartbeat("child", 2, 2.05), |
| 74 | + _heartbeat("child", 3, 2.12), |
| 75 | + ] |
| 76 | + delays, heartbeat_counts = heartbeat_delays_by_runtime(messages, min_lifespan=6.0) |
| 77 | + |
| 78 | + assert heartbeat_counts == {"parent": 5, "child": 3} |
| 79 | + assert set(delays) == {"parent"} |
| 80 | + |
| 81 | + |
| 82 | +def test_long_lived_runtime_anomaly_still_measured() -> None: |
| 83 | + """min_lifespan only excludes runtimes too short-lived to measure. A long-lived runtime |
| 84 | + with the same anomalous fast heartbeat is still measured, and can still fail. |
| 85 | + """ |
| 86 | + messages = [ |
| 87 | + _heartbeat("parent", 1, 0), |
| 88 | + _heartbeat("parent", 2, 2), |
| 89 | + _heartbeat("parent", 3, 4), |
| 90 | + _heartbeat("parent", 4, 6), |
| 91 | + _heartbeat("parent", 5, 8), |
| 92 | + _heartbeat("parent", 6, 8.05), # anomalous fast heartbeat, but lifespan is 8.05s |
| 93 | + ] |
| 94 | + delays, heartbeat_counts = heartbeat_delays_by_runtime(messages, min_lifespan=6.0) |
| 95 | + |
| 96 | + assert heartbeat_counts == {"parent": 6} |
| 97 | + assert delays["parent"] == pytest.approx([2.0, 2.0, 2.0, 2.0, 0.05]) |
0 commit comments