blob: b3b2f463c5f854e8b24382d10d7b5c25cd87b1c1 [file] [log] [blame]
Sebastian Jansson6ce033a2020-01-22 10:12:56 +01001/*
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
12namespace webrtc {
Sebastian Jansson094ce2e2020-01-22 14:37:52 +010013std::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}
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010029bool TimeController::Wait(const std::function<bool()>& done,
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::ms(5);
35 for (auto elapsed = TimeDelta::Zero(); elapsed < max_duration;
36 elapsed += kStep) {
37 if (done())
38 return true;
39 AdvanceTime(kStep);
40 }
41 return done();
42}
43} // namespace webrtc