test_utils.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_THREAD_POOL_TEST_UTILS_H_
  5. #define BASE_TASK_THREAD_POOL_TEST_UTILS_H_
  6. #include <atomic>
  7. #include "base/callback.h"
  8. #include "base/task/common/checked_lock.h"
  9. #include "base/task/post_job.h"
  10. #include "base/task/task_features.h"
  11. #include "base/task/task_traits.h"
  12. #include "base/task/thread_pool/delayed_task_manager.h"
  13. #include "base/task/thread_pool/pooled_task_runner_delegate.h"
  14. #include "base/task/thread_pool/sequence.h"
  15. #include "base/task/thread_pool/task_tracker.h"
  16. #include "base/task/thread_pool/thread_group.h"
  17. #include "base/task/thread_pool/worker_thread_observer.h"
  18. #include "base/task_runner.h"
  19. #include "base/thread_annotations.h"
  20. #include "build/build_config.h"
  21. #include "testing/gmock/include/gmock/gmock.h"
  22. namespace base {
  23. namespace internal {
  24. struct Task;
  25. namespace test {
  26. class MockWorkerThreadObserver : public WorkerThreadObserver {
  27. public:
  28. MockWorkerThreadObserver();
  29. ~MockWorkerThreadObserver();
  30. void AllowCallsOnMainExit(int num_calls);
  31. void WaitCallsOnMainExit();
  32. // WorkerThreadObserver:
  33. MOCK_METHOD0(OnWorkerThreadMainEntry, void());
  34. // This doesn't use MOCK_METHOD0 because some tests need to wait for all calls
  35. // to happen, which isn't possible with gmock.
  36. void OnWorkerThreadMainExit() override;
  37. private:
  38. CheckedLock lock_;
  39. std::unique_ptr<ConditionVariable> on_main_exit_cv_ GUARDED_BY(lock_);
  40. int allowed_calls_on_main_exit_ GUARDED_BY(lock_) = 0;
  41. DISALLOW_COPY_AND_ASSIGN(MockWorkerThreadObserver);
  42. };
  43. class MockPooledTaskRunnerDelegate : public PooledTaskRunnerDelegate {
  44. public:
  45. MockPooledTaskRunnerDelegate(TrackedRef<TaskTracker> task_tracker,
  46. DelayedTaskManager* delayed_task_manager);
  47. ~MockPooledTaskRunnerDelegate() override;
  48. // PooledTaskRunnerDelegate:
  49. bool PostTaskWithSequence(Task task,
  50. scoped_refptr<Sequence> sequence) override;
  51. bool EnqueueJobTaskSource(scoped_refptr<JobTaskSource> task_source) override;
  52. void RemoveJobTaskSource(scoped_refptr<JobTaskSource> task_source) override;
  53. bool ShouldYield(const TaskSource* task_source) const override;
  54. void UpdatePriority(scoped_refptr<TaskSource> task_source,
  55. TaskPriority priority) override;
  56. void SetThreadGroup(ThreadGroup* thread_group);
  57. void PostTaskWithSequenceNow(Task task, scoped_refptr<Sequence> sequence);
  58. private:
  59. const TrackedRef<TaskTracker> task_tracker_;
  60. DelayedTaskManager* const delayed_task_manager_;
  61. ThreadGroup* thread_group_ = nullptr;
  62. };
  63. // A simple MockJobTask that will give |worker_task| a fixed number of times,
  64. // possibly in parallel.
  65. class MockJobTask : public base::RefCountedThreadSafe<MockJobTask> {
  66. public:
  67. // Gives |worker_task| to requesting workers |num_tasks_to_run| times.
  68. MockJobTask(base::RepeatingCallback<void(JobDelegate*)> worker_task,
  69. size_t num_tasks_to_run);
  70. // Gives |worker_task| to a single requesting worker.
  71. MockJobTask(base::OnceClosure worker_task);
  72. // Updates the remaining number of time |worker_task| runs to
  73. // |num_tasks_to_run|.
  74. void SetNumTasksToRun(size_t num_tasks_to_run) {
  75. remaining_num_tasks_to_run_ = num_tasks_to_run;
  76. }
  77. size_t GetMaxConcurrency(size_t worker_count) const;
  78. void Run(JobDelegate* delegate);
  79. scoped_refptr<JobTaskSource> GetJobTaskSource(
  80. const Location& from_here,
  81. const TaskTraits& traits,
  82. PooledTaskRunnerDelegate* delegate);
  83. private:
  84. friend class base::RefCountedThreadSafe<MockJobTask>;
  85. ~MockJobTask();
  86. base::RepeatingCallback<void(JobDelegate*)> worker_task_;
  87. std::atomic_size_t remaining_num_tasks_to_run_;
  88. DISALLOW_COPY_AND_ASSIGN(MockJobTask);
  89. };
  90. // An enumeration of possible thread pool types. Used to parametrize relevant
  91. // thread_pool tests.
  92. enum class PoolType {
  93. GENERIC,
  94. #if HAS_NATIVE_THREAD_POOL()
  95. NATIVE,
  96. #endif
  97. };
  98. // Creates a Sequence with given |traits| and pushes |task| to it. If a
  99. // TaskRunner is associated with |task|, it should be be passed as |task_runner|
  100. // along with its |execution_mode|. Returns the created Sequence.
  101. scoped_refptr<Sequence> CreateSequenceWithTask(
  102. Task task,
  103. const TaskTraits& traits,
  104. scoped_refptr<TaskRunner> task_runner = nullptr,
  105. TaskSourceExecutionMode execution_mode =
  106. TaskSourceExecutionMode::kParallel);
  107. // Creates a TaskRunner that posts tasks to the thread group owned by
  108. // |pooled_task_runner_delegate| with the |execution_mode|.
  109. // Caveat: this does not support TaskSourceExecutionMode::kSingleThread.
  110. scoped_refptr<TaskRunner> CreatePooledTaskRunnerWithExecutionMode(
  111. TaskSourceExecutionMode execution_mode,
  112. MockPooledTaskRunnerDelegate* mock_pooled_task_runner_delegate,
  113. const TaskTraits& traits = {});
  114. scoped_refptr<TaskRunner> CreatePooledTaskRunner(
  115. const TaskTraits& traits,
  116. MockPooledTaskRunnerDelegate* mock_pooled_task_runner_delegate);
  117. scoped_refptr<SequencedTaskRunner> CreatePooledSequencedTaskRunner(
  118. const TaskTraits& traits,
  119. MockPooledTaskRunnerDelegate* mock_pooled_task_runner_delegate);
  120. RegisteredTaskSource QueueAndRunTaskSource(
  121. TaskTracker* task_tracker,
  122. scoped_refptr<TaskSource> task_source);
  123. // Calls StartShutdown() and CompleteShutdown() on |task_tracker|.
  124. void ShutdownTaskTracker(TaskTracker* task_tracker);
  125. } // namespace test
  126. } // namespace internal
  127. } // namespace base
  128. #endif // BASE_TASK_THREAD_POOL_TEST_UTILS_H_