blob: 0e1949aee31125bd36a721200338416e02b07c96 [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)
Yves Gerey665174f2018-06-19 15:03:05 +020036 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037 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++) {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100128 // First generate something correct and check that TmToSeconds is happy.
129 int year = rtc::CreateRandomId() % 400 + 1970;
130
131 bool leap_year = false;
132 if (year % 4 == 0)
133 leap_year = true;
134 if (year % 100 == 0)
135 leap_year = false;
136 if (year % 400 == 0)
137 leap_year = true;
138
139 std::tm tm;
140 tm.tm_year = year - 1900; // std::tm is year 1900 based.
141 tm.tm_mon = rtc::CreateRandomId() % 12;
142 tm.tm_mday = rtc::CreateRandomId() % mdays[tm.tm_mon] + 1;
143 tm.tm_hour = rtc::CreateRandomId() % 24;
144 tm.tm_min = rtc::CreateRandomId() % 60;
145 tm.tm_sec = rtc::CreateRandomId() % 60;
146 int64_t t = rtc::TmToSeconds(tm);
147 EXPECT_TRUE(t >= 0);
148
149 // Now damage a random field and check that TmToSeconds is unhappy.
150 switch (rtc::CreateRandomId() % 11) {
151 case 0:
152 tm.tm_year = 1969 - 1900;
153 break;
154 case 1:
155 tm.tm_mon = -1;
156 break;
157 case 2:
158 tm.tm_mon = 12;
159 break;
160 case 3:
161 tm.tm_mday = 0;
162 break;
163 case 4:
164 tm.tm_mday = mdays[tm.tm_mon] + (leap_year && tm.tm_mon == 1) + 1;
165 break;
166 case 5:
167 tm.tm_hour = -1;
168 break;
169 case 6:
170 tm.tm_hour = 24;
171 break;
172 case 7:
173 tm.tm_min = -1;
174 break;
175 case 8:
176 tm.tm_min = 60;
177 break;
178 case 9:
179 tm.tm_sec = -1;
180 break;
181 case 10:
182 tm.tm_sec = 60;
183 break;
184 }
185 EXPECT_EQ(rtc::TmToSeconds(tm), -1);
186 }
187 // Check consistency with the system gmtime_r. With time_t, we can only
188 // portably test dates until 2038, which is achieved by the % 0x80000000.
189 for (int i = 0; i < times; i++) {
190 time_t t = rtc::CreateRandomId() % 0x80000000;
191#if defined(WEBRTC_WIN)
192 std::tm* tm = std::gmtime(&t);
193 EXPECT_TRUE(tm);
194 EXPECT_TRUE(rtc::TmToSeconds(*tm) == t);
195#else
196 std::tm tm;
197 EXPECT_TRUE(gmtime_r(&t, &tm));
198 EXPECT_TRUE(rtc::TmToSeconds(tm) == t);
199#endif
200 }
201 }
202};
203
204TEST_F(TmToSeconds, TestTmToSeconds) {
205 TestTmToSeconds(100000);
206}
207
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700208// Test that all the time functions exposed by TimeUtils get time from the
209// fake clock when it's set.
210TEST(FakeClock, TimeFunctionsUseFakeClock) {
211 FakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700212 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700213
nissedeb95f32016-11-28 01:54:54 -0800214 clock.SetTimeNanos(987654321);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700215 EXPECT_EQ(987u, Time32());
216 EXPECT_EQ(987, TimeMillis());
nissedeb95f32016-11-28 01:54:54 -0800217 EXPECT_EQ(987654, TimeMicros());
218 EXPECT_EQ(987654321, TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700219 EXPECT_EQ(1000u, TimeAfter(13));
220
deadbeeff5f03e82016-06-06 11:16:06 -0700221 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700222 // After it's unset, we should get a normal time.
223 EXPECT_NE(987, TimeMillis());
224}
225
226TEST(FakeClock, InitialTime) {
227 FakeClock clock;
nissedeb95f32016-11-28 01:54:54 -0800228 EXPECT_EQ(0, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700229}
230
231TEST(FakeClock, SetTimeNanos) {
232 FakeClock clock;
nissedeb95f32016-11-28 01:54:54 -0800233 clock.SetTimeNanos(123);
234 EXPECT_EQ(123, clock.TimeNanos());
235 clock.SetTimeNanos(456);
236 EXPECT_EQ(456, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700237}
238
239TEST(FakeClock, AdvanceTime) {
240 FakeClock clock;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200241 clock.AdvanceTime(webrtc::TimeDelta::us(1u));
242 EXPECT_EQ(1000, clock.TimeNanos());
243 clock.AdvanceTime(webrtc::TimeDelta::us(2222u));
244 EXPECT_EQ(2223000, clock.TimeNanos());
245 clock.AdvanceTime(webrtc::TimeDelta::ms(3333u));
246 EXPECT_EQ(3335223000, clock.TimeNanos());
247 clock.AdvanceTime(webrtc::TimeDelta::seconds(4444u));
248 EXPECT_EQ(4447335223000, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700249}
250
251// When the clock is advanced, threads that are waiting in a socket select
252// should wake up and look at the new time. This allows tests using the
253// fake clock to run much faster, if the test is bound by time constraints
254// (such as a test for a STUN ping timeout).
255TEST(FakeClock, SettingTimeWakesThreads) {
256 int64_t real_start_time_ms = TimeMillis();
257
258 FakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700259 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700260
tommie7251592017-07-14 14:44:46 -0700261 std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer());
262 worker->Start();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700263
264 // Post an event that won't be executed for 10 seconds.
265 Event message_handler_dispatched(false, false);
266 auto functor = [&message_handler_dispatched] {
267 message_handler_dispatched.Set();
268 };
269 FunctorMessageHandler<void, decltype(functor)> handler(functor);
tommie7251592017-07-14 14:44:46 -0700270 worker->PostDelayed(RTC_FROM_HERE, 60000, &handler);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700271
272 // Wait for a bit for the worker thread to be started and enter its socket
deadbeeff5f03e82016-06-06 11:16:06 -0700273 // select(). Otherwise this test would be trivial since the worker thread
274 // would process the event as soon as it was started.
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700275 Thread::Current()->SleepMs(1000);
276
277 // Advance the fake clock, expecting the worker thread to wake up
deadbeeff5f03e82016-06-06 11:16:06 -0700278 // and dispatch the message instantly.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200279 clock.AdvanceTime(webrtc::TimeDelta::seconds(60u));
deadbeeff5f03e82016-06-06 11:16:06 -0700280 EXPECT_TRUE(message_handler_dispatched.Wait(0));
tommie7251592017-07-14 14:44:46 -0700281 worker->Stop();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700282
deadbeeff5f03e82016-06-06 11:16:06 -0700283 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700284
deadbeeff5f03e82016-06-06 11:16:06 -0700285 // The message should have been dispatched long before the 60 seconds fully
286 // elapsed (just a sanity check).
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700287 int64_t real_end_time_ms = TimeMillis();
deadbeeff5f03e82016-06-06 11:16:06 -0700288 EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700289}
290
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291} // namespace rtc