From b139d18d58afd73c35baf3c5dce6e14035bc430a Mon Sep 17 00:00:00 2001 From: zhuweiye Date: Sun, 22 Nov 2026 09:22:33 +0800 Subject: [PATCH] =?UTF-8?q?test(metrics):=20=E7=AD=89=20on=5Fsent=20?= =?UTF-8?q?=E6=8A=8A=E5=9C=A8=E9=80=94=20Gauge=20=E5=87=8F=E5=9B=9E?= =?UTF-8?q?=E5=8E=BB=EF=BC=88CI=20=E5=81=B6=E5=8F=91=E5=81=87=E7=BA=A2?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/functional/web_app_metrics_func.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/functional/web_app_metrics_func.cpp b/tests/functional/web_app_metrics_func.cpp index b50db1b5..8f39dd75 100644 --- a/tests/functional/web_app_metrics_func.cpp +++ b/tests/functional/web_app_metrics_func.cpp @@ -232,11 +232,17 @@ void test_endpoints() { check(get(port, "/hello").status == 200, "业务请求 200"); check(get(port, "/missing").status == 404, "未命中路由 404"); - // 先直接渲染一次注册表:此刻没有在途请求(前面的响应都已发完),在途 Gauge - // 必须是 0。`/metrics` 那个 HTTP 请求自己在飞的时候会是 1 —— 那是自计数, - // 不是泄漏,下面那条断言照实写 1。 - check(contains(t.reg.render(), "http_requests_in_flight 0"), - "metrics(直接读):在途 Gauge 为 0"); + // 先直接渲染注册表:没有在途请求时在途 Gauge 必须是 0。 + // **要轮询等待**:客户端收到响应 ≠ 服务端的 `on_sent` 已经跑过(它在下一次 + // 循环迭代里才减回去),CI 上出现过刚收完就断言、偶发读到 1 的假红。 + std::string direct = t.reg.render(); + for (int i = 0; i < 200 && !contains(direct, "http_requests_in_flight 0"); + ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + direct = t.reg.render(); + } + check(contains(direct, "http_requests_in_flight 0"), + "metrics(直接读):在途 Gauge 归零"); const reply m = get(port, "/metrics"); check(m.status == 200, "metrics:200");