blob: aeb9daf8813ae1bbaa75fdfcf67da66be7c5bfce [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
15#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/helpers.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/message_handler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24namespace rtc {
25
26TEST(TimeTest, TimeInMs) {
Honghai Zhang82d78622016-05-06 11:29:15 -070027 int64_t ts_earlier = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028 Thread::SleepMs(100);
Honghai Zhang82d78622016-05-06 11:29:15 -070029 int64_t ts_now = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 // Allow for the thread to wakeup ~20ms early.
31 EXPECT_GE(ts_now, ts_earlier + 80);
32 // Make sure the Time is not returning in smaller unit like microseconds.
33 EXPECT_LT(ts_now, ts_earlier + 1000);
34}
35
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036TEST(TimeTest, Intervals) {
Honghai Zhang82d78622016-05-06 11:29:15 -070037 int64_t ts_earlier = TimeMillis();
38 int64_t ts_later = TimeAfter(500);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039
40 // We can't depend on ts_later and ts_earlier to be exactly 500 apart
Honghai Zhang82d78622016-05-06 11:29:15 -070041 // since time elapses between the calls to TimeMillis() and TimeAfter(500)
Yves Gerey665174f2018-06-19 15:03:05 +020042 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
44
45 // Time has elapsed since ts_earlier
46 EXPECT_GE(TimeSince(ts_earlier), 0);
47
48 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
49 EXPECT_LE(TimeUntil(ts_earlier), 0);
50
51 // ts_later likely hasn't happened yet, so TimeSince could be -ve
52 // but within 500
53 EXPECT_GE(TimeSince(ts_later), -500);
54
55 // TimeUntil ts_later is at most 500
56 EXPECT_LE(TimeUntil(ts_later), 500);
57}
58
honghaiz34b11eb2016-03-16 08:55:44 -070059TEST(TimeTest, TestTimeDiff64) {
60 int64_t ts_diff = 100;
nisse1bffc1d2016-05-02 08:18:55 -070061 int64_t ts_earlier = rtc::TimeMillis();
honghaiz34b11eb2016-03-16 08:55:44 -070062 int64_t ts_later = ts_earlier + ts_diff;
63 EXPECT_EQ(ts_diff, rtc::TimeDiff(ts_later, ts_earlier));
64 EXPECT_EQ(-ts_diff, rtc::TimeDiff(ts_earlier, ts_later));
65}
66
Mirko Bonadei6a489f22019-04-09 15:11:12 +020067class TimestampWrapAroundHandlerTest : public ::testing::Test {
henrike@webrtc.org99b41622014-05-21 20:42:17 +000068 public:
69 TimestampWrapAroundHandlerTest() {}
70
71 protected:
72 TimestampWrapAroundHandler wraparound_handler_;
73};
74
75TEST_F(TimestampWrapAroundHandlerTest, Unwrap) {
sprang1b3530b2016-03-10 01:32:53 -080076 // Start value.
77 int64_t ts = 2;
78 EXPECT_EQ(ts,
79 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
80
81 // Wrap backwards.
82 ts = -2;
83 EXPECT_EQ(ts,
84 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
85
86 // Forward to 2 again.
henrike@webrtc.org99b41622014-05-21 20:42:17 +000087 ts = 2;
sprang1b3530b2016-03-10 01:32:53 -080088 EXPECT_EQ(ts,
89 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
90
91 // Max positive skip ahead, until max value (0xffffffff).
92 for (uint32_t i = 0; i <= 0xf; ++i) {
93 ts = (i << 28) + 0x0fffffff;
94 EXPECT_EQ(
95 ts, wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
96 }
97
98 // Wrap around.
99 ts += 2;
100 EXPECT_EQ(ts,
101 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
102
103 // Max wrap backward...
104 ts -= 0x0fffffff;
105 EXPECT_EQ(ts,
106 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
107
108 // ...and back again.
109 ts += 0x0fffffff;
110 EXPECT_EQ(ts,
111 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
112}
113
114TEST_F(TimestampWrapAroundHandlerTest, NoNegativeStart) {
115 int64_t ts = 0xfffffff0;
116 EXPECT_EQ(ts,
117 wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000118}
119
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200120class TmToSeconds : public ::testing::Test {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100121 public:
122 TmToSeconds() {
123 // Set use of the test RNG to get deterministic expiration timestamp.
124 rtc::SetRandomTestMode(true);
125 }
ehmaldonadoda8dcfb2017-01-04 07:11:23 -0800126 ~TmToSeconds() override {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100127 // Put it back for the next test.
128 rtc::SetRandomTestMode(false);
129 }
130
131 void TestTmToSeconds(int times) {
132 static char mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
133 for (int i = 0; i < times; i++) {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100134 // First generate something correct and check that TmToSeconds is happy.
135 int year = rtc::CreateRandomId() % 400 + 1970;
136
137 bool leap_year = false;
138 if (year % 4 == 0)
139 leap_year = true;
140 if (year % 100 == 0)
141 leap_year = false;
142 if (year % 400 == 0)
143 leap_year = true;
144
145 std::tm tm;
146 tm.tm_year = year - 1900; // std::tm is year 1900 based.
147 tm.tm_mon = rtc::CreateRandomId() % 12;
148 tm.tm_mday = rtc::CreateRandomId() % mdays[tm.tm_mon] + 1;
149 tm.tm_hour = rtc::CreateRandomId() % 24;
150 tm.tm_min = rtc::CreateRandomId() % 60;
151 tm.tm_sec = rtc::CreateRandomId() % 60;
152 int64_t t = rtc::TmToSeconds(tm);
153 EXPECT_TRUE(t >= 0);
154
155 // Now damage a random field and check that TmToSeconds is unhappy.
156 switch (rtc::CreateRandomId() % 11) {
157 case 0:
158 tm.tm_year = 1969 - 1900;
159 break;
160 case 1:
161 tm.tm_mon = -1;
162 break;
163 case 2:
164 tm.tm_mon = 12;
165 break;
166 case 3:
167 tm.tm_mday = 0;
168 break;
169 case 4:
170 tm.tm_mday = mdays[tm.tm_mon] + (leap_year && tm.tm_mon == 1) + 1;
171 break;
172 case 5:
173 tm.tm_hour = -1;
174 break;
175 case 6:
176 tm.tm_hour = 24;
177 break;
178 case 7:
179 tm.tm_min = -1;
180 break;
181 case 8:
182 tm.tm_min = 60;
183 break;
184 case 9:
185 tm.tm_sec = -1;
186 break;
187 case 10:
188 tm.tm_sec = 60;
189 break;
190 }
191 EXPECT_EQ(rtc::TmToSeconds(tm), -1);
192 }
193 // Check consistency with the system gmtime_r. With time_t, we can only
194 // portably test dates until 2038, which is achieved by the % 0x80000000.
195 for (int i = 0; i < times; i++) {
196 time_t t = rtc::CreateRandomId() % 0x80000000;
197#if defined(WEBRTC_WIN)
198 std::tm* tm = std::gmtime(&t);
199 EXPECT_TRUE(tm);
200 EXPECT_TRUE(rtc::TmToSeconds(*tm) == t);
201#else
202 std::tm tm;
203 EXPECT_TRUE(gmtime_r(&t, &tm));
204 EXPECT_TRUE(rtc::TmToSeconds(tm) == t);
205#endif
206 }
207 }
208};
209
210TEST_F(TmToSeconds, TestTmToSeconds) {
211 TestTmToSeconds(100000);
212}
213
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700214// Test that all the time functions exposed by TimeUtils get time from the
215// fake clock when it's set.
216TEST(FakeClock, TimeFunctionsUseFakeClock) {
217 FakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700218 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700219
Sebastian Janssond624c392019-04-17 10:36:03 +0200220 clock.SetTime(webrtc::Timestamp::us(987654));
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700221 EXPECT_EQ(987u, Time32());
222 EXPECT_EQ(987, TimeMillis());
nissedeb95f32016-11-28 01:54:54 -0800223 EXPECT_EQ(987654, TimeMicros());
Sebastian Janssond624c392019-04-17 10:36:03 +0200224 EXPECT_EQ(987654000, TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700225 EXPECT_EQ(1000u, TimeAfter(13));
226
deadbeeff5f03e82016-06-06 11:16:06 -0700227 SetClockForTesting(nullptr);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700228 // After it's unset, we should get a normal time.
229 EXPECT_NE(987, TimeMillis());
230}
231
232TEST(FakeClock, InitialTime) {
233 FakeClock clock;
nissedeb95f32016-11-28 01:54:54 -0800234 EXPECT_EQ(0, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700235}
236
Sebastian Janssond624c392019-04-17 10:36:03 +0200237TEST(FakeClock, SetTime) {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700238 FakeClock clock;
Sebastian Janssond624c392019-04-17 10:36:03 +0200239 clock.SetTime(webrtc::Timestamp::us(123));
240 EXPECT_EQ(123000, clock.TimeNanos());
241 clock.SetTime(webrtc::Timestamp::us(456));
242 EXPECT_EQ(456000, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700243}
244
245TEST(FakeClock, AdvanceTime) {
246 FakeClock clock;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200247 clock.AdvanceTime(webrtc::TimeDelta::us(1u));
248 EXPECT_EQ(1000, clock.TimeNanos());
249 clock.AdvanceTime(webrtc::TimeDelta::us(2222u));
250 EXPECT_EQ(2223000, clock.TimeNanos());
251 clock.AdvanceTime(webrtc::TimeDelta::ms(3333u));
252 EXPECT_EQ(3335223000, clock.TimeNanos());
253 clock.AdvanceTime(webrtc::TimeDelta::seconds(4444u));
254 EXPECT_EQ(4447335223000, clock.TimeNanos());
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700255}
256
257// When the clock is advanced, threads that are waiting in a socket select
258// should wake up and look at the new time. This allows tests using the
259// fake clock to run much faster, if the test is bound by time constraints
260// (such as a test for a STUN ping timeout).
261TEST(FakeClock, SettingTimeWakesThreads) {
262 int64_t real_start_time_ms = TimeMillis();
263
Sebastian Janssond624c392019-04-17 10:36:03 +0200264 ThreadProcessingFakeClock clock;
deadbeeff5f03e82016-06-06 11:16:06 -0700265 SetClockForTesting(&clock);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700266
tommie7251592017-07-14 14:44:46 -0700267 std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer());
268 worker->Start();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700269
270 // Post an event that won't be executed for 10 seconds.
Niels Möllerc572ff32018-11-07 08:43:50 +0100271 Event message_handler_dispatched;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700272 auto functor = [&message_handler_dispatched] {
273 message_handler_dispatched.Set();
274 };
Artem Titovd8bd7502019-01-09 21:10:00 +0100275 FunctorMessageHandler<void, decltype(functor)> handler(std::move(functor));
tommie7251592017-07-14 14:44:46 -0700276 worker->PostDelayed(RTC_FROM_HERE, 60000, &handler);
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.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200285 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