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}
diff --git a/modules/rtp_rtcp/source/time_util_unittest.cc b/modules/rtp_rtcp/source/time_util_unittest.cc
index 6ff55dd..cdf2712 100644
--- a/modules/rtp_rtcp/source/time_util_unittest.cc
+++ b/modules/rtp_rtcp/source/time_util_unittest.cc
@@ -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;