| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/scoped_task_environment.h" | 5 #include "base/test/scoped_task_environment.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/synchronization/atomic_flag.h" | 8 #include "base/synchronization/atomic_flag.h" |
| 9 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/task_scheduler/post_task.h" | 10 #include "base/task_scheduler/post_task.h" |
| 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/platform_thread.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 15 |
| 13 namespace base { | 16 namespace base { |
| 14 namespace test { | 17 namespace test { |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 void VerifyRunUntilIdleDidNotReturnAndSetFlag( | 21 void VerifyRunUntilIdleDidNotReturnAndSetFlag( |
| 19 AtomicFlag* run_until_idle_returned, | 22 AtomicFlag* run_until_idle_returned, |
| 20 AtomicFlag* task_ran) { | 23 AtomicFlag* task_ran) { |
| 21 EXPECT_FALSE(run_until_idle_returned->IsSet()); | 24 EXPECT_FALSE(run_until_idle_returned->IsSet()); |
| 22 task_ran->Set(); | 25 task_ran->Set(); |
| 23 } | 26 } |
| 24 | 27 |
| 25 } // namespace | 28 void RunUntilIdleTest( |
| 26 | 29 ScopedTaskEnvironment::ExecutionControlMode execution_control_mode) { |
| 27 TEST(ScopedTaskEnvironmentTest, RunUntilIdle) { | |
| 28 AtomicFlag run_until_idle_returned; | 30 AtomicFlag run_until_idle_returned; |
| 29 ScopedTaskEnvironment scoped_task_environment; | 31 ScopedTaskEnvironment scoped_task_environment( |
| 32 ScopedTaskEnvironment::MainThreadType::DEFAULT, execution_control_mode); |
| 30 | 33 |
| 31 AtomicFlag first_main_thread_task_ran; | 34 AtomicFlag first_main_thread_task_ran; |
| 32 ThreadTaskRunnerHandle::Get()->PostTask( | 35 ThreadTaskRunnerHandle::Get()->PostTask( |
| 33 FROM_HERE, BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag, | 36 FROM_HERE, BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag, |
| 34 Unretained(&run_until_idle_returned), | 37 Unretained(&run_until_idle_returned), |
| 35 Unretained(&first_main_thread_task_ran))); | 38 Unretained(&first_main_thread_task_ran))); |
| 36 | 39 |
| 37 AtomicFlag first_task_scheduler_task_ran; | 40 AtomicFlag first_task_scheduler_task_ran; |
| 38 PostTask(FROM_HERE, BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag, | 41 PostTask(FROM_HERE, BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag, |
| 39 Unretained(&run_until_idle_returned), | 42 Unretained(&run_until_idle_returned), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 51 | 54 |
| 52 scoped_task_environment.RunUntilIdle(); | 55 scoped_task_environment.RunUntilIdle(); |
| 53 run_until_idle_returned.Set(); | 56 run_until_idle_returned.Set(); |
| 54 | 57 |
| 55 EXPECT_TRUE(first_main_thread_task_ran.IsSet()); | 58 EXPECT_TRUE(first_main_thread_task_ran.IsSet()); |
| 56 EXPECT_TRUE(first_task_scheduler_task_ran.IsSet()); | 59 EXPECT_TRUE(first_task_scheduler_task_ran.IsSet()); |
| 57 EXPECT_TRUE(second_task_scheduler_task_ran.IsSet()); | 60 EXPECT_TRUE(second_task_scheduler_task_ran.IsSet()); |
| 58 EXPECT_TRUE(second_main_thread_task_ran.IsSet()); | 61 EXPECT_TRUE(second_main_thread_task_ran.IsSet()); |
| 59 } | 62 } |
| 60 | 63 |
| 64 } // namespace |
| 65 |
| 66 TEST(ScopedTaskEnvironmentTest, QueuedRunUntilIdle) { |
| 67 RunUntilIdleTest(ScopedTaskEnvironment::ExecutionControlMode::QUEUED); |
| 68 } |
| 69 |
| 70 TEST(ScopedTaskEnvironmentTest, AsyncRunUntilIdle) { |
| 71 RunUntilIdleTest(ScopedTaskEnvironment::ExecutionControlMode::ASYNC); |
| 72 } |
| 73 |
| 74 // Verify that tasks posted to an ExecutionControlMode::QUEUED |
| 75 // ScopedTaskEnvironment do not run outside of RunUntilIdle(). |
| 76 TEST(ScopedTaskEnvironmentTest, QueuedTasksDoNotRunOutsideOfRunUntilIdle) { |
| 77 ScopedTaskEnvironment scoped_task_environment( |
| 78 ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| 79 ScopedTaskEnvironment::ExecutionControlMode::QUEUED); |
| 80 |
| 81 AtomicFlag run_until_idle_called; |
| 82 PostTask(FROM_HERE, BindOnce( |
| 83 [](AtomicFlag* run_until_idle_called) { |
| 84 EXPECT_TRUE(run_until_idle_called->IsSet()); |
| 85 }, |
| 86 Unretained(&run_until_idle_called))); |
| 87 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 88 run_until_idle_called.Set(); |
| 89 scoped_task_environment.RunUntilIdle(); |
| 90 |
| 91 AtomicFlag other_run_until_idle_called; |
| 92 PostTask(FROM_HERE, BindOnce( |
| 93 [](AtomicFlag* other_run_until_idle_called) { |
| 94 EXPECT_TRUE(other_run_until_idle_called->IsSet()); |
| 95 }, |
| 96 Unretained(&other_run_until_idle_called))); |
| 97 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 98 other_run_until_idle_called.Set(); |
| 99 scoped_task_environment.RunUntilIdle(); |
| 100 } |
| 101 |
| 102 // Verify that a task posted to an ExecutionControlMode::QUEUED |
| 103 // ScopedTaskEnvironment can run without a call to RunUntilIdle(). |
| 104 TEST(ScopedTaskEnvironmentTest, AsyncTasksRunAsTheyArePosted) { |
| 105 ScopedTaskEnvironment scoped_task_environment( |
| 106 ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| 107 ScopedTaskEnvironment::ExecutionControlMode::ASYNC); |
| 108 |
| 109 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
| 110 WaitableEvent::InitialState::NOT_SIGNALED); |
| 111 PostTask(FROM_HERE, |
| 112 BindOnce([](WaitableEvent* task_ran) { task_ran->Signal(); }, |
| 113 Unretained(&task_ran))); |
| 114 task_ran.Wait(); |
| 115 } |
| 116 |
| 117 // Verify that a task posted to an ExecutionControlMode::QUEUED |
| 118 // ScopedTaskEnvironment after a call to RunUntilIdle() can run without another |
| 119 // call to RunUntilIdle(). |
| 120 TEST(ScopedTaskEnvironmentTest, AsyncTasksRunAsTheyArePostedAfterRunUntilIdle) { |
| 121 ScopedTaskEnvironment scoped_task_environment( |
| 122 ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| 123 ScopedTaskEnvironment::ExecutionControlMode::ASYNC); |
| 124 |
| 125 scoped_task_environment.RunUntilIdle(); |
| 126 |
| 127 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
| 128 WaitableEvent::InitialState::NOT_SIGNALED); |
| 129 PostTask(FROM_HERE, |
| 130 BindOnce([](WaitableEvent* task_ran) { task_ran->Signal(); }, |
| 131 Unretained(&task_ran))); |
| 132 task_ran.Wait(); |
| 133 } |
| 134 |
| 61 } // namespace test | 135 } // namespace test |
| 62 } // namespace base | 136 } // namespace base |
| OLD | NEW |