def tcp_requests_received(ns: NamedInstance) -> int:
ns.rndc("stats")
- stats_file = ns.directory / "named.stats"
- last_count: int | None = None
-
- with open(stats_file, "r", encoding="utf-8") as stats:
- for line in stats:
- if "TCP requests received" in line:
- last_count = int(line.split()[0])
-
- assert last_count is not None, f"'TCP requests received' not found in {stats_file}"
- return last_count
+ stats = isctest.text.TextFile(str(ns.directory / "named.stats"))
+ matches = stats.grep("TCP requests received")
+ assert matches, f"'TCP requests received' not found in {stats}"
+ # `rndc stats` appends to the file; only the last occurrence is current
+ return int(matches[-1].string.split()[0])
def tcp_status(ns: NamedInstance) -> TcpStatus: