task_queue_for_test.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2018 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 RTC_BASE_TASK_QUEUE_FOR_TEST_H_
  11. #define RTC_BASE_TASK_QUEUE_FOR_TEST_H_
  12. #include <utility>
  13. #include "absl/strings/string_view.h"
  14. #include "api/task_queue/task_queue_base.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/event.h"
  17. #include "rtc_base/location.h"
  18. #include "rtc_base/task_queue.h"
  19. #include "rtc_base/task_utils/to_queued_task.h"
  20. #include "rtc_base/thread_annotations.h"
  21. namespace webrtc {
  22. template <typename Closure>
  23. void SendTask(rtc::Location loc, TaskQueueBase* task_queue, Closure&& task) {
  24. RTC_CHECK(!task_queue->IsCurrent())
  25. << "Called SendTask to a queue from the same queue at " << loc.ToString();
  26. rtc::Event event;
  27. task_queue->PostTask(
  28. ToQueuedTask(std::forward<Closure>(task), [&event] { event.Set(); }));
  29. RTC_CHECK(event.Wait(/*give_up_after_ms=*/rtc::Event::kForever,
  30. /*warn_after_ms=*/10'000))
  31. << "Waited too long at " << loc.ToString();
  32. }
  33. class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue {
  34. public:
  35. using rtc::TaskQueue::TaskQueue;
  36. explicit TaskQueueForTest(absl::string_view name = "TestQueue",
  37. Priority priority = Priority::NORMAL);
  38. TaskQueueForTest(const TaskQueueForTest&) = delete;
  39. TaskQueueForTest& operator=(const TaskQueueForTest&) = delete;
  40. ~TaskQueueForTest() = default;
  41. // A convenience, test-only method that blocks the current thread while
  42. // a task executes on the task queue.
  43. // This variant is specifically for posting custom QueuedTask derived
  44. // implementations that tests do not want to pass ownership of over to the
  45. // task queue (i.e. the Run() method always returns |false|.).
  46. template <class Closure>
  47. void SendTask(Closure* task) {
  48. RTC_CHECK(!IsCurrent());
  49. rtc::Event event;
  50. PostTask(ToQueuedTask(
  51. [&task] { RTC_CHECK_EQ(false, static_cast<QueuedTask*>(task)->Run()); },
  52. [&event] { event.Set(); }));
  53. event.Wait(rtc::Event::kForever);
  54. }
  55. // A convenience, test-only method that blocks the current thread while
  56. // a task executes on the task queue.
  57. template <class Closure>
  58. void SendTask(Closure&& task, rtc::Location loc) {
  59. ::webrtc::SendTask(loc, Get(), std::forward<Closure>(task));
  60. }
  61. // Wait for the completion of all tasks posted prior to the
  62. // WaitForPreviouslyPostedTasks() call.
  63. void WaitForPreviouslyPostedTasks() {
  64. // Post an empty task on the queue and wait for it to finish, to ensure
  65. // that all already posted tasks on the queue get executed.
  66. SendTask([]() {}, RTC_FROM_HERE);
  67. }
  68. };
  69. } // namespace webrtc
  70. #endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_