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