gfe-relnote: (n/a) Add a wrapper macro for quic::test::ExpectApproxEq for better error report in tests. Test only. The new EXPECT_APPROX_EQ macro shows the filename:linenumber of the callsite, the existing function does not. PiperOrigin-RevId: 246420815 Change-Id: Ife5706cf77261a4f1d120946f4d0acd400076afd
diff --git a/quic/core/congestion_control/bbr_sender_test.cc b/quic/core/congestion_control/bbr_sender_test.cc index 3d40e0a..ec60ecf 100644 --- a/quic/core/congestion_control/bbr_sender_test.cc +++ b/quic/core/congestion_control/bbr_sender_test.cc
@@ -217,8 +217,8 @@ ASSERT_FALSE(sender_->ExportDebugState().is_at_full_bandwidth); DoSimpleTransfer(1024 * 1024, QuicTime::Delta::FromSeconds(15)); EXPECT_EQ(BbrSender::PROBE_BW, sender_->ExportDebugState().mode); - ExpectApproxEq(kTestLinkBandwidth, - sender_->ExportDebugState().max_bandwidth, 0.02f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.02f); } // Send |bytes|-sized bursts of data |number_of_bursts| times, waiting for @@ -276,8 +276,8 @@ // Verify that pacing rate is based on the initial RTT. QuicBandwidth expected_pacing_rate = QuicBandwidth::FromBytesAndTimeDelta( 2.885 * kDefaultWindowTCP, rtt_stats_->initial_rtt()); - ExpectApproxEq(expected_pacing_rate.ToBitsPerSecond(), - sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); + EXPECT_APPROX_EQ(expected_pacing_rate.ToBitsPerSecond(), + sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); ASSERT_GE(kTestBdp, kDefaultWindowTCP + kDefaultTCPMSS); @@ -288,7 +288,7 @@ // The margin here is quite high, since there exists a possibility that the // connection just exited high gain cycle. - ExpectApproxEq(kTestRtt, rtt_stats_->smoothed_rtt(), 0.2f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->smoothed_rtt(), 0.2f); } // Test a simple transfer in a situation when the buffer is less than BDP. @@ -297,8 +297,8 @@ DoSimpleTransfer(12 * 1024 * 1024, QuicTime::Delta::FromSeconds(30)); EXPECT_EQ(BbrSender::PROBE_BW, sender_->ExportDebugState().mode); - ExpectApproxEq(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth, - 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); EXPECT_GE(bbr_sender_.connection()->GetStats().packets_lost, 0u); EXPECT_FALSE(sender_->ExportDebugState().last_sample_is_app_limited); } @@ -363,7 +363,7 @@ // The margin here is high, because the aggregation greatly increases // smoothed rtt. EXPECT_GE(kTestRtt * 4, rtt_stats_->smoothed_rtt()); - ExpectApproxEq(kTestRtt, rtt_stats_->min_rtt(), 0.2f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->min_rtt(), 0.2f); } // Test a simple long data transfer with 2 rtts of aggregation. @@ -398,7 +398,7 @@ // The margin here is high, because the aggregation greatly increases // smoothed rtt. EXPECT_GE(kTestRtt * 2, rtt_stats_->smoothed_rtt()); - ExpectApproxEq(kTestRtt, rtt_stats_->min_rtt(), 0.1f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->min_rtt(), 0.1f); } // Test a simple long data transfer with 2 rtts of aggregation. @@ -425,7 +425,7 @@ // The margin here is high, because the aggregation greatly increases // smoothed rtt. EXPECT_GE(kTestRtt * 4, rtt_stats_->smoothed_rtt()); - ExpectApproxEq(kTestRtt, rtt_stats_->min_rtt(), 0.12f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->min_rtt(), 0.12f); } // Test a simple long data transfer with 2 rtts of aggregation. @@ -452,7 +452,7 @@ // The margin here is high, because the aggregation greatly increases // smoothed rtt. EXPECT_GE(kTestRtt * 4, rtt_stats_->smoothed_rtt()); - ExpectApproxEq(kTestRtt, rtt_stats_->min_rtt(), 0.12f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->min_rtt(), 0.12f); } // Test the number of losses incurred by the startup phase in a situation when @@ -525,8 +525,8 @@ SendBursts(20, 512, QuicTime::Delta::FromSeconds(3)); EXPECT_TRUE(sender_->ExportDebugState().last_sample_is_app_limited); - ExpectApproxEq(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth, - 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); } // Verify the behavior of the algorithm in the case when the connection sends @@ -538,8 +538,8 @@ EXPECT_TRUE(sender_->ExportDebugState().last_sample_is_app_limited); DriveOutOfStartup(); - ExpectApproxEq(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth, - 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); EXPECT_FALSE(sender_->ExportDebugState().last_sample_is_app_limited); } @@ -566,8 +566,8 @@ timeout); ASSERT_TRUE(simulator_result); ASSERT_EQ(BbrSender::DRAIN, sender_->ExportDebugState().mode); - ExpectApproxEq(sender_->BandwidthEstimate() * (1 / 2.885f), - sender_->PacingRate(0), 0.01f); + EXPECT_APPROX_EQ(sender_->BandwidthEstimate() * (1 / 2.885f), + sender_->PacingRate(0), 0.01f); // BBR uses CWND gain of 2.88 during STARTUP, hence it will fill the buffer // with approximately 1.88 BDPs. Here, we use 1.5 to give some margin for // error. @@ -576,7 +576,7 @@ // Observe increased RTT due to bufferbloat. const QuicTime::Delta queueing_delay = kTestLinkBandwidth.TransferTime(queue->bytes_queued()); - ExpectApproxEq(kTestRtt + queueing_delay, rtt_stats_->latest_rtt(), 0.1f); + EXPECT_APPROX_EQ(kTestRtt + queueing_delay, rtt_stats_->latest_rtt(), 0.1f); // Transition to the drain phase and verify that it makes the queue // have at most a BDP worth of packets. @@ -602,7 +602,7 @@ ASSERT_TRUE(simulator_result); // Observe the bufferbloat go away. - ExpectApproxEq(kTestRtt, rtt_stats_->smoothed_rtt(), 0.1f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->smoothed_rtt(), 0.1f); } // Verify that the DRAIN phase works correctly. @@ -641,7 +641,7 @@ // Observe increased RTT due to bufferbloat. const QuicTime::Delta queueing_delay = kTestLinkBandwidth.TransferTime(queue->bytes_queued()); - ExpectApproxEq(kTestRtt + queueing_delay, rtt_stats_->latest_rtt(), 0.1f); + EXPECT_APPROX_EQ(kTestRtt + queueing_delay, rtt_stats_->latest_rtt(), 0.1f); // Transition to the drain phase and verify that it makes the queue // have at most a BDP worth of packets. @@ -667,7 +667,7 @@ ASSERT_TRUE(simulator_result); // Observe the bufferbloat go away. - ExpectApproxEq(kTestRtt, rtt_stats_->smoothed_rtt(), 0.1f); + EXPECT_APPROX_EQ(kTestRtt, rtt_stats_->smoothed_rtt(), 0.1f); } // Verify that the connection enters and exits PROBE_RTT correctly. @@ -834,8 +834,8 @@ SendBursts(5, target_bandwidth * burst_interval, burst_interval); EXPECT_EQ(BbrSender::PROBE_BW, sender_->ExportDebugState().mode); EXPECT_EQ(0, sender_->ExportDebugState().gain_cycle_index); - ExpectApproxEq(kTestLinkBandwidth, - sender_->ExportDebugState().max_bandwidth, 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); } // Now that in-flight is almost zero and the pacing gain is still above 1, @@ -1170,8 +1170,8 @@ // Verify that pacing rate is based on the initial RTT. QuicBandwidth expected_pacing_rate = QuicBandwidth::FromBytesAndTimeDelta( 2.773 * kDefaultWindowTCP, rtt_stats_->initial_rtt()); - ExpectApproxEq(expected_pacing_rate.ToBitsPerSecond(), - sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); + EXPECT_APPROX_EQ(expected_pacing_rate.ToBitsPerSecond(), + sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); // Run until the full bandwidth is reached and check how many rounds it was. bbr_sender_.AddBytesToTransfer(12 * 1024 * 1024); @@ -1181,8 +1181,8 @@ ASSERT_TRUE(simulator_result); EXPECT_EQ(BbrSender::DRAIN, sender_->ExportDebugState().mode); EXPECT_EQ(3u, sender_->ExportDebugState().rounds_without_bandwidth_gain); - ExpectApproxEq(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth, - 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); EXPECT_EQ(0u, bbr_sender_.connection()->GetStats().packets_lost); EXPECT_FALSE(sender_->ExportDebugState().last_sample_is_app_limited); } @@ -1198,8 +1198,8 @@ // Verify that pacing rate is based on the initial RTT. QuicBandwidth expected_pacing_rate = QuicBandwidth::FromBytesAndTimeDelta( 2.885 * kDefaultWindowTCP, rtt_stats_->initial_rtt()); - ExpectApproxEq(expected_pacing_rate.ToBitsPerSecond(), - sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); + EXPECT_APPROX_EQ(expected_pacing_rate.ToBitsPerSecond(), + sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); // Run until the full bandwidth is reached and check how many rounds it was. bbr_sender_.AddBytesToTransfer(12 * 1024 * 1024); @@ -1209,8 +1209,8 @@ ASSERT_TRUE(simulator_result); EXPECT_EQ(BbrSender::DRAIN, sender_->ExportDebugState().mode); EXPECT_EQ(3u, sender_->ExportDebugState().rounds_without_bandwidth_gain); - ExpectApproxEq(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth, - 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); EXPECT_EQ(0u, bbr_sender_.connection()->GetStats().packets_lost); EXPECT_FALSE(sender_->ExportDebugState().last_sample_is_app_limited); // Expect an SRTT less than 2.7 * Min RTT on exit from STARTUP. @@ -1231,8 +1231,8 @@ // Verify that pacing rate is based on the initial RTT. QuicBandwidth expected_pacing_rate = QuicBandwidth::FromBytesAndTimeDelta( 2.885 * kDefaultWindowTCP, rtt_stats_->initial_rtt()); - ExpectApproxEq(expected_pacing_rate.ToBitsPerSecond(), - sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); + EXPECT_APPROX_EQ(expected_pacing_rate.ToBitsPerSecond(), + sender_->PacingRate(0).ToBitsPerSecond(), 0.01f); // Run until the full bandwidth is reached and check how many rounds it was. bbr_sender_.AddBytesToTransfer(12 * 1024 * 1024); @@ -1242,8 +1242,8 @@ ASSERT_TRUE(simulator_result); EXPECT_EQ(BbrSender::DRAIN, sender_->ExportDebugState().mode); EXPECT_EQ(3u, sender_->ExportDebugState().rounds_without_bandwidth_gain); - ExpectApproxEq(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth, - 0.01f); + EXPECT_APPROX_EQ(kTestLinkBandwidth, + sender_->ExportDebugState().max_bandwidth, 0.01f); EXPECT_EQ(0u, bbr_sender_.connection()->GetStats().packets_lost); EXPECT_FALSE(sender_->ExportDebugState().last_sample_is_app_limited); } @@ -1281,7 +1281,7 @@ kTestRtt); EXPECT_EQ(kTestLinkBandwidth, sender_->ExportDebugState().max_bandwidth); EXPECT_EQ(kTestLinkBandwidth, sender_->BandwidthEstimate()); - ExpectApproxEq(kTestRtt, sender_->ExportDebugState().min_rtt, 0.01f); + EXPECT_APPROX_EQ(kTestRtt, sender_->ExportDebugState().min_rtt, 0.01f); DriveOutOfStartup(); }
diff --git a/quic/test_tools/quic_test_utils.h b/quic/test_tools/quic_test_utils.h index 08337da..91f25d5 100644 --- a/quic/test_tools/quic_test_utils.h +++ b/quic/test_tools/quic_test_utils.h
@@ -1128,7 +1128,8 @@ // Verifies that the relative error of |actual| with respect to |expected| is // no more than |margin|. - +// Please use EXPECT_APPROX_EQ, a wrapper around this function, for better error +// report. template <typename T> void ExpectApproxEq(T expected, T actual, float relative_margin) { // If |relative_margin| > 1 and T is an unsigned type, the comparison will @@ -1138,10 +1139,16 @@ T absolute_margin = expected * relative_margin; - EXPECT_GE(expected + absolute_margin, actual); - EXPECT_LE(expected - absolute_margin, actual); + EXPECT_GE(expected + absolute_margin, actual) << "actual value too big"; + EXPECT_LE(expected - absolute_margin, actual) << "actual value too small"; } +#define EXPECT_APPROX_EQ(expected, actual, relative_margin) \ + do { \ + SCOPED_TRACE(testing::Message() << "relative_margin:" << relative_margin); \ + quic::test::ExpectApproxEq(expected, actual, relative_margin); \ + } while (0) + template <typename T> QuicHeaderList AsHeaderList(const T& container) { QuicHeaderList l;
diff --git a/quic/test_tools/quic_test_utils_test.cc b/quic/test_tools/quic_test_utils_test.cc index 8c30335..31e4804 100644 --- a/quic/test_tools/quic_test_utils_test.cc +++ b/quic/test_tools/quic_test_utils_test.cc
@@ -22,29 +22,29 @@ } TEST_F(QuicTestUtilsTest, BasicApproxEq) { - ExpectApproxEq(10, 10, 1e-6f); - ExpectApproxEq(1000, 1001, 0.01f); - EXPECT_NONFATAL_FAILURE(ExpectApproxEq(1000, 1100, 0.01f), ""); + EXPECT_APPROX_EQ(10, 10, 1e-6f); + EXPECT_APPROX_EQ(1000, 1001, 0.01f); + EXPECT_NONFATAL_FAILURE(EXPECT_APPROX_EQ(1000, 1100, 0.01f), ""); - ExpectApproxEq(64, 31, 0.55f); - EXPECT_NONFATAL_FAILURE(ExpectApproxEq(31, 64, 0.55f), ""); + EXPECT_APPROX_EQ(64, 31, 0.55f); + EXPECT_NONFATAL_FAILURE(EXPECT_APPROX_EQ(31, 64, 0.55f), ""); } TEST_F(QuicTestUtilsTest, QuicTimeDelta) { - ExpectApproxEq(QuicTime::Delta::FromMicroseconds(1000), - QuicTime::Delta::FromMicroseconds(1003), 0.01f); + EXPECT_APPROX_EQ(QuicTime::Delta::FromMicroseconds(1000), + QuicTime::Delta::FromMicroseconds(1003), 0.01f); EXPECT_NONFATAL_FAILURE( - ExpectApproxEq(QuicTime::Delta::FromMicroseconds(1000), - QuicTime::Delta::FromMicroseconds(1200), 0.01f), + EXPECT_APPROX_EQ(QuicTime::Delta::FromMicroseconds(1000), + QuicTime::Delta::FromMicroseconds(1200), 0.01f), ""); } TEST_F(QuicTestUtilsTest, QuicBandwidth) { - ExpectApproxEq(QuicBandwidth::FromBytesPerSecond(1000), - QuicBandwidth::FromBitsPerSecond(8005), 0.01f); + EXPECT_APPROX_EQ(QuicBandwidth::FromBytesPerSecond(1000), + QuicBandwidth::FromBitsPerSecond(8005), 0.01f); EXPECT_NONFATAL_FAILURE( - ExpectApproxEq(QuicBandwidth::FromBytesPerSecond(1000), - QuicBandwidth::FromBitsPerSecond(9005), 0.01f), + EXPECT_APPROX_EQ(QuicBandwidth::FromBytesPerSecond(1000), + QuicBandwidth::FromBitsPerSecond(9005), 0.01f), ""); }
diff --git a/quic/test_tools/simulator/simulator_test.cc b/quic/test_tools/simulator/simulator_test.cc index 33ad564..1e7a8ce 100644 --- a/quic/test_tools/simulator/simulator_test.cc +++ b/quic/test_tools/simulator/simulator_test.cc
@@ -224,7 +224,7 @@ const QuicTime end_time = simulator.GetClock()->Now(); const QuicBandwidth observed_bandwidth = QuicBandwidth::FromBytesAndTimeDelta( saturator_a.bytes_transmitted(), end_time - start_time); - test::ExpectApproxEq(link.bandwidth(), observed_bandwidth, 0.01f); + EXPECT_APPROX_EQ(link.bandwidth(), observed_bandwidth, 0.01f); } // Accepts packets and stores them internally. @@ -683,15 +683,15 @@ // Ensure we've transmitted the amount of data we expected. for (auto* saturator : {&saturator1, &saturator2}) { - test::ExpectApproxEq(bandwidth * simulation_time, - saturator->bytes_transmitted(), 0.01f); + EXPECT_APPROX_EQ(bandwidth * simulation_time, + saturator->bytes_transmitted(), 0.01f); } // Check that only one direction is throttled. - test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, - saturator2.counter()->bytes(), 0.1f); - test::ExpectApproxEq(saturator2.bytes_transmitted(), - saturator1.counter()->bytes(), 0.1f); + EXPECT_APPROX_EQ(saturator1.bytes_transmitted() / 4, + saturator2.counter()->bytes(), 0.1f); + EXPECT_APPROX_EQ(saturator2.bytes_transmitted(), + saturator1.counter()->bytes(), 0.1f); } // Ensure that a larger burst is allowed when the policed saturator exits @@ -740,14 +740,14 @@ simulator.RunFor(2 * base_propagation_delay); // Expect the burst to pass without losses. - test::ExpectApproxEq(saturator1.bytes_transmitted(), - saturator2.counter()->bytes(), 0.1f); + EXPECT_APPROX_EQ(saturator1.bytes_transmitted(), + saturator2.counter()->bytes(), 0.1f); // Expect subsequent traffic to be policed. saturator1.Resume(); simulator.RunFor(QuicTime::Delta::FromSeconds(10)); - test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, - saturator2.counter()->bytes(), 0.1f); + EXPECT_APPROX_EQ(saturator1.bytes_transmitted() / 4, + saturator2.counter()->bytes(), 0.1f); } // Test that the packet aggregation support in queues work.