This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new d1811b8a Fix timing assumptions in multidimensional latency recorder 
test (#3581)
d1811b8a is described below

commit d1811b8a88fa2d2d42b107b03fd7652cfa8c6c15
Author: Xiaofeng Wang <[email protected]>
AuthorDate: Mon Oct 5 16:46:30 2026 +0800

    Fix timing assumptions in multidimensional latency recorder test (#3581)
    
    * Fix timing assumptions in multidimensional latency recorder test
    
    Wait for asynchronous sampling with a bounded five-second timeout instead
    of assuming a one-second sleep is sufficient. Check positive QPS without
    assuming a minimum rate of seven requests per second.
    
    Use nonfatal assertions for independent metrics and restore modified
    flags automatically on all test exit paths.
    
    * Preserve sampled metrics in latency recorder test
    
    Cache each nonzero latency, maximum latency, and QPS observation while
    polling. Assert the cached values so a sampler tick cannot clear the
    one-second windows between polling and the assertions.
    
    Include QPS in the readiness check and preserve successful observations
    independently when sampler ticks occur between metric reads.
---
 test/bvar_multi_dimension_unittest.cpp | 59 +++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 23 deletions(-)

diff --git a/test/bvar_multi_dimension_unittest.cpp 
b/test/bvar_multi_dimension_unittest.cpp
index ace902c1..eb148ec3 100644
--- a/test/bvar_multi_dimension_unittest.cpp
+++ b/test/bvar_multi_dimension_unittest.cpp
@@ -397,18 +397,8 @@ TEST_F(MultiDimensionTest, get_description) {
 }
 
 TEST_F(MultiDimensionTest, mlatencyrecorder) {
-    std::string old_bvar_dump_interval;
-    std::string old_mbvar_dump;
-    std::string old_bvar_latency_p1;
-    std::string old_bvar_latency_p2;
-    std::string old_bvar_latency_p3;
-
-    GFLAGS_NAMESPACE::GetCommandLineOption("bvar_dump_interval", 
&old_bvar_dump_interval);
-    GFLAGS_NAMESPACE::GetCommandLineOption("mbvar_dump", &old_mbvar_dump);
-    GFLAGS_NAMESPACE::GetCommandLineOption("bvar_latency_p1", 
&old_bvar_latency_p1);
-    GFLAGS_NAMESPACE::GetCommandLineOption("bvar_latency_p2", 
&old_bvar_latency_p2);
-    GFLAGS_NAMESPACE::GetCommandLineOption("bvar_latency_p3", 
&old_bvar_latency_p3);
-
+    // Restore flags even if a fatal assertion returns from the test.
+    GFLAGS_NAMESPACE::FlagSaver flag_saver;
     GFLAGS_NAMESPACE::SetCommandLineOption("bvar_dump_interval", "1");
     GFLAGS_NAMESPACE::SetCommandLineOption("mbvar_dump", "true");
     GFLAGS_NAMESPACE::SetCommandLineOption("bvar_latency_p1", "60");
@@ -420,17 +410,40 @@ TEST_F(MultiDimensionTest, mlatencyrecorder) {
     bvar::LatencyRecorder* my_latencyrecorder = 
my_mlatencyrecorder.get_stats(labels_value);
     ASSERT_TRUE(my_latencyrecorder);
     *my_latencyrecorder << 1 << 2 << 3 << 4 << 5 << 6 << 7;
-    sleep(1);
-    ASSERT_EQ(4, my_latencyrecorder->latency());
-    ASSERT_EQ(7, my_latencyrecorder->max_latency());
-    ASSERT_LE(7, my_latencyrecorder->qps());
-    ASSERT_EQ(7, my_latencyrecorder->count());
-
-    GFLAGS_NAMESPACE::SetCommandLineOption("bvar_dump_interval", 
old_bvar_dump_interval.c_str());
-    GFLAGS_NAMESPACE::SetCommandLineOption("mbvar_dump", 
old_mbvar_dump.c_str());
-    GFLAGS_NAMESPACE::SetCommandLineOption("bvar_latency_p1", 
old_bvar_latency_p1.c_str());
-    GFLAGS_NAMESPACE::SetCommandLineOption("bvar_latency_p2", 
old_bvar_latency_p2.c_str());
-    GFLAGS_NAMESPACE::SetCommandLineOption("bvar_latency_p3", 
old_bvar_latency_p3.c_str());
+    EXPECT_EQ(7, my_latencyrecorder->count());
+
+    // Sampling runs once per second. Allow five seconds for scheduling
+    // delays on busy CI runners while keeping the wait bounded.
+    const int64_t sampling_timeout_us = 5 * 1000000L;
+    const int64_t deadline =
+        butil::monotonic_time_us() + sampling_timeout_us;
+    int64_t latency = 0;
+    int64_t max_latency = 0;
+    int64_t qps = 0;
+    // Keep each nonzero observation: the next sampler tick can clear
+    // these one-second windows before the other metrics are read.
+    while ((latency == 0 || max_latency == 0 || qps == 0) &&
+           butil::monotonic_time_us() < deadline) {
+        if (latency == 0) {
+            latency = my_latencyrecorder->latency();
+        }
+        if (max_latency == 0) {
+            max_latency = my_latencyrecorder->max_latency();
+        }
+        if (qps == 0) {
+            qps = my_latencyrecorder->qps();
+        }
+        if (latency != 0 && max_latency != 0 && qps != 0) {
+            break;
+        }
+        usleep(10000);
+    }
+
+    EXPECT_EQ(4, latency);
+    EXPECT_EQ(7, max_latency);
+    // QPS uses the actual sampling interval and randomized rounding.
+    // Seven requests can yield fewer than seven requests per second.
+    EXPECT_GT(qps, 0);
 }
 
 TEST_F(MultiDimensionTest, mstatus) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to