blob: ced6e357c4f90a864d6e3030f8d2a4618df9da23 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <memory>
14
Artem Titovc374d112022-06-16 21:27:45 +020015#include "api/task_queue/to_queued_task.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/helpers.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/message_handler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
25namespace rtc {
26
27TEST(TimeTest, TimeInMs) {
Honghai Zhang82d78622016-05-06 11:29:15 -070028 int64_t ts_earlier = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029 Thread::SleepMs(100);
Honghai Zhang82d78622016-05-06 11:29:15 -070030 int64_t ts_now = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 // Allow for the thread to wakeup ~20ms early.
32 EXPECT_GE(ts_now, ts_earlier + 80);
33 // Make sure the Time is not returning in smaller unit like microseconds.
34 EXPECT_LT(ts_now, ts_earlier + 1000);
35}
36
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037TEST(TimeTest, Intervals) {
Honghai Zhang82d78622016-05-06 11:29:15 -070038 int64_t ts_earlier = TimeMillis();
39 int64_t ts_later = TimeAfter(500);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040
41 // We can't depend on ts_later and ts_earlier to be exactly 500 apart
Honghai Zhang82d78622016-05-06 11:29:15 -070042 // since time elapses between the calls to TimeMillis() and TimeAfter(500)
Yves Gerey665174f2018-06-19 15:03:05 +020043 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
45
46 // Time has elapsed since ts_earlier
47 EXPECT_GE(TimeSince(ts_earlier), 0);
48
49 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
50 EXPECT_LE(TimeUntil(ts_earlier), 0);
51
52 // ts_later likely hasn't happened yet, so TimeSince could be -ve
53 // but within 500
54 EXPECT_GE(TimeSince(ts_later), -500);
55
56 // TimeUntil ts_later is at most 500
57 EXPECT_LE(TimeUntil(ts_later), 500);
58}
59
honghaiz34b11eb2016-03-16 08:55:44 -070060TEST(TimeTest, TestTimeDiff64) {
61 int64_t ts_diff = 100;
nisse1bffc1d2016-05-02 08:18:55 -070062 int64_t ts_earlier = rtc::TimeMillis();
honghaiz34b11eb2016-03-16 08:55:44 -070063 int64_t ts_later = ts_earlier + ts_diff;
64 EXPECT_EQ(ts_diff, rtc::TimeDiff(ts_later, ts_earlier));
65 EXPECT_EQ(-ts_diff, rtc::TimeDiff(ts_earlier, ts_later));
66}
67
Mirko Bonadei6a489f22019-04-09 15:11:12 +020068class TimestampWrapAroundHandlerTest : public ::testing::Test {
henrike@webrtc.org99b41622014-05-21 20:42:17 +000069 public:
70 TimestampWrapAroundHandlerTest() {}
71
72 protected:
73 TimestampWrapAroundHandler wraparound_handler_;
74};
75
76TEST_F(TimestampWrapAroundHandlerTest, Unwrap) {
sprang1b3530b2016-03-10 01:32:53 -080077 // Start value.
78 int64_t ts = 2;
79 EXPECT_EQ(ts,
80 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
81
82 // Wrap backwards.
83 ts = -2;
84 EXPECT_EQ(ts,
85 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
86
87 // Forward to 2 again.
henrike@webrtc.org99b41622014-05-21 20:42:17 +000088 ts = 2;
sprang1b3530b2016-03-10 01:32:53 -080089 EXPECT_EQ(ts,
90 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
91
92 // Max positive skip ahead, until max value (0xffffffff).
93 for (uint32_t i = 0; i <= 0xf; ++i) {
94 ts = (i << 28) + 0x0fffffff;
95 EXPECT_EQ(
96 ts, wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
97 }
98
99 // Wrap around.
100 ts += 2;
101 EXPECT_EQ(ts,
102 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
103
104 // Max wrap backward...
105 ts -= 0x0fffffff;
106 EXPECT_EQ(ts,
107 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
108
109 // ...and back again.
110 ts += 0x0fffffff;
111 EXPECT_EQ(ts,
112 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
113}
114
115TEST_F(TimestampWrapAroundHandlerTest, NoNegativeStart) {
116 int64_t ts = 0xfffffff0;
117 EXPECT_EQ(ts,
118 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000119}
120
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200121class TmToSeconds : public ::testing::Test {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100122 public:
123 TmToSeconds() {
124 // Set use of the test RNG to get deterministic expiration timestamp.
125 rtc::SetRandomTestMode(true);
126 }
ehmaldonadoda8dcfb2017-01-04 07:11:23 -0800127 ~TmToSeconds() override {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100128 // Put it back for the next test.
129 rtc::SetRandomTestMode(false);
130 }
131
132 void TestTmToSeconds(int times) {
133 static char mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
134 for (int i = 0; i < times; i++) {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100135 // First generate something correct and check that TmToSeconds is happy.
136 int year = rtc::CreateRandomId() % 400 + 1970;
137
138 bool leap_year = false;
139 if (year % 4 == 0)
140 leap_year = true;
141 if (year % 100 == 0)
142 leap_year = false;
143 if (year % 400 == 0)
144 leap_year = true;
145
146 std::tm tm;
147 tm.tm_year = year - 1900; // std::tm is year 1900 based.
148 tm.tm_mon = rtc::CreateRandomId() % 12;
149 tm.tm_mday = rtc::CreateRandomId() % mdays[tm.tm_mon] + 1;
150 tm.tm_hour = rtc::CreateRandomId() % 24;
151 tm.tm_min = rtc::CreateRandomId() % 60;
152 tm.tm_sec = rtc::CreateRandomId() % 60;
153 int64_t t = rtc::TmToSeconds(tm);
154 EXPECT_TRUE(t >= 0);
155
156 // Now damage a random field and check that TmToSeconds is unhappy.
157 switch (rtc::CreateRandomId() % 11) {
158 case 0:
159 tm.tm_year = 1969 - 1900;
160 break;
161 case 1:
162 tm.tm_mon = -1;
163 break;
164 case 2:
165 tm.tm_mon = 12;
166 break;
167 case 3:
168 tm.tm_mday = 0;
169 break;
170 case 4:
171 tm.tm_mday = mdays[tm.tm_mon] + (leap_year && tm.tm_mon == 1) + 1;
172 break;
173 case 5:
174 tm.tm_hour = -1;
175 break;
176 case 6:
177 tm.tm_hour = 24;
178 break;
179 case 7:
180 tm.tm_min = -1;
181 break;
182 case 8:
183 tm.tm_min = 60;
184 break;
185 case 9:
186 tm.tm_sec = -1;
187 break;
188 case 10:
189 tm.tm_sec = 60;
190 break;
191 }
192 EXPECT_EQ(rtc::TmToSeconds(tm), -1);
193 }
194 // Check consistency with the system gmtime_r. With time_t, we can only
195 // portably test dates until 2038, which is achieved by the % 0x80000000.
196 for (int i = 0; i < times; i++) {
197 time_t t = rtc::CreateRandomId() % 0x80000000;
198#if defined(WEBRTC_WIN)
199 std::tm* tm = std::gmtime(&t);
200 EXPECT_TRUE(tm);
201 EXPECT_TRUE(rtc::TmToSeconds(*tm) == t);
202#else
203 std::tm tm;
204 EXPECT_TRUE(gmtime_r(&t, &tm));
205 EXPECT_TRUE(rtc::TmToSeconds(tm) == t);
206#endif
207 }
208 }
209};
210
211TEST_F(TmToSeconds, TestTmToSeconds) {
212 TestTmToSeconds(100000);
213}
214
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700215// Test that all the time functions exposed by TimeUtils get time from the
216// fake clock when it's set.
217TEST(FakeClock, TimeFunctionsUseFakeClock) {
218 FakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700219 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700220
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100221 clock.SetTime(webrtc::Timestamp::Micros(987654));
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700222 EXPECT_EQ(987u, Time32());
223 EXPECT_EQ(987, TimeMillis());
nissedeb95f32016-11-28 01:54:54 -0800224 EXPECT_EQ(987654, TimeMicros());
Sebastian Janssond624c392019-04-17 10:36:03 +0200225 EXPECT_EQ(987654000, TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700226 EXPECT_EQ(1000u, TimeAfter(13));
227
deadbeeff5f03e82016-06-06 11:16:06 -0700228 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700229 // After it's unset, we should get a normal time.
230 EXPECT_NE(987, TimeMillis());
231}
232
233TEST(FakeClock, InitialTime) {
234 FakeClock clock;
nissedeb95f32016-11-28 01:54:54 -0800235 EXPECT_EQ(0, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700236}
237
Sebastian Janssond624c392019-04-17 10:36:03 +0200238TEST(FakeClock, SetTime) {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700239 FakeClock clock;
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100240 clock.SetTime(webrtc::Timestamp::Micros(123));
Sebastian Janssond624c392019-04-17 10:36:03 +0200241 EXPECT_EQ(123000, clock.TimeNanos());
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100242 clock.SetTime(webrtc::Timestamp::Micros(456));
Sebastian Janssond624c392019-04-17 10:36:03 +0200243 EXPECT_EQ(456000, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700244}
245
246TEST(FakeClock, AdvanceTime) {
247 FakeClock clock;
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100248 clock.AdvanceTime(webrtc::TimeDelta::Micros(1u));
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200249 EXPECT_EQ(1000, clock.TimeNanos());
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100250 clock.AdvanceTime(webrtc::TimeDelta::Micros(2222u));
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200251 EXPECT_EQ(2223000, clock.TimeNanos());
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100252 clock.AdvanceTime(webrtc::TimeDelta::Millis(3333u));
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200253 EXPECT_EQ(3335223000, clock.TimeNanos());
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100254 clock.AdvanceTime(webrtc::TimeDelta::Seconds(4444u));
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200255 EXPECT_EQ(4447335223000, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700256}
257
258// When the clock is advanced, threads that are waiting in a socket select
259// should wake up and look at the new time. This allows tests using the
260// fake clock to run much faster, if the test is bound by time constraints
261// (such as a test for a STUN ping timeout).
262TEST(FakeClock, SettingTimeWakesThreads) {
263 int64_t real_start_time_ms = TimeMillis();
264
Sebastian Janssond624c392019-04-17 10:36:03 +0200265 ThreadProcessingFakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700266 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700267
tommie7251592017-07-14 14:44:46 -0700268 std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer());
269 worker->Start();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700270
271 // Post an event that won't be executed for 10 seconds.
Niels Möllerc572ff32018-11-07 08:43:50 +0100272 Event message_handler_dispatched;
Danil Chapovalovb877e712019-11-29 15:19:27 +0100273 worker->PostDelayedTask(webrtc::ToQueuedTask([&message_handler_dispatched] {
274 message_handler_dispatched.Set();
275 }),
276 /*milliseconds=*/60000);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700277
278 // Wait for a bit for the worker thread to be started and enter its socket
deadbeeff5f03e82016-06-06 11:16:06 -0700279 // select(). Otherwise this test would be trivial since the worker thread
280 // would process the event as soon as it was started.
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700281 Thread::Current()->SleepMs(1000);
282
283 // Advance the fake clock, expecting the worker thread to wake up
deadbeeff5f03e82016-06-06 11:16:06 -0700284 // and dispatch the message instantly.
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100285 clock.AdvanceTime(webrtc::TimeDelta::Seconds(60u));
deadbeeff5f03e82016-06-06 11:16:06 -0700286 EXPECT_TRUE(message_handler_dispatched.Wait(0));
tommie7251592017-07-14 14:44:46 -0700287 worker->Stop();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700288
deadbeeff5f03e82016-06-06 11:16:06 -0700289 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700290
deadbeeff5f03e82016-06-06 11:16:06 -0700291 // The message should have been dispatched long before the 60 seconds fully
292 // elapsed (just a sanity check).
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700293 int64_t real_end_time_ms = TimeMillis();
deadbeeff5f03e82016-06-06 11:16:06 -0700294 EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700295}
296
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297} // namespace rtc