1 /*
2 * Copyright (c) 2019 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
11 #include "test/pc/e2e/test_activities_executor.h"
12
13 #include <memory>
14 #include <utility>
15
16 #include "absl/memory/memory.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19 #include "rtc_base/task_queue_for_test.h"
20
21 namespace webrtc {
22 namespace webrtc_pc_e2e {
23
Start(TaskQueueBase * task_queue)24 void TestActivitiesExecutor::Start(TaskQueueBase* task_queue) {
25 RTC_DCHECK(task_queue);
26 task_queue_ = task_queue;
27 MutexLock lock(&lock_);
28 start_time_ = Now();
29 while (!scheduled_activities_.empty()) {
30 PostActivity(std::move(scheduled_activities_.front()));
31 scheduled_activities_.pop();
32 }
33 }
34
Stop()35 void TestActivitiesExecutor::Stop() {
36 if (task_queue_ == nullptr) {
37 // Already stopped or not started.
38 return;
39 }
40 SendTask(task_queue_, [this]() {
41 MutexLock lock(&lock_);
42 for (auto& handle : repeating_task_handles_) {
43 handle.Stop();
44 }
45 });
46 task_queue_ = nullptr;
47 }
48
ScheduleActivity(TimeDelta initial_delay_since_start,absl::optional<TimeDelta> interval,std::function<void (TimeDelta)> func)49 void TestActivitiesExecutor::ScheduleActivity(
50 TimeDelta initial_delay_since_start,
51 absl::optional<TimeDelta> interval,
52 std::function<void(TimeDelta)> func) {
53 RTC_CHECK(initial_delay_since_start.IsFinite() &&
54 initial_delay_since_start >= TimeDelta::Zero());
55 RTC_CHECK(!interval ||
56 (interval->IsFinite() && *interval > TimeDelta::Zero()));
57 MutexLock lock(&lock_);
58 ScheduledActivity activity(initial_delay_since_start, interval, func);
59 if (start_time_.IsInfinite()) {
60 scheduled_activities_.push(std::move(activity));
61 } else {
62 PostActivity(std::move(activity));
63 }
64 }
65
PostActivity(ScheduledActivity activity)66 void TestActivitiesExecutor::PostActivity(ScheduledActivity activity) {
67 // Because start_time_ will never change at this point copy it to local
68 // variable to capture in in lambda without requirement to hold a lock.
69 Timestamp start_time = start_time_;
70
71 TimeDelta remaining_delay =
72 activity.initial_delay_since_start == TimeDelta::Zero()
73 ? TimeDelta::Zero()
74 : activity.initial_delay_since_start - (Now() - start_time);
75 if (remaining_delay < TimeDelta::Zero()) {
76 RTC_LOG(LS_WARNING) << "Executing late task immediately, late by="
77 << ToString(remaining_delay.Abs());
78 remaining_delay = TimeDelta::Zero();
79 }
80
81 if (activity.interval) {
82 if (remaining_delay == TimeDelta::Zero()) {
83 repeating_task_handles_.push_back(RepeatingTaskHandle::Start(
84 task_queue_, [activity, start_time, this]() {
85 activity.func(Now() - start_time);
86 return *activity.interval;
87 }));
88 return;
89 }
90 repeating_task_handles_.push_back(RepeatingTaskHandle::DelayedStart(
91 task_queue_, remaining_delay, [activity, start_time, this]() {
92 activity.func(Now() - start_time);
93 return *activity.interval;
94 }));
95 return;
96 }
97
98 if (remaining_delay == TimeDelta::Zero()) {
99 task_queue_->PostTask(
100 [activity, start_time, this]() { activity.func(Now() - start_time); });
101 return;
102 }
103
104 task_queue_->PostDelayedTask(
105 [activity, start_time, this]() { activity.func(Now() - start_time); },
106 remaining_delay);
107 }
108
Now() const109 Timestamp TestActivitiesExecutor::Now() const {
110 return clock_->CurrentTime();
111 }
112
ScheduledActivity(TimeDelta initial_delay_since_start,absl::optional<TimeDelta> interval,std::function<void (TimeDelta)> func)113 TestActivitiesExecutor::ScheduledActivity::ScheduledActivity(
114 TimeDelta initial_delay_since_start,
115 absl::optional<TimeDelta> interval,
116 std::function<void(TimeDelta)> func)
117 : initial_delay_since_start(initial_delay_since_start),
118 interval(interval),
119 func(std::move(func)) {}
120
121 } // namespace webrtc_pc_e2e
122 } // namespace webrtc
123