/* * Copyright 2004 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
TEST(TimeTest, TimeInMs) {
int64_t ts_earlier = TimeMillis();
Thread::SleepMs(100);
int64_t ts_now = TimeMillis(); // Allow for the thread to wakeup ~20ms early.
EXPECT_GE(ts_now, ts_earlier + 80); // Make sure the Time is not returning in smaller unit like microseconds.
EXPECT_LT(ts_now, ts_earlier + 1000);
}
// We can't depend on ts_later and ts_earlier to be exactly 500 apart // since time elapses between the calls to TimeMillis() and TimeAfter(500)
EXPECT_LE(500, TimeDiff(ts_later, ts_earlier));
EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
// Time has elapsed since ts_earlier
EXPECT_GE(TimeSince(ts_earlier), 0);
// ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
EXPECT_LE(TimeUntil(ts_earlier), 0);
// ts_later likely hasn't happened yet, so TimeSince could be -ve // but within 500
EXPECT_GE(TimeSince(ts_later), -500);
// TimeUntil ts_later is at most 500
EXPECT_LE(TimeUntil(ts_later), 500);
}
class TmToSeconds : public ::testing::Test { public:
TmToSeconds() { // Set use of the test RNG to get deterministic expiration timestamp.
rtc::SetRandomTestMode(true);
}
~TmToSeconds() override { // Put it back for the next test.
rtc::SetRandomTestMode(false);
}
void TestTmToSeconds(int times) { staticchar mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (int i = 0; i < times; i++) { // First generate something correct and check that TmToSeconds is happy. int year = rtc::CreateRandomId() % 400 + 1970;
// Test that all the time functions exposed by TimeUtils get time from the // fake clock when it's set.
TEST(FakeClock, TimeFunctionsUseFakeClock) {
FakeClock clock;
SetClockForTesting(&clock);
// When the clock is advanced, threads that are waiting in a socket select // should wake up and look at the new time. This allows tests using the // fake clock to run much faster, if the test is bound by time constraints // (such as a test for a STUN ping timeout).
TEST(FakeClock, SettingTimeWakesThreads) {
int64_t real_start_time_ms = TimeMillis();
// Post an event that won't be executed for 10 seconds.
Event message_handler_dispatched;
worker->PostDelayedTask(
[&message_handler_dispatched] { message_handler_dispatched.Set(); },
TimeDelta::Seconds(60));
// Wait for a bit for the worker thread to be started and enter its socket // select(). Otherwise this test would be trivial since the worker thread // would process the event as soon as it was started.
Thread::Current()->SleepMs(1000);
// Advance the fake clock, expecting the worker thread to wake up // and dispatch the message instantly.
clock.AdvanceTime(webrtc::TimeDelta::Seconds(60u));
EXPECT_TRUE(message_handler_dispatched.Wait(webrtc::TimeDelta::Zero()));
worker->Stop();
SetClockForTesting(nullptr);
// The message should have been dispatched long before the 60 seconds fully // elapsed (just a sanity check).
int64_t real_end_time_ms = TimeMillis();
EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
}
} // namespace rtc
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-06-05)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.