Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 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 { |
Sebastian Jansson | 094ce2e | 2020-01-22 14:37:52 +0100 | [diff] [blame] | 13 | std::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 Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 29 | bool 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 |