blob: 364dbc235d02bc636fe8765e55fc2e02b023de31 [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}
Artem Titovb5a01382020-04-29 11:09:59 +020029bool TimeController::Wait(const std::function<bool()>& condition,
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010030 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.
Danil Chapovalov0c626af2020-02-10 11:16:00 +010034 const auto kStep = TimeDelta::Millis(5);
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010035 for (auto elapsed = TimeDelta::Zero(); elapsed < max_duration;
36 elapsed += kStep) {
Artem Titovb5a01382020-04-29 11:09:59 +020037 if (condition())
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010038 return true;
39 AdvanceTime(kStep);
40 }
Artem Titovb5a01382020-04-29 11:09:59 +020041 return condition();
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010042}
43} // namespace webrtc