You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
8.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "rtc_base/time_utils.h"
  11. #include <memory>
  12. #include "api/units/time_delta.h"
  13. #include "rtc_base/event.h"
  14. #include "rtc_base/fake_clock.h"
  15. #include "rtc_base/helpers.h"
  16. #include "rtc_base/location.h"
  17. #include "rtc_base/message_handler.h"
  18. #include "rtc_base/thread.h"
  19. #include "test/gtest.h"
  20. namespace rtc {
  21. TEST(TimeTest, TimeInMs) {
  22. int64_t ts_earlier = TimeMillis();
  23. Thread::SleepMs(100);
  24. int64_t ts_now = TimeMillis();
  25. // Allow for the thread to wakeup ~20ms early.
  26. EXPECT_GE(ts_now, ts_earlier + 80);
  27. // Make sure the Time is not returning in smaller unit like microseconds.
  28. EXPECT_LT(ts_now, ts_earlier + 1000);
  29. }
  30. TEST(TimeTest, Intervals) {
  31. int64_t ts_earlier = TimeMillis();
  32. int64_t ts_later = TimeAfter(500);
  33. // We can't depend on ts_later and ts_earlier to be exactly 500 apart
  34. // since time elapses between the calls to TimeMillis() and TimeAfter(500)
  35. EXPECT_LE(500, TimeDiff(ts_later, ts_earlier));
  36. EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
  37. // Time has elapsed since ts_earlier
  38. EXPECT_GE(TimeSince(ts_earlier), 0);
  39. // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
  40. EXPECT_LE(TimeUntil(ts_earlier), 0);
  41. // ts_later likely hasn't happened yet, so TimeSince could be -ve
  42. // but within 500
  43. EXPECT_GE(TimeSince(ts_later), -500);
  44. // TimeUntil ts_later is at most 500
  45. EXPECT_LE(TimeUntil(ts_later), 500);
  46. }
  47. TEST(TimeTest, TestTimeDiff64) {
  48. int64_t ts_diff = 100;
  49. int64_t ts_earlier = rtc::TimeMillis();
  50. int64_t ts_later = ts_earlier + ts_diff;
  51. EXPECT_EQ(ts_diff, rtc::TimeDiff(ts_later, ts_earlier));
  52. EXPECT_EQ(-ts_diff, rtc::TimeDiff(ts_earlier, ts_later));
  53. }
  54. class TimestampWrapAroundHandlerTest : public ::testing::Test {
  55. public:
  56. TimestampWrapAroundHandlerTest() {}
  57. protected:
  58. TimestampWrapAroundHandler wraparound_handler_;
  59. };
  60. TEST_F(TimestampWrapAroundHandlerTest, Unwrap) {
  61. // Start value.
  62. int64_t ts = 2;
  63. EXPECT_EQ(ts,
  64. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  65. // Wrap backwards.
  66. ts = -2;
  67. EXPECT_EQ(ts,
  68. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  69. // Forward to 2 again.
  70. ts = 2;
  71. EXPECT_EQ(ts,
  72. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  73. // Max positive skip ahead, until max value (0xffffffff).
  74. for (uint32_t i = 0; i <= 0xf; ++i) {
  75. ts = (i << 28) + 0x0fffffff;
  76. EXPECT_EQ(
  77. ts, wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  78. }
  79. // Wrap around.
  80. ts += 2;
  81. EXPECT_EQ(ts,
  82. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  83. // Max wrap backward...
  84. ts -= 0x0fffffff;
  85. EXPECT_EQ(ts,
  86. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  87. // ...and back again.
  88. ts += 0x0fffffff;
  89. EXPECT_EQ(ts,
  90. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  91. }
  92. TEST_F(TimestampWrapAroundHandlerTest, NoNegativeStart) {
  93. int64_t ts = 0xfffffff0;
  94. EXPECT_EQ(ts,
  95. wraparound_handler_.Unwrap(static_cast<uint32_t>(ts & 0xffffffff)));
  96. }
  97. class TmToSeconds : public ::testing::Test {
  98. public:
  99. TmToSeconds() {
  100. // Set use of the test RNG to get deterministic expiration timestamp.
  101. rtc::SetRandomTestMode(true);
  102. }
  103. ~TmToSeconds() override {
  104. // Put it back for the next test.
  105. rtc::SetRandomTestMode(false);
  106. }
  107. void TestTmToSeconds(int times) {
  108. static char mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  109. for (int i = 0; i < times; i++) {
  110. // First generate something correct and check that TmToSeconds is happy.
  111. int year = rtc::CreateRandomId() % 400 + 1970;
  112. bool leap_year = false;
  113. if (year % 4 == 0)
  114. leap_year = true;
  115. if (year % 100 == 0)
  116. leap_year = false;
  117. if (year % 400 == 0)
  118. leap_year = true;
  119. std::tm tm;
  120. tm.tm_year = year - 1900; // std::tm is year 1900 based.
  121. tm.tm_mon = rtc::CreateRandomId() % 12;
  122. tm.tm_mday = rtc::CreateRandomId() % mdays[tm.tm_mon] + 1;
  123. tm.tm_hour = rtc::CreateRandomId() % 24;
  124. tm.tm_min = rtc::CreateRandomId() % 60;
  125. tm.tm_sec = rtc::CreateRandomId() % 60;
  126. int64_t t = rtc::TmToSeconds(tm);
  127. EXPECT_TRUE(t >= 0);
  128. // Now damage a random field and check that TmToSeconds is unhappy.
  129. switch (rtc::CreateRandomId() % 11) {
  130. case 0:
  131. tm.tm_year = 1969 - 1900;
  132. break;
  133. case 1:
  134. tm.tm_mon = -1;
  135. break;
  136. case 2:
  137. tm.tm_mon = 12;
  138. break;
  139. case 3:
  140. tm.tm_mday = 0;
  141. break;
  142. case 4:
  143. tm.tm_mday = mdays[tm.tm_mon] + (leap_year && tm.tm_mon == 1) + 1;
  144. break;
  145. case 5:
  146. tm.tm_hour = -1;
  147. break;
  148. case 6:
  149. tm.tm_hour = 24;
  150. break;
  151. case 7:
  152. tm.tm_min = -1;
  153. break;
  154. case 8:
  155. tm.tm_min = 60;
  156. break;
  157. case 9:
  158. tm.tm_sec = -1;
  159. break;
  160. case 10:
  161. tm.tm_sec = 60;
  162. break;
  163. }
  164. EXPECT_EQ(rtc::TmToSeconds(tm), -1);
  165. }
  166. // Check consistency with the system gmtime_r. With time_t, we can only
  167. // portably test dates until 2038, which is achieved by the % 0x80000000.
  168. for (int i = 0; i < times; i++) {
  169. time_t t = rtc::CreateRandomId() % 0x80000000;
  170. #if defined(WEBRTC_WIN)
  171. std::tm* tm = std::gmtime(&t);
  172. EXPECT_TRUE(tm);
  173. EXPECT_TRUE(rtc::TmToSeconds(*tm) == t);
  174. #else
  175. std::tm tm;
  176. EXPECT_TRUE(gmtime_r(&t, &tm));
  177. EXPECT_TRUE(rtc::TmToSeconds(tm) == t);
  178. #endif
  179. }
  180. }
  181. };
  182. TEST_F(TmToSeconds, TestTmToSeconds) {
  183. TestTmToSeconds(100000);
  184. }
  185. // Test that all the time functions exposed by TimeUtils get time from the
  186. // fake clock when it's set.
  187. TEST(FakeClock, TimeFunctionsUseFakeClock) {
  188. FakeClock clock;
  189. SetClockForTesting(&clock);
  190. clock.SetTime(webrtc::Timestamp::us(987654));
  191. EXPECT_EQ(987u, Time32());
  192. EXPECT_EQ(987, TimeMillis());
  193. EXPECT_EQ(987654, TimeMicros());
  194. EXPECT_EQ(987654000, TimeNanos());
  195. EXPECT_EQ(1000u, TimeAfter(13));
  196. SetClockForTesting(nullptr);
  197. // After it's unset, we should get a normal time.
  198. EXPECT_NE(987, TimeMillis());
  199. }
  200. TEST(FakeClock, InitialTime) {
  201. FakeClock clock;
  202. EXPECT_EQ(0, clock.TimeNanos());
  203. }
  204. TEST(FakeClock, SetTime) {
  205. FakeClock clock;
  206. clock.SetTime(webrtc::Timestamp::us(123));
  207. EXPECT_EQ(123000, clock.TimeNanos());
  208. clock.SetTime(webrtc::Timestamp::us(456));
  209. EXPECT_EQ(456000, clock.TimeNanos());
  210. }
  211. TEST(FakeClock, AdvanceTime) {
  212. FakeClock clock;
  213. clock.AdvanceTime(webrtc::TimeDelta::us(1u));
  214. EXPECT_EQ(1000, clock.TimeNanos());
  215. clock.AdvanceTime(webrtc::TimeDelta::us(2222u));
  216. EXPECT_EQ(2223000, clock.TimeNanos());
  217. clock.AdvanceTime(webrtc::TimeDelta::ms(3333u));
  218. EXPECT_EQ(3335223000, clock.TimeNanos());
  219. clock.AdvanceTime(webrtc::TimeDelta::seconds(4444u));
  220. EXPECT_EQ(4447335223000, clock.TimeNanos());
  221. }
  222. // When the clock is advanced, threads that are waiting in a socket select
  223. // should wake up and look at the new time. This allows tests using the
  224. // fake clock to run much faster, if the test is bound by time constraints
  225. // (such as a test for a STUN ping timeout).
  226. TEST(FakeClock, SettingTimeWakesThreads) {
  227. int64_t real_start_time_ms = TimeMillis();
  228. ThreadProcessingFakeClock clock;
  229. SetClockForTesting(&clock);
  230. std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer());
  231. worker->Start();
  232. // Post an event that won't be executed for 10 seconds.
  233. Event message_handler_dispatched;
  234. auto functor = [&message_handler_dispatched] {
  235. message_handler_dispatched.Set();
  236. };
  237. FunctorMessageHandler<void, decltype(functor)> handler(std::move(functor));
  238. worker->PostDelayed(RTC_FROM_HERE, 60000, &handler);
  239. // Wait for a bit for the worker thread to be started and enter its socket
  240. // select(). Otherwise this test would be trivial since the worker thread
  241. // would process the event as soon as it was started.
  242. Thread::Current()->SleepMs(1000);
  243. // Advance the fake clock, expecting the worker thread to wake up
  244. // and dispatch the message instantly.
  245. clock.AdvanceTime(webrtc::TimeDelta::seconds(60u));
  246. EXPECT_TRUE(message_handler_dispatched.Wait(0));
  247. worker->Stop();
  248. SetClockForTesting(nullptr);
  249. // The message should have been dispatched long before the 60 seconds fully
  250. // elapsed (just a sanity check).
  251. int64_t real_end_time_ms = TimeMillis();
  252. EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
  253. }
  254. } // namespace rtc