test_activities_executor.h 2.9 KB

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