queued_task_poster_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "remoting/base/queued_task_poster.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/run_loop.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/threading/thread.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace remoting {
  16. class QueuedTaskPosterTest : public testing::Test {
  17. public:
  18. QueuedTaskPosterTest();
  19. void SetUp() override;
  20. void TearDown() override;
  21. protected:
  22. base::OnceClosure SetSequenceStartedClosure(bool started);
  23. base::OnceClosure AssertExecutionOrderClosure(int order);
  24. base::OnceClosure AssertSequenceNotStartedClosure();
  25. void RunUntilPosterDone();
  26. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  27. scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
  28. std::unique_ptr<QueuedTaskPoster> poster_;
  29. int current_execution_order_ = 0;
  30. private:
  31. void SetSequenceStarted(bool started);
  32. void AssertExecutionOrder(int order);
  33. void AssertSequenceNotStarted();
  34. base::Thread target_thread_;
  35. base::test::SingleThreadTaskEnvironment task_environment_;
  36. bool sequence_started_ = false;
  37. };
  38. QueuedTaskPosterTest::QueuedTaskPosterTest()
  39. : target_thread_("Target Thread") {}
  40. void QueuedTaskPosterTest::SetUp() {
  41. target_thread_.StartAndWaitForTesting();
  42. main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
  43. target_task_runner_ = target_thread_.task_runner();
  44. poster_ = std::make_unique<QueuedTaskPoster>(target_task_runner_);
  45. }
  46. void QueuedTaskPosterTest::TearDown() {
  47. target_thread_.Stop();
  48. }
  49. base::OnceClosure QueuedTaskPosterTest::SetSequenceStartedClosure(
  50. bool started) {
  51. return base::BindOnce(&QueuedTaskPosterTest::SetSequenceStarted,
  52. base::Unretained(this), started);
  53. }
  54. base::OnceClosure QueuedTaskPosterTest::AssertExecutionOrderClosure(int order) {
  55. return base::BindOnce(&QueuedTaskPosterTest::AssertExecutionOrder,
  56. base::Unretained(this), order);
  57. }
  58. base::OnceClosure QueuedTaskPosterTest::AssertSequenceNotStartedClosure() {
  59. return base::BindOnce(&QueuedTaskPosterTest::AssertSequenceNotStarted,
  60. base::Unretained(this));
  61. }
  62. void QueuedTaskPosterTest::RunUntilPosterDone() {
  63. base::RunLoop run_loop;
  64. poster_->AddTask(base::BindOnce(
  65. base::IgnoreResult(&base::SingleThreadTaskRunner::PostTask),
  66. main_task_runner_, FROM_HERE, run_loop.QuitClosure()));
  67. run_loop.Run();
  68. }
  69. void QueuedTaskPosterTest::SetSequenceStarted(bool started) {
  70. sequence_started_ = started;
  71. }
  72. void QueuedTaskPosterTest::AssertExecutionOrder(int order) {
  73. ASSERT_EQ(current_execution_order_ + 1, order);
  74. current_execution_order_++;
  75. }
  76. void QueuedTaskPosterTest::AssertSequenceNotStarted() {
  77. ASSERT_FALSE(sequence_started_);
  78. }
  79. TEST_F(QueuedTaskPosterTest, TestTaskOrder) {
  80. poster_->AddTask(AssertExecutionOrderClosure(1));
  81. poster_->AddTask(AssertExecutionOrderClosure(2));
  82. poster_->AddTask(AssertExecutionOrderClosure(3));
  83. poster_->AddTask(AssertExecutionOrderClosure(4));
  84. poster_->AddTask(AssertExecutionOrderClosure(5));
  85. RunUntilPosterDone();
  86. EXPECT_EQ(5, current_execution_order_);
  87. }
  88. TEST_F(QueuedTaskPosterTest, TestTaskSequenceNotInterfered) {
  89. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  90. poster_->AddTask(SetSequenceStartedClosure(true));
  91. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  92. poster_->AddTask(AssertExecutionOrderClosure(1));
  93. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  94. poster_->AddTask(AssertExecutionOrderClosure(2));
  95. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  96. poster_->AddTask(AssertExecutionOrderClosure(3));
  97. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  98. poster_->AddTask(AssertExecutionOrderClosure(4));
  99. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  100. poster_->AddTask(AssertExecutionOrderClosure(5));
  101. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  102. poster_->AddTask(SetSequenceStartedClosure(false));
  103. target_task_runner_->PostTask(FROM_HERE, AssertSequenceNotStartedClosure());
  104. RunUntilPosterDone();
  105. EXPECT_EQ(5, current_execution_order_);
  106. }
  107. TEST_F(QueuedTaskPosterTest, TestUsingPosterInMultipleTasks) {
  108. poster_->AddTask(AssertExecutionOrderClosure(1));
  109. poster_->AddTask(AssertExecutionOrderClosure(2));
  110. poster_->AddTask(AssertExecutionOrderClosure(3));
  111. RunUntilPosterDone();
  112. EXPECT_EQ(3, current_execution_order_);
  113. poster_->AddTask(AssertExecutionOrderClosure(4));
  114. poster_->AddTask(AssertExecutionOrderClosure(5));
  115. poster_->AddTask(AssertExecutionOrderClosure(6));
  116. RunUntilPosterDone();
  117. EXPECT_EQ(6, current_execution_order_);
  118. }
  119. } // namespace remoting