diff --git a/api/BUILD.gn b/api/BUILD.gn index 8ec98dfc30..f02b34cc39 100644 --- a/api/BUILD.gn +++ b/api/BUILD.gn @@ -1560,7 +1560,6 @@ if (rtc_include_tests) { "rtp_parameters_unittest.cc", "scoped_refptr_unittest.cc", "sequence_checker_unittest.cc", - "test/create_time_controller_unittest.cc", "test/peerconnection_quality_test_fixture_unittest.cc", ] diff --git a/api/test/create_time_controller.cc b/api/test/create_time_controller.cc index f0fb5acb0e..2a533ada98 100644 --- a/api/test/create_time_controller.cc +++ b/api/test/create_time_controller.cc @@ -26,16 +26,10 @@ #include "pc/media_factory.h" #include "rtc_base/checks.h" #include "system_wrappers/include/clock.h" -#include "test/time_controller/external_time_controller.h" #include "test/time_controller/simulated_time_controller.h" namespace webrtc { -std::unique_ptr CreateTimeController( - ControlledAlarmClock* alarm) { - return std::make_unique(alarm); -} - std::unique_ptr CreateSimulatedTimeController() { return std::make_unique( Timestamp::Seconds(10000)); diff --git a/api/test/create_time_controller.h b/api/test/create_time_controller.h index c8257da650..7d1d9b3800 100644 --- a/api/test/create_time_controller.h +++ b/api/test/create_time_controller.h @@ -17,10 +17,6 @@ namespace webrtc { -// Creates a time coltroller that wraps `alarm`. -std::unique_ptr CreateTimeController( - ControlledAlarmClock* alarm); - // Creates a time controller that runs in simulated time. std::unique_ptr CreateSimulatedTimeController(); diff --git a/api/test/create_time_controller_unittest.cc b/api/test/create_time_controller_unittest.cc deleted file mode 100644 index 365d4aebe3..0000000000 --- a/api/test/create_time_controller_unittest.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2019 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. - */ - -#include "api/test/create_time_controller.h" - -#include - -#include "api/test/time_controller.h" -#include "api/units/time_delta.h" -#include "api/units/timestamp.h" -#include "system_wrappers/include/clock.h" -#include "test/gtest.h" - -namespace webrtc { -namespace { - -class FakeAlarm : public ControlledAlarmClock { - public: - explicit FakeAlarm(Timestamp start_time); - - Clock* GetClock() override; - bool ScheduleAlarmAt(Timestamp deadline) override; - void SetCallback(std::function callback) override; - void Sleep(TimeDelta duration) override; - - private: - SimulatedClock clock_; - Timestamp deadline_; - std::function callback_; -}; - -FakeAlarm::FakeAlarm(Timestamp start_time) - : clock_(start_time), - deadline_(Timestamp::PlusInfinity()), - callback_([] {}) {} - -Clock* FakeAlarm::GetClock() { - return &clock_; -} - -bool FakeAlarm::ScheduleAlarmAt(Timestamp deadline) { - if (deadline < deadline_) { - deadline_ = deadline; - return true; - } - return false; -} - -void FakeAlarm::SetCallback(std::function callback) { - callback_ = callback; -} - -void FakeAlarm::Sleep(TimeDelta duration) { - Timestamp end_time = clock_.CurrentTime() + duration; - - while (deadline_ <= end_time) { - clock_.AdvanceTime(deadline_ - clock_.CurrentTime()); - deadline_ = Timestamp::PlusInfinity(); - callback_(); - } - - clock_.AdvanceTime(end_time - clock_.CurrentTime()); -} - -TEST(CreateTimeControllerTest, CreatesNonNullController) { - FakeAlarm alarm(Timestamp::Millis(100)); - EXPECT_NE(CreateTimeController(&alarm), nullptr); -} - -} // namespace -} // namespace webrtc diff --git a/api/test/time_controller.h b/api/test/time_controller.h index c2b20e2e64..a464f4d49b 100644 --- a/api/test/time_controller.h +++ b/api/test/time_controller.h @@ -62,28 +62,5 @@ class TimeController { TimeDelta max_duration = TimeDelta::Seconds(5)); }; -// Interface for telling time, scheduling an event to fire at a particular time, -// and waiting for time to pass. -class ControlledAlarmClock { - public: - virtual ~ControlledAlarmClock() = default; - - // Gets a clock that tells the alarm clock's notion of time. - virtual Clock* GetClock() = 0; - - // Schedules the alarm to fire at `deadline`. - // An alarm clock only supports one deadline. Calls to `ScheduleAlarmAt` with - // an earlier deadline will reset the alarm to fire earlier.Calls to - // `ScheduleAlarmAt` with a later deadline are ignored. Returns true if the - // deadline changed, false otherwise. - virtual bool ScheduleAlarmAt(Timestamp deadline) = 0; - - // Sets the callback that should be run when the alarm fires. - virtual void SetCallback(std::function callback) = 0; - - // Waits for `duration` to pass, according to the alarm clock. - virtual void Sleep(TimeDelta duration) = 0; -}; - } // namespace webrtc #endif // API_TEST_TIME_CONTROLLER_H_ diff --git a/test/time_controller/BUILD.gn b/test/time_controller/BUILD.gn index e9bd8acdd2..ae4cf58cf0 100644 --- a/test/time_controller/BUILD.gn +++ b/test/time_controller/BUILD.gn @@ -11,8 +11,6 @@ import("../../webrtc.gni") rtc_library("time_controller") { testonly = true sources = [ - "external_time_controller.cc", - "external_time_controller.h", "real_time_controller.cc", "real_time_controller.h", "simulated_task_queue.cc", @@ -48,7 +46,6 @@ if (rtc_include_tests) { rtc_library("time_controller_unittests") { testonly = true sources = [ - "external_time_controller_unittest.cc", "simulated_time_controller_unittest.cc", "time_controller_conformance_test.cc", ] diff --git a/test/time_controller/external_time_controller.cc b/test/time_controller/external_time_controller.cc deleted file mode 100644 index 41f36eaaef..0000000000 --- a/test/time_controller/external_time_controller.cc +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2019 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. - */ -#include "test/time_controller/external_time_controller.h" - -#include -#include -#include -#include - -#include "absl/functional/any_invocable.h" -#include "api/task_queue/task_queue_base.h" -#include "api/task_queue/task_queue_factory.h" -#include "api/units/time_delta.h" -#include "api/units/timestamp.h" -#include "rtc_base/checks.h" -#include "rtc_base/synchronization/yield_policy.h" -#include "test/time_controller/simulated_time_controller.h" - -namespace webrtc { - -// Wraps a TaskQueue so that it can reschedule the time controller whenever -// an external call schedules a new task. -class ExternalTimeController::TaskQueueWrapper : public TaskQueueBase { - public: - TaskQueueWrapper(ExternalTimeController* parent, - std::unique_ptr base) - : parent_(parent), base_(std::move(base)) {} - - void PostTaskImpl(absl::AnyInvocable task, - const PostTaskTraits& traits, - const Location& location) override { - parent_->UpdateTime(); - base_->PostTask(TaskWrapper(std::move(task))); - parent_->ScheduleNext(); - } - - void PostDelayedTaskImpl(absl::AnyInvocable task, - TimeDelta delay, - const PostDelayedTaskTraits& traits, - const Location& location) override { - parent_->UpdateTime(); - if (traits.high_precision) { - base_->PostDelayedHighPrecisionTask(TaskWrapper(std::move(task)), delay); - } else { - base_->PostDelayedTask(TaskWrapper(std::move(task)), delay); - } - parent_->ScheduleNext(); - } - - void Delete() override { delete this; } - - private: - absl::AnyInvocable TaskWrapper( - absl::AnyInvocable task) { - return [task = std::move(task), this]() mutable { - CurrentTaskQueueSetter current(this); - std::move(task)(); - }; - } - - ExternalTimeController* const parent_; - std::unique_ptr base_; -}; - -ExternalTimeController::ExternalTimeController(ControlledAlarmClock* alarm) - : alarm_(alarm), - impl_(alarm_->GetClock()->CurrentTime()), - yield_policy_(&impl_) { - global_clock_.SetTime(alarm_->GetClock()->CurrentTime()); - alarm_->SetCallback([this] { Run(); }); -} - -Clock* ExternalTimeController::GetClock() { - return alarm_->GetClock(); -} - -TaskQueueFactory* ExternalTimeController::GetTaskQueueFactory() { - return this; -} - -void ExternalTimeController::AdvanceTime(TimeDelta duration) { - alarm_->Sleep(duration); -} - -std::unique_ptr ExternalTimeController::CreateThread( - const std::string& name, - std::unique_ptr socket_server) { - RTC_DCHECK_NOTREACHED(); - return nullptr; -} - -rtc::Thread* ExternalTimeController::GetMainThread() { - RTC_DCHECK_NOTREACHED(); - return nullptr; -} - -std::unique_ptr -ExternalTimeController::CreateTaskQueue( - absl::string_view name, - TaskQueueFactory::Priority priority) const { - return std::unique_ptr( - new TaskQueueWrapper(const_cast(this), - impl_.CreateTaskQueue(name, priority))); -} - -void ExternalTimeController::Run() { - rtc::ScopedYieldPolicy yield_policy(&impl_); - UpdateTime(); - impl_.RunReadyRunners(); - ScheduleNext(); -} - -void ExternalTimeController::UpdateTime() { - Timestamp now = alarm_->GetClock()->CurrentTime(); - impl_.AdvanceTime(now); - global_clock_.SetTime(now); -} - -void ExternalTimeController::ScheduleNext() { - RTC_DCHECK_EQ(impl_.CurrentTime(), alarm_->GetClock()->CurrentTime()); - TimeDelta delay = - std::max(impl_.NextRunTime() - impl_.CurrentTime(), TimeDelta::Zero()); - if (delay.IsFinite()) { - alarm_->ScheduleAlarmAt(alarm_->GetClock()->CurrentTime() + delay); - } -} - -} // namespace webrtc diff --git a/test/time_controller/external_time_controller.h b/test/time_controller/external_time_controller.h deleted file mode 100644 index a67f2557b4..0000000000 --- a/test/time_controller/external_time_controller.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2019 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. - */ -#ifndef TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_ -#define TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_ - -#include -#include - -#include "absl/strings/string_view.h" -#include "api/task_queue/task_queue_base.h" -#include "api/task_queue/task_queue_factory.h" -#include "api/test/time_controller.h" -#include "api/units/time_delta.h" -#include "api/units/timestamp.h" -#include "system_wrappers/include/clock.h" -#include "test/time_controller/simulated_time_controller.h" - -namespace webrtc { - -// TimeController implementation built on an external controlled alarm. -// This implementation is used to delegate scheduling and execution to an -// external run loop. -class ExternalTimeController : public TimeController, public TaskQueueFactory { - public: - explicit ExternalTimeController(ControlledAlarmClock* alarm); - - // Implementation of TimeController. - Clock* GetClock() override; - TaskQueueFactory* GetTaskQueueFactory() override; - void AdvanceTime(TimeDelta duration) override; - std::unique_ptr CreateThread( - const std::string& name, - std::unique_ptr socket_server) override; - rtc::Thread* GetMainThread() override; - - // Implementation of TaskQueueFactory. - std::unique_ptr CreateTaskQueue( - absl::string_view name, - TaskQueueFactory::Priority priority) const override; - - private: - class TaskQueueWrapper; - - // Executes any tasks scheduled at or before the current time. May call - // `ScheduleNext` to schedule the next call to `Run`. - void Run(); - - void UpdateTime(); - void ScheduleNext(); - - ControlledAlarmClock* alarm_; - sim_time_impl::SimulatedTimeControllerImpl impl_; - rtc::ScopedYieldPolicy yield_policy_; - - // Overrides the global rtc::Clock to ensure that it reports the same times as - // the time controller. - rtc::ScopedBaseFakeClock global_clock_; -}; - -} // namespace webrtc - -#endif // TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_ diff --git a/test/time_controller/external_time_controller_unittest.cc b/test/time_controller/external_time_controller_unittest.cc deleted file mode 100644 index 302055999a..0000000000 --- a/test/time_controller/external_time_controller_unittest.cc +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2019 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. - */ - -#include "test/time_controller/external_time_controller.h" - -#include -#include -#include - -#include "api/task_queue/task_queue_base.h" -#include "rtc_base/event.h" -#include "rtc_base/task_utils/repeating_task.h" -#include "test/gmock.h" -#include "test/gtest.h" - -// NOTE: Since these tests rely on real time behavior, they will be flaky -// if run on heavily loaded systems. -namespace webrtc { -namespace { -using ::testing::AtLeast; -using ::testing::Invoke; -using ::testing::MockFunction; -using ::testing::NiceMock; -using ::testing::Return; -constexpr Timestamp kStartTime = Timestamp::Seconds(1000); - -class FakeAlarm : public ControlledAlarmClock { - public: - explicit FakeAlarm(Timestamp start_time); - - Clock* GetClock() override; - bool ScheduleAlarmAt(Timestamp deadline) override; - void SetCallback(std::function callback) override; - void Sleep(TimeDelta duration) override; - - private: - SimulatedClock clock_; - Timestamp deadline_; - std::function callback_; -}; - -FakeAlarm::FakeAlarm(Timestamp start_time) - : clock_(start_time), - deadline_(Timestamp::PlusInfinity()), - callback_([] {}) {} - -Clock* FakeAlarm::GetClock() { - return &clock_; -} - -bool FakeAlarm::ScheduleAlarmAt(Timestamp deadline) { - if (deadline < deadline_) { - deadline_ = deadline; - return true; - } - return false; -} - -void FakeAlarm::SetCallback(std::function callback) { - callback_ = callback; -} - -void FakeAlarm::Sleep(TimeDelta duration) { - Timestamp end_time = clock_.CurrentTime() + duration; - - while (deadline_ <= end_time) { - clock_.AdvanceTime(deadline_ - clock_.CurrentTime()); - deadline_ = Timestamp::PlusInfinity(); - callback_(); - } - - clock_.AdvanceTime(end_time - clock_.CurrentTime()); -} - -} // namespace - -TEST(ExternalTimeControllerTest, TaskIsStoppedOnStop) { - const TimeDelta kShortInterval = TimeDelta::Millis(5); - const TimeDelta kLongInterval = TimeDelta::Millis(20); - const int kShortIntervalCount = 4; - const int kMargin = 1; - FakeAlarm alarm(kStartTime); - ExternalTimeController time_simulation(&alarm); - std::unique_ptr task_queue = - time_simulation.GetTaskQueueFactory()->CreateTaskQueue( - "TestQueue", TaskQueueFactory::Priority::NORMAL); - std::atomic_int counter(0); - auto handle = RepeatingTaskHandle::Start(task_queue.get(), [&] { - if (++counter >= kShortIntervalCount) - return kLongInterval; - return kShortInterval; - }); - // Sleep long enough to go through the initial phase. - time_simulation.AdvanceTime(kShortInterval * (kShortIntervalCount + kMargin)); - EXPECT_EQ(counter.load(), kShortIntervalCount); - - task_queue->PostTask( - [handle = std::move(handle)]() mutable { handle.Stop(); }); - - // Sleep long enough that the task would run at least once more if not - // stopped. - time_simulation.AdvanceTime(kLongInterval * 2); - EXPECT_EQ(counter.load(), kShortIntervalCount); -} - -TEST(ExternalTimeControllerTest, TaskCanStopItself) { - std::atomic_int counter(0); - FakeAlarm alarm(kStartTime); - ExternalTimeController time_simulation(&alarm); - std::unique_ptr task_queue = - time_simulation.GetTaskQueueFactory()->CreateTaskQueue( - "TestQueue", TaskQueueFactory::Priority::NORMAL); - - RepeatingTaskHandle handle; - task_queue->PostTask([&] { - handle = RepeatingTaskHandle::Start(task_queue.get(), [&] { - ++counter; - handle.Stop(); - return TimeDelta::Millis(2); - }); - }); - time_simulation.AdvanceTime(TimeDelta::Millis(10)); - EXPECT_EQ(counter.load(), 1); -} - -TEST(ExternalTimeControllerTest, YieldForTask) { - FakeAlarm alarm(kStartTime); - ExternalTimeController time_simulation(&alarm); - - std::unique_ptr task_queue = - time_simulation.GetTaskQueueFactory()->CreateTaskQueue( - "TestQueue", TaskQueueFactory::Priority::NORMAL); - - rtc::Event event; - task_queue->PostTask([&] { event.Set(); }); - EXPECT_TRUE(event.Wait(TimeDelta::Millis(200))); -} - -TEST(ExternalTimeControllerTest, TasksYieldToEachOther) { - FakeAlarm alarm(kStartTime); - ExternalTimeController time_simulation(&alarm); - - std::unique_ptr task_queue = - time_simulation.GetTaskQueueFactory()->CreateTaskQueue( - "TestQueue", TaskQueueFactory::Priority::NORMAL); - std::unique_ptr other_queue = - time_simulation.GetTaskQueueFactory()->CreateTaskQueue( - "OtherQueue", TaskQueueFactory::Priority::NORMAL); - - task_queue->PostTask([&] { - rtc::Event event; - other_queue->PostTask([&] { event.Set(); }); - EXPECT_TRUE(event.Wait(TimeDelta::Millis(200))); - }); - - time_simulation.AdvanceTime(TimeDelta::Millis(300)); -} - -TEST(ExternalTimeControllerTest, CurrentTaskQueue) { - FakeAlarm alarm(kStartTime); - ExternalTimeController time_simulation(&alarm); - - std::unique_ptr task_queue = - time_simulation.GetTaskQueueFactory()->CreateTaskQueue( - "TestQueue", TaskQueueFactory::Priority::NORMAL); - - task_queue->PostTask([&] { EXPECT_TRUE(task_queue->IsCurrent()); }); - - time_simulation.AdvanceTime(TimeDelta::Millis(10)); -} - -} // namespace webrtc