diff options
Diffstat (limited to 'third_party/libwebrtc/net/dcsctp/timer')
7 files changed, 186 insertions, 165 deletions
diff --git a/third_party/libwebrtc/net/dcsctp/timer/BUILD.gn b/third_party/libwebrtc/net/dcsctp/timer/BUILD.gn index d3be1ec872..9dbe11b3ba 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/BUILD.gn +++ b/third_party/libwebrtc/net/dcsctp/timer/BUILD.gn @@ -12,6 +12,8 @@ rtc_library("timer") { deps = [ "../../../api:array_view", "../../../api/task_queue:task_queue", + "../../../api/units:time_delta", + "../../../api/units:timestamp", "../../../rtc_base:checks", "../../../rtc_base:strong_alias", "../../../rtc_base/containers:flat_map", @@ -37,6 +39,7 @@ rtc_library("task_queue_timeout") { "../../../api/task_queue:pending_task_safety_flag", "../../../api/task_queue:task_queue", "../../../api/units:time_delta", + "../../../api/units:timestamp", "../../../rtc_base:checks", "../../../rtc_base:logging", "../public:socket", @@ -59,6 +62,7 @@ if (rtc_include_tests) { "../../../api:array_view", "../../../api/task_queue:task_queue", "../../../api/task_queue/test:mock_task_queue_base", + "../../../api/units:time_delta", "../../../rtc_base:checks", "../../../rtc_base:gunit_helpers", "../../../test:test_support", diff --git a/third_party/libwebrtc/net/dcsctp/timer/fake_timeout.h b/third_party/libwebrtc/net/dcsctp/timer/fake_timeout.h index 4621b2ce83..cac49287d4 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/fake_timeout.h +++ b/third_party/libwebrtc/net/dcsctp/timer/fake_timeout.h @@ -19,6 +19,7 @@ #include "absl/types/optional.h" #include "api/task_queue/task_queue_base.h" +#include "api/units/timestamp.h" #include "net/dcsctp/public/timeout.h" #include "net/dcsctp/public/types.h" #include "rtc_base/checks.h" @@ -29,46 +30,46 @@ namespace dcsctp { // A timeout used in tests. class FakeTimeout : public Timeout { public: - FakeTimeout(std::function<TimeMs()> get_time, + FakeTimeout(std::function<webrtc::Timestamp()> get_time, std::function<void(FakeTimeout*)> on_delete) : get_time_(std::move(get_time)), on_delete_(std::move(on_delete)) {} ~FakeTimeout() override { on_delete_(this); } void Start(DurationMs duration_ms, TimeoutID timeout_id) override { - RTC_DCHECK(expiry_ == TimeMs::InfiniteFuture()); + RTC_DCHECK(expiry_.IsPlusInfinity()); timeout_id_ = timeout_id; - expiry_ = get_time_() + duration_ms; + expiry_ = get_time_() + duration_ms.ToTimeDelta(); } void Stop() override { - RTC_DCHECK(expiry_ != TimeMs::InfiniteFuture()); - expiry_ = TimeMs::InfiniteFuture(); + RTC_DCHECK(!expiry_.IsPlusInfinity()); + expiry_ = webrtc::Timestamp::PlusInfinity(); } - bool EvaluateHasExpired(TimeMs now) { + bool EvaluateHasExpired(webrtc::Timestamp now) { if (now >= expiry_) { - expiry_ = TimeMs::InfiniteFuture(); + expiry_ = webrtc::Timestamp::PlusInfinity(); return true; } return false; } TimeoutID timeout_id() const { return timeout_id_; } - TimeMs expiry() const { return expiry_; } + webrtc::Timestamp expiry() const { return expiry_; } private: - const std::function<TimeMs()> get_time_; + const std::function<webrtc::Timestamp()> get_time_; const std::function<void(FakeTimeout*)> on_delete_; TimeoutID timeout_id_ = TimeoutID(0); - TimeMs expiry_ = TimeMs::InfiniteFuture(); + webrtc::Timestamp expiry_ = webrtc::Timestamp::PlusInfinity(); }; class FakeTimeoutManager { public: // The `get_time` function must return the current time, relative to any // epoch. - explicit FakeTimeoutManager(std::function<TimeMs()> get_time) + explicit FakeTimeoutManager(std::function<webrtc::Timestamp()> get_time) : get_time_(std::move(get_time)) {} std::unique_ptr<FakeTimeout> CreateTimeout() { @@ -89,7 +90,7 @@ class FakeTimeoutManager { // Timer::is_running_ to false before you operate on the Timer or Timeout // again. absl::optional<TimeoutID> GetNextExpiredTimeout() { - TimeMs now = get_time_(); + webrtc::Timestamp now = get_time_(); std::vector<TimeoutID> expired_timers; for (auto& timer : timers_) { if (timer->EvaluateHasExpired(now)) { @@ -99,21 +100,21 @@ class FakeTimeoutManager { return absl::nullopt; } - DurationMs GetTimeToNextTimeout() const { - TimeMs next_expiry = TimeMs::InfiniteFuture(); + webrtc::TimeDelta GetTimeToNextTimeout() const { + webrtc::Timestamp next_expiry = webrtc::Timestamp::PlusInfinity(); for (const FakeTimeout* timer : timers_) { if (timer->expiry() < next_expiry) { next_expiry = timer->expiry(); } } - TimeMs now = get_time_(); - return next_expiry != TimeMs::InfiniteFuture() && next_expiry >= now + webrtc::Timestamp now = get_time_(); + return !next_expiry.IsPlusInfinity() && next_expiry >= now ? next_expiry - now - : DurationMs::InfiniteDuration(); + : webrtc::TimeDelta::PlusInfinity(); } private: - const std::function<TimeMs()> get_time_; + const std::function<webrtc::Timestamp()> get_time_; webrtc::flat_set<FakeTimeout*> timers_; }; diff --git a/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.cc b/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.cc index 6c43640d39..7612f98f3a 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.cc +++ b/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.cc @@ -14,6 +14,8 @@ #include "rtc_base/logging.h" namespace dcsctp { +using ::webrtc::TimeDelta; +using ::webrtc::Timestamp; TaskQueueTimeoutFactory::TaskQueueTimeout::TaskQueueTimeout( TaskQueueTimeoutFactory& parent, @@ -30,8 +32,8 @@ TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() { void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, TimeoutID timeout_id) { RTC_DCHECK_RUN_ON(&parent_.thread_checker_); - RTC_DCHECK(timeout_expiration_ == TimeMs::InfiniteFuture()); - timeout_expiration_ = parent_.get_time_() + duration_ms; + RTC_DCHECK(timeout_expiration_.IsPlusInfinity()); + timeout_expiration_ = parent_.Now() + duration_ms.ToTimeDelta(); timeout_id_ = timeout_id; if (timeout_expiration_ >= posted_task_expiration_) { @@ -43,7 +45,7 @@ void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, return; } - if (posted_task_expiration_ != TimeMs::InfiniteFuture()) { + if (!posted_task_expiration_.IsPlusInfinity()) { RTC_DLOG(LS_VERBOSE) << "New timeout duration is less than scheduled - " "ghosting old delayed task."; // There is already a scheduled delayed task, but its expiration time is @@ -63,10 +65,10 @@ void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, [timeout_id, this]() { RTC_DLOG(LS_VERBOSE) << "Timout expired: " << timeout_id.value(); RTC_DCHECK_RUN_ON(&parent_.thread_checker_); - RTC_DCHECK(posted_task_expiration_ != TimeMs::InfiniteFuture()); - posted_task_expiration_ = TimeMs::InfiniteFuture(); + RTC_DCHECK(!posted_task_expiration_.IsPlusInfinity()); + posted_task_expiration_ = Timestamp::PlusInfinity(); - if (timeout_expiration_ == TimeMs::InfiniteFuture()) { + if (timeout_expiration_.IsPlusInfinity()) { // The timeout was stopped before it expired. Very common. } else { // Note that the timeout might have been restarted, which updated @@ -74,10 +76,10 @@ void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, // if it's not quite time to trigger the timeout yet, schedule a // new delayed task with what's remaining and retry at that point // in time. - DurationMs remaining = timeout_expiration_ - parent_.get_time_(); - timeout_expiration_ = TimeMs::InfiniteFuture(); - if (*remaining > 0) { - Start(remaining, timeout_id_); + TimeDelta remaining = timeout_expiration_ - parent_.Now(); + timeout_expiration_ = Timestamp::PlusInfinity(); + if (remaining > TimeDelta::Zero()) { + Start(DurationMs(remaining.ms()), timeout_id_); } else { // It has actually triggered. RTC_DLOG(LS_VERBOSE) @@ -93,7 +95,7 @@ void TaskQueueTimeoutFactory::TaskQueueTimeout::Stop() { // As the TaskQueue doesn't support deleting a posted task, just mark the // timeout as not running. RTC_DCHECK_RUN_ON(&parent_.thread_checker_); - timeout_expiration_ = TimeMs::InfiniteFuture(); + timeout_expiration_ = Timestamp::PlusInfinity(); } } // namespace dcsctp diff --git a/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.h b/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.h index faae14464f..4b40309f83 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.h +++ b/third_party/libwebrtc/net/dcsctp/timer/task_queue_timeout.h @@ -15,6 +15,7 @@ #include "api/task_queue/pending_task_safety_flag.h" #include "api/task_queue/task_queue_base.h" +#include "api/units/timestamp.h" #include "net/dcsctp/public/timeout.h" namespace dcsctp { @@ -74,14 +75,17 @@ class TaskQueueTimeoutFactory { rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> pending_task_safety_flag_; // The time when the posted delayed task is set to expire. Will be set to // the infinite future if there is no such task running. - TimeMs posted_task_expiration_ = TimeMs::InfiniteFuture(); + webrtc::Timestamp posted_task_expiration_ = + webrtc::Timestamp::PlusInfinity(); // The time when the timeout expires. It will be set to the infinite future // if the timeout is not running/not started. - TimeMs timeout_expiration_ = TimeMs::InfiniteFuture(); + webrtc::Timestamp timeout_expiration_ = webrtc::Timestamp::PlusInfinity(); // The current timeout ID that will be reported when expired. TimeoutID timeout_id_ = TimeoutID(0); }; + webrtc::Timestamp Now() { return webrtc::Timestamp::Millis(*get_time_()); } + RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker thread_checker_; webrtc::TaskQueueBase& task_queue_; const std::function<TimeMs()> get_time_; diff --git a/third_party/libwebrtc/net/dcsctp/timer/timer.cc b/third_party/libwebrtc/net/dcsctp/timer/timer.cc index 208f26fdf9..07c9f3d786 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/timer.cc +++ b/third_party/libwebrtc/net/dcsctp/timer/timer.cc @@ -22,21 +22,23 @@ namespace dcsctp { namespace { +using ::webrtc::TimeDelta; + TimeoutID MakeTimeoutId(TimerID timer_id, TimerGeneration generation) { return TimeoutID(static_cast<uint64_t>(*timer_id) << 32 | *generation); } -DurationMs GetBackoffDuration(const TimerOptions& options, - DurationMs base_duration, - int expiration_count) { +TimeDelta GetBackoffDuration(const TimerOptions& options, + TimeDelta base_duration, + int expiration_count) { switch (options.backoff_algorithm) { case TimerBackoffAlgorithm::kFixed: return base_duration; case TimerBackoffAlgorithm::kExponential: { - DurationMs duration = base_duration; + TimeDelta duration = base_duration; while (expiration_count > 0 && duration < Timer::kMaxTimerDuration) { - duration *= 2; + duration = duration * 2; --expiration_count; if (duration > options.max_backoff_duration) { @@ -44,13 +46,13 @@ DurationMs GetBackoffDuration(const TimerOptions& options, } } - return DurationMs(std::min(duration, Timer::kMaxTimerDuration)); + return TimeDelta(std::min(duration, Timer::kMaxTimerDuration)); } } } } // namespace -constexpr DurationMs Timer::kMaxTimerDuration; +constexpr TimeDelta Timer::kMaxTimerDuration; Timer::Timer(TimerID id, absl::string_view name, @@ -76,12 +78,12 @@ void Timer::Start() { if (!is_running()) { is_running_ = true; generation_ = TimerGeneration(*generation_ + 1); - timeout_->Start(duration_, MakeTimeoutId(id_, generation_)); + timeout_->Start(DurationMs(duration_), MakeTimeoutId(id_, generation_)); } else { // Timer was running - stop and restart it, to make it expire in `duration_` // from now. generation_ = TimerGeneration(*generation_ + 1); - timeout_->Restart(duration_, MakeTimeoutId(id_, generation_)); + timeout_->Restart(DurationMs(duration_), MakeTimeoutId(id_, generation_)); } } @@ -103,23 +105,24 @@ void Timer::Trigger(TimerGeneration generation) { // timer. Note that it might be very quickly restarted again, if the // `on_expired_` callback returns a new duration. is_running_ = true; - DurationMs duration = + TimeDelta duration = GetBackoffDuration(options_, duration_, expiration_count_); generation_ = TimerGeneration(*generation_ + 1); - timeout_->Start(duration, MakeTimeoutId(id_, generation_)); + timeout_->Start(DurationMs(duration), MakeTimeoutId(id_, generation_)); } - absl::optional<DurationMs> new_duration = on_expired_(); - if (new_duration.has_value() && new_duration != duration_) { - duration_ = new_duration.value(); + TimeDelta new_duration = on_expired_(); + RTC_DCHECK(new_duration != TimeDelta::PlusInfinity()); + if (new_duration > TimeDelta::Zero() && new_duration != duration_) { + duration_ = new_duration; if (is_running_) { // Restart it with new duration. timeout_->Stop(); - DurationMs duration = + TimeDelta duration = GetBackoffDuration(options_, duration_, expiration_count_); generation_ = TimerGeneration(*generation_ + 1); - timeout_->Start(duration, MakeTimeoutId(id_, generation_)); + timeout_->Start(DurationMs(duration), MakeTimeoutId(id_, generation_)); } } } diff --git a/third_party/libwebrtc/net/dcsctp/timer/timer.h b/third_party/libwebrtc/net/dcsctp/timer/timer.h index 95aae570c8..30b07f9bfa 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/timer.h +++ b/third_party/libwebrtc/net/dcsctp/timer/timer.h @@ -22,6 +22,7 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "api/task_queue/task_queue_base.h" +#include "api/units/time_delta.h" #include "net/dcsctp/public/timeout.h" #include "rtc_base/strong_alias.h" @@ -40,30 +41,31 @@ enum class TimerBackoffAlgorithm { }; struct TimerOptions { - explicit TimerOptions(DurationMs duration) + explicit TimerOptions(webrtc::TimeDelta duration) : TimerOptions(duration, TimerBackoffAlgorithm::kExponential) {} - TimerOptions(DurationMs duration, TimerBackoffAlgorithm backoff_algorithm) + TimerOptions(webrtc::TimeDelta duration, + TimerBackoffAlgorithm backoff_algorithm) : TimerOptions(duration, backoff_algorithm, absl::nullopt) {} - TimerOptions(DurationMs duration, + TimerOptions(webrtc::TimeDelta duration, TimerBackoffAlgorithm backoff_algorithm, absl::optional<int> max_restarts) : TimerOptions(duration, backoff_algorithm, max_restarts, - DurationMs::InfiniteDuration()) {} - TimerOptions(DurationMs duration, + webrtc::TimeDelta::PlusInfinity()) {} + TimerOptions(webrtc::TimeDelta duration, TimerBackoffAlgorithm backoff_algorithm, absl::optional<int> max_restarts, - DurationMs max_backoff_duration) + webrtc::TimeDelta max_backoff_duration) : TimerOptions(duration, backoff_algorithm, max_restarts, max_backoff_duration, webrtc::TaskQueueBase::DelayPrecision::kLow) {} - TimerOptions(DurationMs duration, + TimerOptions(webrtc::TimeDelta duration, TimerBackoffAlgorithm backoff_algorithm, absl::optional<int> max_restarts, - DurationMs max_backoff_duration, + webrtc::TimeDelta max_backoff_duration, webrtc::TaskQueueBase::DelayPrecision precision) : duration(duration), backoff_algorithm(backoff_algorithm), @@ -72,7 +74,7 @@ struct TimerOptions { precision(precision) {} // The initial timer duration. Can be overridden with `set_duration`. - const DurationMs duration; + const webrtc::TimeDelta duration; // If the duration should be increased (using exponential backoff) when it is // restarted. If not set, the same duration will be used. const TimerBackoffAlgorithm backoff_algorithm; @@ -80,7 +82,7 @@ struct TimerOptions { // or absl::nullopt if there is no limit. const absl::optional<int> max_restarts; // The maximum timeout value for exponential backoff. - const DurationMs max_backoff_duration; + const webrtc::TimeDelta max_backoff_duration; // The precision of the webrtc::TaskQueueBase used for scheduling. const webrtc::TaskQueueBase::DelayPrecision precision; }; @@ -100,12 +102,14 @@ struct TimerOptions { class Timer { public: // The maximum timer duration - one day. - static constexpr DurationMs kMaxTimerDuration = DurationMs(24 * 3600 * 1000); + static constexpr webrtc::TimeDelta kMaxTimerDuration = + webrtc::TimeDelta::Seconds(24 * 3600); - // When expired, the timer handler can optionally return a new duration which - // will be set as `duration` and used as base duration when the timer is - // restarted and as input to the backoff algorithm. - using OnExpired = std::function<absl::optional<DurationMs>()>; + // When expired, the timer handler can optionally return a new non-zero + // duration which will be set as `duration` and used as base duration when the + // timer is restarted and as input to the backoff algorithm. If zero is + // returned, the current duration is used. + using OnExpired = std::function<webrtc::TimeDelta()>; // TimerManager will have pointers to these instances, so they must not move. Timer(const Timer&) = delete; @@ -123,13 +127,13 @@ class Timer { // Sets the base duration. The actual timer duration may be larger depending // on the backoff algorithm. - void set_duration(DurationMs duration) { + void set_duration(webrtc::TimeDelta duration) { duration_ = std::min(duration, kMaxTimerDuration); } // Retrieves the base duration. The actual timer duration may be larger // depending on the backoff algorithm. - DurationMs duration() const { return duration_; } + webrtc::TimeDelta duration() const { return duration_; } // Returns the number of times the timer has expired. int expiration_count() const { return expiration_count_; } @@ -167,7 +171,7 @@ class Timer { const UnregisterHandler unregister_handler_; const std::unique_ptr<Timeout> timeout_; - DurationMs duration_; + webrtc::TimeDelta duration_; // Increased on each start, and is matched on Trigger, to avoid races. And by // race, meaning that a timeout - which may be evaluated/expired on a diff --git a/third_party/libwebrtc/net/dcsctp/timer/timer_test.cc b/third_party/libwebrtc/net/dcsctp/timer/timer_test.cc index 93876160bb..9a7c029ec5 100644 --- a/third_party/libwebrtc/net/dcsctp/timer/timer_test.cc +++ b/third_party/libwebrtc/net/dcsctp/timer/timer_test.cc @@ -13,6 +13,7 @@ #include "absl/types/optional.h" #include "api/task_queue/task_queue_base.h" +#include "api/units/time_delta.h" #include "net/dcsctp/public/timeout.h" #include "net/dcsctp/timer/fake_timeout.h" #include "rtc_base/gunit.h" @@ -21,6 +22,8 @@ namespace dcsctp { namespace { using ::testing::Return; +using ::webrtc::TimeDelta; +using ::webrtc::Timestamp; class TimerTest : public testing::Test { protected: @@ -29,10 +32,10 @@ class TimerTest : public testing::Test { manager_([this](webrtc::TaskQueueBase::DelayPrecision precision) { return timeout_manager_.CreateTimeout(precision); }) { - ON_CALL(on_expired_, Call).WillByDefault(Return(absl::nullopt)); + ON_CALL(on_expired_, Call).WillByDefault(Return(TimeDelta::Zero())); } - void AdvanceTimeAndRunTimers(DurationMs duration) { + void AdvanceTimeAndRunTimers(TimeDelta duration) { now_ = now_ + duration; for (;;) { @@ -45,16 +48,16 @@ class TimerTest : public testing::Test { } } - TimeMs now_ = TimeMs(0); + Timestamp now_ = Timestamp::Zero(); FakeTimeoutManager timeout_manager_; TimerManager manager_; - testing::MockFunction<absl::optional<DurationMs>()> on_expired_; + testing::MockFunction<TimeDelta()> on_expired_; }; TEST_F(TimerTest, TimerIsInitiallyStopped) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed)); EXPECT_FALSE(t1->is_running()); } @@ -62,50 +65,50 @@ TEST_F(TimerTest, TimerIsInitiallyStopped) { TEST_F(TimerTest, TimerExpiresAtGivenTime) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed)); EXPECT_CALL(on_expired_, Call).Times(0); t1->Start(); EXPECT_TRUE(t1->is_running()); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); } TEST_F(TimerTest, TimerReschedulesAfterExpiredWithFixedBackoff) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed)); EXPECT_CALL(on_expired_, Call).Times(0); t1->Start(); EXPECT_EQ(t1->expiration_count(), 0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Fire first time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_TRUE(t1->is_running()); EXPECT_EQ(t1->expiration_count(), 1); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Second time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_TRUE(t1->is_running()); EXPECT_EQ(t1->expiration_count(), 2); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Third time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_TRUE(t1->is_running()); EXPECT_EQ(t1->expiration_count(), 3); } @@ -113,151 +116,151 @@ TEST_F(TimerTest, TimerReschedulesAfterExpiredWithFixedBackoff) { TEST_F(TimerTest, TimerWithNoRestarts) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed, + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed, /*max_restart=*/0)); EXPECT_CALL(on_expired_, Call).Times(0); t1->Start(); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Fire first time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_FALSE(t1->is_running()); // Second time - shouldn't fire EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(5000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(5)); EXPECT_FALSE(t1->is_running()); } TEST_F(TimerTest, TimerWithOneRestart) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed, + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed, /*max_restart=*/1)); EXPECT_CALL(on_expired_, Call).Times(0); t1->Start(); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Fire first time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_TRUE(t1->is_running()); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Second time - max restart limit reached. EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_FALSE(t1->is_running()); // Third time - should not fire. EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(5000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(5)); EXPECT_FALSE(t1->is_running()); } TEST_F(TimerTest, TimerWithTwoRestart) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed, + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed, /*max_restart=*/2)); EXPECT_CALL(on_expired_, Call).Times(0); t1->Start(); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Fire first time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_TRUE(t1->is_running()); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Second time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_TRUE(t1->is_running()); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Third time EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_FALSE(t1->is_running()); } TEST_F(TimerTest, TimerWithExponentialBackoff) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kExponential)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kExponential)); t1->Start(); // Fire first time at 5 seconds EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(5000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(5)); // Second time at 5*2^1 = 10 seconds later. EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(9000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(9)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); // Third time at 5*2^2 = 20 seconds later. EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(19000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(19)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); // Fourth time at 5*2^3 = 40 seconds later. EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(39000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(39)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); } TEST_F(TimerTest, StartTimerWillStopAndStart) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kExponential)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kExponential)); t1->Start(); - AdvanceTimeAndRunTimers(DurationMs(3000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(3)); t1->Start(); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(2000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(2)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(3000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(3)); } TEST_F(TimerTest, ExpirationCounterWillResetIfStopped) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kExponential)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kExponential)); t1->Start(); // Fire first time at 5 seconds EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(5000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(5)); EXPECT_EQ(t1->expiration_count(), 1); // Second time at 5*2^1 = 10 seconds later. EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(9000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(9)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_EQ(t1->expiration_count(), 2); t1->Start(); @@ -265,79 +268,79 @@ TEST_F(TimerTest, ExpirationCounterWillResetIfStopped) { // Third time at 5*2^0 = 5 seconds later. EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_EQ(t1->expiration_count(), 1); } TEST_F(TimerTest, StopTimerWillMakeItNotExpire) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kExponential)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kExponential)); t1->Start(); EXPECT_TRUE(t1->is_running()); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); t1->Stop(); EXPECT_FALSE(t1->is_running()); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); } TEST_F(TimerTest, ReturningNewDurationWhenExpired) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(5000), TimerBackoffAlgorithm::kFixed)); + TimerOptions(TimeDelta::Seconds(5), TimerBackoffAlgorithm::kFixed)); EXPECT_CALL(on_expired_, Call).Times(0); t1->Start(); - EXPECT_EQ(t1->duration(), DurationMs(5000)); + EXPECT_EQ(t1->duration(), TimeDelta::Seconds(5)); - AdvanceTimeAndRunTimers(DurationMs(4000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(4)); // Fire first time - EXPECT_CALL(on_expired_, Call).WillOnce(Return(DurationMs(2000))); - AdvanceTimeAndRunTimers(DurationMs(1000)); - EXPECT_EQ(t1->duration(), DurationMs(2000)); + EXPECT_CALL(on_expired_, Call).WillOnce(Return(TimeDelta::Seconds(2))); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); + EXPECT_EQ(t1->duration(), TimeDelta::Seconds(2)); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); // Second time - EXPECT_CALL(on_expired_, Call).WillOnce(Return(DurationMs(10000))); - AdvanceTimeAndRunTimers(DurationMs(1000)); - EXPECT_EQ(t1->duration(), DurationMs(10000)); + EXPECT_CALL(on_expired_, Call).WillOnce(Return(TimeDelta::Seconds(10))); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); + EXPECT_EQ(t1->duration(), TimeDelta::Seconds(10)); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(9000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(9)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); } TEST_F(TimerTest, TimersHaveMaximumDuration) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(1000), TimerBackoffAlgorithm::kExponential)); + TimerOptions(TimeDelta::Seconds(1), TimerBackoffAlgorithm::kExponential)); - t1->set_duration(DurationMs(2 * *Timer::kMaxTimerDuration)); + t1->set_duration(2 * Timer::kMaxTimerDuration); EXPECT_EQ(t1->duration(), Timer::kMaxTimerDuration); } TEST_F(TimerTest, TimersHaveMaximumBackoffDuration) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(1000), TimerBackoffAlgorithm::kExponential)); + TimerOptions(TimeDelta::Seconds(1), TimerBackoffAlgorithm::kExponential)); t1->Start(); - int max_exponent = static_cast<int>(log2(*Timer::kMaxTimerDuration / 1000)); + int max_exponent = static_cast<int>(log2(Timer::kMaxTimerDuration.seconds())); for (int i = 0; i < max_exponent; ++i) { EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000 * (1 << i))); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1 * (1 << i))); } // Reached the maximum duration. @@ -357,77 +360,77 @@ TEST_F(TimerTest, TimersHaveMaximumBackoffDuration) { TEST_F(TimerTest, TimerCanBeStartedFromWithinExpirationHandler) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(1000), TimerBackoffAlgorithm::kFixed)); + TimerOptions(TimeDelta::Seconds(1), TimerBackoffAlgorithm::kFixed)); t1->Start(); // Start a timer, but don't return any new duration in callback. EXPECT_CALL(on_expired_, Call).WillOnce([&]() { EXPECT_TRUE(t1->is_running()); - t1->set_duration(DurationMs(5000)); + t1->set_duration(TimeDelta::Seconds(5)); t1->Start(); - return absl::nullopt; + return TimeDelta::Zero(); }); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4999)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(4999)); // Start a timer, and return any new duration in callback. EXPECT_CALL(on_expired_, Call).WillOnce([&]() { EXPECT_TRUE(t1->is_running()); - t1->set_duration(DurationMs(5000)); + t1->set_duration(TimeDelta::Seconds(5)); t1->Start(); - return absl::make_optional(DurationMs(8000)); + return TimeDelta::Seconds(8); }); - AdvanceTimeAndRunTimers(DurationMs(1)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1)); EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(7999)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(7999)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1)); } TEST_F(TimerTest, DurationStaysWithinMaxTimerBackOffDuration) { std::unique_ptr<Timer> t1 = manager_.CreateTimer( "t1", on_expired_.AsStdFunction(), - TimerOptions(DurationMs(1000), TimerBackoffAlgorithm::kExponential, - /*max_restarts=*/absl::nullopt, DurationMs(5000))); + TimerOptions(TimeDelta::Seconds(1), TimerBackoffAlgorithm::kExponential, + /*max_restarts=*/absl::nullopt, TimeDelta::Seconds(5))); t1->Start(); // Initial timeout, 1000 ms EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1000)); + AdvanceTimeAndRunTimers(TimeDelta::Seconds(1)); // Exponential backoff -> 2000 ms EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(1999)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1999)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1)); // Exponential backoff -> 4000 ms EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(3999)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(3999)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1)); // Limited backoff -> 5000ms EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4999)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(4999)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1)); // ... where it plateaus EXPECT_CALL(on_expired_, Call).Times(0); - AdvanceTimeAndRunTimers(DurationMs(4999)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(4999)); EXPECT_CALL(on_expired_, Call).Times(1); - AdvanceTimeAndRunTimers(DurationMs(1)); + AdvanceTimeAndRunTimers(TimeDelta::Millis(1)); } TEST(TimerManagerTest, TimerManagerPassesPrecisionToCreateTimeoutMethod) { - FakeTimeoutManager timeout_manager([&]() { return TimeMs(0); }); + FakeTimeoutManager timeout_manager([&]() { return Timestamp::Zero(); }); absl::optional<webrtc::TaskQueueBase::DelayPrecision> create_timer_precison; TimerManager manager([&](webrtc::TaskQueueBase::DelayPrecision precision) { create_timer_precison = precision; @@ -435,22 +438,22 @@ TEST(TimerManagerTest, TimerManagerPassesPrecisionToCreateTimeoutMethod) { }); // Default TimerOptions. manager.CreateTimer( - "test_timer", []() { return absl::optional<DurationMs>(); }, - TimerOptions(DurationMs(123))); + "test_timer", []() { return TimeDelta::Zero(); }, + TimerOptions(TimeDelta::Millis(123))); EXPECT_EQ(create_timer_precison, webrtc::TaskQueueBase::DelayPrecision::kLow); // High precision TimerOptions. manager.CreateTimer( - "test_timer", []() { return absl::optional<DurationMs>(); }, - TimerOptions(DurationMs(123), TimerBackoffAlgorithm::kExponential, - absl::nullopt, DurationMs::InfiniteDuration(), + "test_timer", []() { return TimeDelta::Zero(); }, + TimerOptions(TimeDelta::Millis(123), TimerBackoffAlgorithm::kExponential, + absl::nullopt, TimeDelta::PlusInfinity(), webrtc::TaskQueueBase::DelayPrecision::kHigh)); EXPECT_EQ(create_timer_precison, webrtc::TaskQueueBase::DelayPrecision::kHigh); // Low precision TimerOptions. manager.CreateTimer( - "test_timer", []() { return absl::optional<DurationMs>(); }, - TimerOptions(DurationMs(123), TimerBackoffAlgorithm::kExponential, - absl::nullopt, DurationMs::InfiniteDuration(), + "test_timer", []() { return TimeDelta::Zero(); }, + TimerOptions(TimeDelta::Millis(123), TimerBackoffAlgorithm::kExponential, + absl::nullopt, TimeDelta::PlusInfinity(), webrtc::TaskQueueBase::DelayPrecision::kLow)); EXPECT_EQ(create_timer_precison, webrtc::TaskQueueBase::DelayPrecision::kLow); } |