From 8204737ea693b213c7367d0ea89498bb2baf0462 Mon Sep 17 00:00:00 2001 From: Andreas Wrede Date: Mon, 17 Aug 2026 07:28:36 -0400 Subject: [PATCH] fix: use the latest RTT sample, not the oldest, in host info API --- hbd/server/http.py | 2 +- tests/test_http_host_info.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/hbd/server/http.py b/hbd/server/http.py index c819529..2fd217a 100644 --- a/hbd/server/http.py +++ b/hbd/server/http.py @@ -275,7 +275,7 @@ def _build_host_info(host, threshold_checker=None) -> dict: "family": getattr(conn, "afam", family), "addr": getattr(conn, "addr", ""), "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), "lastbeat": getattr(conn, "lastbeat", None), } diff --git a/tests/test_http_host_info.py b/tests/test_http_host_info.py index c38915d..6f22e46 100644 --- a/tests/test_http_host_info.py +++ b/tests/test_http_host_info.py @@ -1,5 +1,4 @@ """Tests for _build_host_info helper in http.py.""" -import pytest from unittest.mock import MagicMock 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: - def __init__(self, afam, addr, state, rtt, statetime, lastbeat): + def __init__(self, afam, addr, state, rtt, statetime, lastbeat, rtts=None): self.afam = afam self.addr = addr self.state = state - self.rtts = [rtt] + self.rtts = rtts if rtts is not None else [rtt] self.statetime = statetime self.lastbeat = lastbeat @@ -200,3 +199,14 @@ def test_build_host_info_connections_empty(): host = _FakeHost() result = _build_host_info(host) 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