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 #ifndef TEST_PC_E2E_TEST_ACTIVITIES_EXECUTOR_H_ 12 #define TEST_PC_E2E_TEST_ACTIVITIES_EXECUTOR_H_ 13 14 #include <queue> 15 #include <vector> 16 17 #include "absl/types/optional.h" 18 #include "api/units/time_delta.h" 19 #include "api/units/timestamp.h" 20 #include "rtc_base/synchronization/mutex.h" 21 #include "rtc_base/task_queue_for_test.h" 22 #include "rtc_base/task_utils/repeating_task.h" 23 #include "system_wrappers/include/clock.h" 24 25 namespace webrtc { 26 namespace webrtc_pc_e2e { 27 28 class TestActivitiesExecutor { 29 public: TestActivitiesExecutor(Clock * clock)30 explicit TestActivitiesExecutor(Clock* clock) : clock_(clock) {} ~TestActivitiesExecutor()31 ~TestActivitiesExecutor() { Stop(); } 32 33 // Starts scheduled activities according to their schedule. All activities 34 // that will be scheduled after Start(...) was invoked will be executed 35 // immediately according to their schedule. 36 void Start(TaskQueueForTest* task_queue); 37 void Stop(); 38 39 // Schedule activity to be executed. If test isn't started yet, then activity 40 // will be executed according to its schedule after Start() will be invoked. 41 // If test is started, then it will be executed immediately according to its 42 // schedule. 43 void ScheduleActivity(TimeDelta initial_delay_since_start, 44 absl::optional<TimeDelta> interval, 45 std::function<void(TimeDelta)> func); 46 47 private: 48 struct ScheduledActivity { 49 ScheduledActivity(TimeDelta initial_delay_since_start, 50 absl::optional<TimeDelta> interval, 51 std::function<void(TimeDelta)> func); 52 53 TimeDelta initial_delay_since_start; 54 absl::optional<TimeDelta> interval; 55 std::function<void(TimeDelta)> func; 56 }; 57 58 void PostActivity(ScheduledActivity activity) 59 RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); 60 Timestamp Now() const; 61 62 Clock* const clock_; 63 64 TaskQueueForTest* task_queue_; 65 66 Mutex lock_; 67 // Time when test was started. Minus infinity means that it wasn't started 68 // yet. 69 Timestamp start_time_ RTC_GUARDED_BY(lock_) = Timestamp::MinusInfinity(); 70 // Queue of activities that were added before test was started. 71 // Activities from this queue will be posted on the |task_queue_| after test 72 // will be set up and then this queue will be unused. 73 std::queue<ScheduledActivity> scheduled_activities_ RTC_GUARDED_BY(lock_); 74 // List of task handles for activities, that are posted on |task_queue_| as 75 // repeated during the call. 76 std::vector<RepeatingTaskHandle> repeating_task_handles_ 77 RTC_GUARDED_BY(lock_); 78 }; 79 80 } // namespace webrtc_pc_e2e 81 } // namespace webrtc 82 83 #endif // TEST_PC_E2E_TEST_ACTIVITIES_EXECUTOR_H_ 84