blob: 7b393a663c4e87b97f5aaf7789eba2b11b39371a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/timeutils.h"
12#include "rtc_base/event.h"
13#include "rtc_base/fakeclock.h"
14#include "rtc_base/gunit.h"
15#include "rtc_base/helpers.h"
16#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18namespace rtc {
19
20TEST(TimeTest, TimeInMs) {
Honghai Zhang82d78622016-05-06 11:29:15 -070021 int64_t ts_earlier = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022 Thread::SleepMs(100);
Honghai Zhang82d78622016-05-06 11:29:15 -070023 int64_t ts_now = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024 // Allow for the thread to wakeup ~20ms early.
25 EXPECT_GE(ts_now, ts_earlier + 80);
26 // Make sure the Time is not returning in smaller unit like microseconds.
27 EXPECT_LT(ts_now, ts_earlier + 1000);
28}
29
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030TEST(TimeTest, Intervals) {
Honghai Zhang82d78622016-05-06 11:29:15 -070031 int64_t ts_earlier = TimeMillis();
32 int64_t ts_later = TimeAfter(500);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
34 // We can't depend on ts_later and ts_earlier to be exactly 500 apart
Honghai Zhang82d78622016-05-06 11:29:15 -070035 // since time elapses between the calls to TimeMillis() and TimeAfter(500)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier));
37 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
38
39 // Time has elapsed since ts_earlier
40 EXPECT_GE(TimeSince(ts_earlier), 0);
41
42 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
43 EXPECT_LE(TimeUntil(ts_earlier), 0);
44
45 // ts_later likely hasn't happened yet, so TimeSince could be -ve
46 // but within 500
47 EXPECT_GE(TimeSince(ts_later), -500);
48
49 // TimeUntil ts_later is at most 500
50 EXPECT_LE(TimeUntil(ts_later), 500);
51}
52
honghaiz34b11eb2016-03-16 08:55:44 -070053TEST(TimeTest, TestTimeDiff64) {
54 int64_t ts_diff = 100;
nisse1bffc1d2016-05-02 08:18:55 -070055 int64_t ts_earlier = rtc::TimeMillis();
honghaiz34b11eb2016-03-16 08:55:44 -070056 int64_t ts_later = ts_earlier + ts_diff;
57 EXPECT_EQ(ts_diff, rtc::TimeDiff(ts_later, ts_earlier));
58 EXPECT_EQ(-ts_diff, rtc::TimeDiff(ts_earlier, ts_later));
59}
60
henrike@webrtc.org99b41622014-05-21 20:42:17 +000061class TimestampWrapAroundHandlerTest : public testing::Test {
62 public:
63 TimestampWrapAroundHandlerTest() {}
64
65 protected:
66 TimestampWrapAroundHandler wraparound_handler_;
67};
68
69TEST_F(TimestampWrapAroundHandlerTest, Unwrap) {
sprang1b3530b2016-03-10 01:32:53 -080070 // Start value.
71 int64_t ts = 2;
72 EXPECT_EQ(ts,
73 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
74
75 // Wrap backwards.
76 ts = -2;
77 EXPECT_EQ(ts,
78 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
79
80 // Forward to 2 again.
henrike@webrtc.org99b41622014-05-21 20:42:17 +000081 ts = 2;
sprang1b3530b2016-03-10 01:32:53 -080082 EXPECT_EQ(ts,
83 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
84
85 // Max positive skip ahead, until max value (0xffffffff).
86 for (uint32_t i = 0; i <= 0xf; ++i) {
87 ts = (i << 28) + 0x0fffffff;
88 EXPECT_EQ(
89 ts, wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
90 }
91
92 // Wrap around.
93 ts += 2;
94 EXPECT_EQ(ts,
95 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
96
97 // Max wrap backward...
98 ts -= 0x0fffffff;
99 EXPECT_EQ(ts,
100 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
101
102 // ...and back again.
103 ts += 0x0fffffff;
104 EXPECT_EQ(ts,
105 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
106}
107
108TEST_F(TimestampWrapAroundHandlerTest, NoNegativeStart) {
109 int64_t ts = 0xfffffff0;
110 EXPECT_EQ(ts,
111 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000112}
113
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100114class TmToSeconds : public testing::Test {
115 public:
116 TmToSeconds() {
117 // Set use of the test RNG to get deterministic expiration timestamp.
118 rtc::SetRandomTestMode(true);
119 }
ehmaldonadoda8dcfb2017-01-04 07:11:23 -0800120 ~TmToSeconds() override {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100121 // Put it back for the next test.
122 rtc::SetRandomTestMode(false);
123 }
124
125 void TestTmToSeconds(int times) {
126 static char mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
127 for (int i = 0; i < times; i++) {
128
129 // First generate something correct and check that TmToSeconds is happy.
130 int year = rtc::CreateRandomId() % 400 + 1970;
131
132 bool leap_year = false;
133 if (year % 4 == 0)
134 leap_year = true;
135 if (year % 100 == 0)
136 leap_year = false;
137 if (year % 400 == 0)
138 leap_year = true;
139
140 std::tm tm;
141 tm.tm_year = year - 1900; // std::tm is year 1900 based.
142 tm.tm_mon = rtc::CreateRandomId() % 12;
143 tm.tm_mday = rtc::CreateRandomId() % mdays[tm.tm_mon] + 1;
144 tm.tm_hour = rtc::CreateRandomId() % 24;
145 tm.tm_min = rtc::CreateRandomId() % 60;
146 tm.tm_sec = rtc::CreateRandomId() % 60;
147 int64_t t = rtc::TmToSeconds(tm);
148 EXPECT_TRUE(t >= 0);
149
150 // Now damage a random field and check that TmToSeconds is unhappy.
151 switch (rtc::CreateRandomId() % 11) {
152 case 0:
153 tm.tm_year = 1969 - 1900;
154 break;
155 case 1:
156 tm.tm_mon = -1;
157 break;
158 case 2:
159 tm.tm_mon = 12;
160 break;
161 case 3:
162 tm.tm_mday = 0;
163 break;
164 case 4:
165 tm.tm_mday = mdays[tm.tm_mon] + (leap_year && tm.tm_mon == 1) + 1;
166 break;
167 case 5:
168 tm.tm_hour = -1;
169 break;
170 case 6:
171 tm.tm_hour = 24;
172 break;
173 case 7:
174 tm.tm_min = -1;
175 break;
176 case 8:
177 tm.tm_min = 60;
178 break;
179 case 9:
180 tm.tm_sec = -1;
181 break;
182 case 10:
183 tm.tm_sec = 60;
184 break;
185 }
186 EXPECT_EQ(rtc::TmToSeconds(tm), -1);
187 }
188 // Check consistency with the system gmtime_r. With time_t, we can only
189 // portably test dates until 2038, which is achieved by the % 0x80000000.
190 for (int i = 0; i < times; i++) {
191 time_t t = rtc::CreateRandomId() % 0x80000000;
192#if defined(WEBRTC_WIN)
193 std::tm* tm = std::gmtime(&t);
194 EXPECT_TRUE(tm);
195 EXPECT_TRUE(rtc::TmToSeconds(*tm) == t);
196#else
197 std::tm tm;
198 EXPECT_TRUE(gmtime_r(&t, &tm));
199 EXPECT_TRUE(rtc::TmToSeconds(tm) == t);
200#endif
201 }
202 }
203};
204
205TEST_F(TmToSeconds, TestTmToSeconds) {
206 TestTmToSeconds(100000);
207}
208
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700209// Test that all the time functions exposed by TimeUtils get time from the
210// fake clock when it's set.
211TEST(FakeClock, TimeFunctionsUseFakeClock) {
212 FakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700213 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700214
nissedeb95f32016-11-28 01:54:54 -0800215 clock.SetTimeNanos(987654321);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700216 EXPECT_EQ(987u, Time32());
217 EXPECT_EQ(987, TimeMillis());
nissedeb95f32016-11-28 01:54:54 -0800218 EXPECT_EQ(987654, TimeMicros());
219 EXPECT_EQ(987654321, TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700220 EXPECT_EQ(1000u, TimeAfter(13));
221
deadbeeff5f03e82016-06-06 11:16:06 -0700222 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700223 // After it's unset, we should get a normal time.
224 EXPECT_NE(987, TimeMillis());
225}
226
227TEST(FakeClock, InitialTime) {
228 FakeClock clock;
nissedeb95f32016-11-28 01:54:54 -0800229 EXPECT_EQ(0, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700230}
231
232TEST(FakeClock, SetTimeNanos) {
233 FakeClock clock;
nissedeb95f32016-11-28 01:54:54 -0800234 clock.SetTimeNanos(123);
235 EXPECT_EQ(123, clock.TimeNanos());
236 clock.SetTimeNanos(456);
237 EXPECT_EQ(456, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700238}
239
240TEST(FakeClock, AdvanceTime) {
241 FakeClock clock;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200242 clock.AdvanceTime(webrtc::TimeDelta::us(1u));
243 EXPECT_EQ(1000, clock.TimeNanos());
244 clock.AdvanceTime(webrtc::TimeDelta::us(2222u));
245 EXPECT_EQ(2223000, clock.TimeNanos());
246 clock.AdvanceTime(webrtc::TimeDelta::ms(3333u));
247 EXPECT_EQ(3335223000, clock.TimeNanos());
248 clock.AdvanceTime(webrtc::TimeDelta::seconds(4444u));
249 EXPECT_EQ(4447335223000, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700250}
251
252// When the clock is advanced, threads that are waiting in a socket select
253// should wake up and look at the new time. This allows tests using the
254// fake clock to run much faster, if the test is bound by time constraints
255// (such as a test for a STUN ping timeout).
256TEST(FakeClock, SettingTimeWakesThreads) {
257 int64_t real_start_time_ms = TimeMillis();
258
259 FakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700260 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700261
tommie7251592017-07-14 14:44:46 -0700262 std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer());
263 worker->Start();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700264
265 // Post an event that won't be executed for 10 seconds.
266 Event message_handler_dispatched(false, false);
267 auto functor = [&message_handler_dispatched] {
268 message_handler_dispatched.Set();
269 };
270 FunctorMessageHandler<void, decltype(functor)> handler(functor);
tommie7251592017-07-14 14:44:46 -0700271 worker->PostDelayed(RTC_FROM_HERE, 60000, &handler);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700272
273 // Wait for a bit for the worker thread to be started and enter its socket
deadbeeff5f03e82016-06-06 11:16:06 -0700274 // select(). Otherwise this test would be trivial since the worker thread
275 // would process the event as soon as it was started.
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700276 Thread::Current()->SleepMs(1000);
277
278 // Advance the fake clock, expecting the worker thread to wake up
deadbeeff5f03e82016-06-06 11:16:06 -0700279 // and dispatch the message instantly.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200280 clock.AdvanceTime(webrtc::TimeDelta::seconds(60u));
deadbeeff5f03e82016-06-06 11:16:06 -0700281 EXPECT_TRUE(message_handler_dispatched.Wait(0));
tommie7251592017-07-14 14:44:46 -0700282 worker->Stop();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700283
deadbeeff5f03e82016-06-06 11:16:06 -0700284 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700285
deadbeeff5f03e82016-06-06 11:16:06 -0700286 // The message should have been dispatched long before the 60 seconds fully
287 // elapsed (just a sanity check).
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700288 int64_t real_end_time_ms = TimeMillis();
deadbeeff5f03e82016-06-06 11:16:06 -0700289 EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700290}
291
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000292} // namespace rtc