Add TimeMicrosToNtp to calculate current NtpTime without Clock

Bug: webrtc:6733, webrtc:8239
Change-Id: I8ac4464cd7a7ec2b2dbad44430f1141a80ba39c1
Reviewed-on: https://webrtc-review.googlesource.com/25541
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20904}
This commit is contained in:
Danil Chapovalov
2017-11-28 10:26:54 +01:00
committed by Commit Bot
parent fb09eeb8f1
commit 2492984441
3 changed files with 84 additions and 3 deletions

View File

@ -9,10 +9,49 @@
*/
#include "modules/rtp_rtcp/source/time_util.h"
#include "rtc_base/fakeclock.h"
#include "rtc_base/timeutils.h"
#include "system_wrappers/include/clock.h"
#include "test/gtest.h"
namespace webrtc {
TEST(TimeUtilTest, TimeMicrosToNtpMatchRealTimeClockInitially) {
Clock* legacy_clock = Clock::GetRealTimeClock();
NtpTime before_legacy_time = TimeMicrosToNtp(rtc::TimeMicros());
NtpTime legacy_time = legacy_clock->CurrentNtpTime();
NtpTime after_legacy_time = TimeMicrosToNtp(rtc::TimeMicros());
// This test will fail once every 136 years, when NtpTime wraparound.
// More often than that, it will fail if system adjust ntp time while test
// is running.
// To mitigate ntp time adjustment and potentional different precisions of
// Clock and TimeMicrosToNtp, relax expectation by a millisecond.
EXPECT_GE(legacy_time.ToMs(), before_legacy_time.ToMs() - 1);
EXPECT_LE(legacy_time.ToMs(), after_legacy_time.ToMs() + 1);
}
TEST(TimeUtilTest, TimeMicrosToNtpDoesntChangeBetweenRuns) {
rtc::ScopedFakeClock clock;
// TimeMicrosToNtp is not pure: it behave differently between different
// execution of the program, but should behave same during same execution.
const int64_t time_us = 12345;
clock.SetTimeMicros(2);
NtpTime time_ntp = TimeMicrosToNtp(time_us);
clock.SetTimeMicros(time_us);
EXPECT_EQ(TimeMicrosToNtp(time_us), time_ntp);
clock.SetTimeMicros(1000000);
EXPECT_EQ(TimeMicrosToNtp(time_us), time_ntp);
}
TEST(TimeUtilTest, TimeMicrosToNtpKeepsIntervals) {
rtc::ScopedFakeClock clock;
NtpTime time_ntp1 = TimeMicrosToNtp(rtc::TimeMicros());
clock.AdvanceTimeMicros(20000);
NtpTime time_ntp2 = TimeMicrosToNtp(rtc::TimeMicros());
EXPECT_EQ(time_ntp2.ToMs() - time_ntp1.ToMs(), 20);
}
TEST(TimeUtilTest, CompactNtp) {
const uint32_t kNtpSec = 0x12345678;
const uint32_t kNtpFrac = 0x23456789;