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 namespace webrtc { CreateTaskQueueFactory()13std::unique_ptr<TaskQueueFactory> TimeController::CreateTaskQueueFactory() { 14 class FactoryWrapper final : public TaskQueueFactory { 15 public: 16 explicit FactoryWrapper(TaskQueueFactory* inner_factory) 17 : inner_(inner_factory) {} 18 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( 19 absl::string_view name, 20 Priority priority) const override { 21 return inner_->CreateTaskQueue(name, priority); 22 } 23 24 private: 25 TaskQueueFactory* const inner_; 26 }; 27 return std::make_unique<FactoryWrapper>(GetTaskQueueFactory()); 28 } Wait(const std::function<bool ()> & condition,TimeDelta max_duration)29bool TimeController::Wait(const std::function<bool()>& condition, 30 TimeDelta max_duration) { 31 // Step size is chosen to be short enough to not significantly affect latency 32 // in real time tests while being long enough to avoid adding too much load to 33 // the system. 34 const auto kStep = TimeDelta::Millis(5); 35 for (auto elapsed = TimeDelta::Zero(); elapsed < max_duration; 36 elapsed += kStep) { 37 if (condition()) 38 return true; 39 AdvanceTime(kStep); 40 } 41 return condition(); 42 } 43 } // namespace webrtc 44