Fixed flaky clock_unittest by using relative comparison.

ClockTest.NtpTime was checking that the two methods for getting the
system time are returning a value that is within a fixed error margin
(100 ms) of each other. Unfortunately, even such a wide margin was
sometimes exceeded on heavily loaded machines
(https://build.chromium.org/p/client.webrtc/builders/Mac%20Asan/builds/9235/steps/system_wrappers_unittests/logs/stdio).

This CL changes the test to sandwich clock->CurrentNtp() between
clock->CurrentNtpTimeInMilliseconds(). This way the test will pass no
matter how much time elapses between the two method calls, as long as
the clock is monotonic.

Repeated test runs showed that there may be 1 ms worth of rounding error
between the two methods of getting time, so we have to allow that.

BUG=None.

Review-Url: https://codereview.webrtc.org/2393063002
Cr-Commit-Position: refs/heads/master@{#14520}
This commit is contained in:
skvlad
2016-10-05 01:55:04 -07:00
committed by Commit bot
parent 81e007aee9
commit de3f844a20

View File

@ -18,11 +18,19 @@ TEST(ClockTest, NtpTime) {
Clock* clock = Clock::GetRealTimeClock();
uint32_t seconds;
uint32_t fractions;
// To ensure the test runs correctly even on a heavily loaded system, do not
// compare the seconds/fractions and millisecond values directly. Instead,
// we check that the NTP time is between the "milliseconds" values returned
// right before and right after the call.
// The comparison includes 1 ms of margin to account for the rounding error in
// the conversion.
int64_t milliseconds_lower_bound = clock->CurrentNtpInMilliseconds();
clock->CurrentNtp(seconds, fractions);
int64_t milliseconds = clock->CurrentNtpInMilliseconds();
EXPECT_GT(milliseconds / 1000, kNtpJan1970);
EXPECT_GE(milliseconds, Clock::NtpToMs(seconds, fractions));
EXPECT_NEAR(milliseconds, Clock::NtpToMs(seconds, fractions), 100);
int64_t milliseconds_upper_bound = clock->CurrentNtpInMilliseconds();
EXPECT_GT(milliseconds_lower_bound / 1000, kNtpJan1970);
EXPECT_LE(milliseconds_lower_bound - 1, Clock::NtpToMs(seconds, fractions));
EXPECT_GE(milliseconds_upper_bound + 1, Clock::NtpToMs(seconds, fractions));
}
} // namespace webrtc