time_controller.cc (1791B)
1 /* 2 * Copyright (c) 2020 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 "api/test/time_controller.h" 11 12 #include <functional> 13 #include <memory> 14 15 #include "absl/strings/string_view.h" 16 #include "api/task_queue/task_queue_base.h" 17 #include "api/task_queue/task_queue_factory.h" 18 #include "api/units/time_delta.h" 19 20 namespace webrtc { 21 std::unique_ptr<TaskQueueFactory> TimeController::CreateTaskQueueFactory() { 22 class FactoryWrapper final : public TaskQueueFactory { 23 public: 24 explicit FactoryWrapper(TaskQueueFactory* inner_factory) 25 : inner_(inner_factory) {} 26 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( 27 absl::string_view name, 28 Priority priority) const override { 29 return inner_->CreateTaskQueue(name, priority); 30 } 31 32 private: 33 TaskQueueFactory* const inner_; 34 }; 35 return std::make_unique<FactoryWrapper>(GetTaskQueueFactory()); 36 } 37 bool TimeController::Wait(const std::function<bool()>& condition, 38 TimeDelta max_duration) { 39 // Step size is chosen to be short enough to not significantly affect latency 40 // in real time tests while being long enough to avoid adding too much load to 41 // the system. 42 const auto kStep = TimeDelta::Millis(5); 43 for (auto elapsed = TimeDelta::Zero(); elapsed < max_duration; 44 elapsed += kStep) { 45 if (condition()) 46 return true; 47 AdvanceTime(kStep); 48 } 49 return condition(); 50 } 51 } // namespace webrtc