fix: use the latest RTT sample, not the oldest, in host info API

This commit is contained in:
2026-08-17 07:28:36 -04:00
parent 5b2fc8fd46
commit 8204737ea6
2 changed files with 14 additions and 4 deletions
+1 -1
View File
@@ -275,7 +275,7 @@ def _build_host_info(host, threshold_checker=None) -> dict:
"family": getattr(conn, "afam", family), "family": getattr(conn, "afam", family),
"addr": getattr(conn, "addr", ""), "addr": getattr(conn, "addr", ""),
"state": getattr(conn, "state", ""), "state": getattr(conn, "state", ""),
"rtt": (getattr(conn, "rtts", None) or [None])[0], "rtt": (getattr(conn, "rtts", None) or [None])[-1],
"statetime": getattr(conn, "statetime", None), "statetime": getattr(conn, "statetime", None),
"lastbeat": getattr(conn, "lastbeat", None), "lastbeat": getattr(conn, "lastbeat", None),
} }
+13 -3
View File
@@ -1,5 +1,4 @@
"""Tests for _build_host_info helper in http.py.""" """Tests for _build_host_info helper in http.py."""
import pytest
from unittest.mock import MagicMock from unittest.mock import MagicMock
from hbd.server.http import _build_host_info from hbd.server.http import _build_host_info
@@ -175,11 +174,11 @@ def test_build_host_info_covers_empty_when_exact_matches_only():
class _FakeConnFull: class _FakeConnFull:
def __init__(self, afam, addr, state, rtt, statetime, lastbeat): def __init__(self, afam, addr, state, rtt, statetime, lastbeat, rtts=None):
self.afam = afam self.afam = afam
self.addr = addr self.addr = addr
self.state = state self.state = state
self.rtts = [rtt] self.rtts = rtts if rtts is not None else [rtt]
self.statetime = statetime self.statetime = statetime
self.lastbeat = lastbeat self.lastbeat = lastbeat
@@ -200,3 +199,14 @@ def test_build_host_info_connections_empty():
host = _FakeHost() host = _FakeHost()
result = _build_host_info(host) result = _build_host_info(host)
assert result["connections"] == [] assert result["connections"] == []
def test_build_host_info_connection_rtt_uses_latest_sample():
"""rtt should reflect the most recent sample, not the oldest retained one."""
host = _FakeHost(connections={
"IPv4": _FakeConnFull("IPv4", "10.0.0.5", "up", None, 1000.0, 2000.0,
rtts=[100.0, 50.0, 14.2]),
})
result = _build_host_info(host)
conns = {c["family"]: c for c in result["connections"]}
assert conns["IPv4"]["rtt"] == 14.2