Chromium Code Reviews| Index: base/test/scoped_task_environment_unittest.cc |
| diff --git a/base/test/scoped_task_environment_unittest.cc b/base/test/scoped_task_environment_unittest.cc |
| index 245cda28cde677cd1f3ae30dec9420184a554cd3..6bb1beeae5be09f98660d16e4f08adaa3bd6376c 100644 |
| --- a/base/test/scoped_task_environment_unittest.cc |
| +++ b/base/test/scoped_task_environment_unittest.cc |
| @@ -6,7 +6,10 @@ |
| #include "base/bind.h" |
| #include "base/synchronization/atomic_flag.h" |
| +#include "base/synchronization/waitable_event.h" |
| #include "base/task_scheduler/post_task.h" |
| +#include "base/test/test_timeouts.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -22,11 +25,11 @@ void VerifyRunUntilIdleDidNotReturnAndSetFlag( |
| task_ran->Set(); |
| } |
| -} // namespace |
| - |
| -TEST(ScopedTaskEnvironmentTest, RunUntilIdle) { |
| +void RunUntilIdleTest( |
| + ScopedTaskEnvironment::ExecutionMode execution_control_mode) { |
| AtomicFlag run_until_idle_returned; |
| - ScopedTaskEnvironment scoped_task_environment; |
| + ScopedTaskEnvironment scoped_task_environment( |
| + ScopedTaskEnvironment::MainThreadType::DEFAULT, execution_control_mode); |
| AtomicFlag first_main_thread_task_ran; |
| ThreadTaskRunnerHandle::Get()->PostTask( |
| @@ -58,5 +61,76 @@ TEST(ScopedTaskEnvironmentTest, RunUntilIdle) { |
| EXPECT_TRUE(second_main_thread_task_ran.IsSet()); |
| } |
| +} // namespace |
| + |
| +TEST(ScopedTaskEnvironmentTest, QueuedRunUntilIdle) { |
| + RunUntilIdleTest(ScopedTaskEnvironment::ExecutionMode::QUEUED); |
| +} |
| + |
| +TEST(ScopedTaskEnvironmentTest, AsyncRunUntilIdle) { |
| + RunUntilIdleTest(ScopedTaskEnvironment::ExecutionMode::ASYNC); |
| +} |
| + |
| +// Verify that tasks posted to an ExecutionMode::QUEUED ScopedTaskEnvironment do |
| +// not run outside of RunUntilIdle(). |
| +TEST(ScopedTaskEnvironmentTest, QueuedTasksDoNotRunOutsideOfRunUntilIdle) { |
| + ScopedTaskEnvironment scoped_task_environment( |
| + ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| + ScopedTaskEnvironment::ExecutionMode::QUEUED); |
| + |
| + AtomicFlag run_until_idle_called; |
| + PostTask(FROM_HERE, BindOnce( |
| + [](AtomicFlag* run_until_idle_called) { |
| + EXPECT_TRUE(run_until_idle_called->IsSet()); |
| + }, |
| + Unretained(&run_until_idle_called))); |
| + PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| + run_until_idle_called.Set(); |
| + scoped_task_environment.RunUntilIdle(); |
| + |
| + AtomicFlag other_run_until_idle_called; |
| + PostTask(FROM_HERE, BindOnce( |
| + [](AtomicFlag* other_run_until_idle_called) { |
| + EXPECT_TRUE(other_run_until_idle_called->IsSet()); |
| + }, |
| + Unretained(&other_run_until_idle_called))); |
| + PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| + other_run_until_idle_called.Set(); |
| + scoped_task_environment.RunUntilIdle(); |
| +} |
| + |
| +// Verify that a task posted to an ExecutionMode::Async ScopedTaskEnvironment |
|
gab
2017/05/25 19:28:57
ASYNC (caps)
fdoray
2017/05/25 20:06:44
Done.
|
| +// can run without a call to RunUntilIdle(). |
| +TEST(ScopedTaskEnvironmentTest, AsyncTasksRunAsTheyArePosted) { |
| + ScopedTaskEnvironment scoped_task_environment( |
| + ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| + ScopedTaskEnvironment::ExecutionMode::ASYNC); |
| + |
| + WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
| + WaitableEvent::InitialState::NOT_SIGNALED); |
| + PostTask(FROM_HERE, |
| + BindOnce([](WaitableEvent* task_ran) { task_ran->Signal(); }, |
| + Unretained(&task_ran))); |
| + task_ran.Wait(); |
| +} |
| + |
| +// Verify that a task posted to an ExecutionMode::ASYNC ScopedTaskEnvironment |
| +// after a call to RunUntilIdle() can run without another call to |
| +// RunUntilIdle(). |
| +TEST(ScopedTaskEnvironmentTest, AsyncTasksRunAsTheyArePostedAfterRunUntilIdle) { |
| + ScopedTaskEnvironment scoped_task_environment( |
| + ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| + ScopedTaskEnvironment::ExecutionMode::ASYNC); |
| + |
| + scoped_task_environment.RunUntilIdle(); |
| + |
| + WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
| + WaitableEvent::InitialState::NOT_SIGNALED); |
| + PostTask(FROM_HERE, |
| + BindOnce([](WaitableEvent* task_ran) { task_ran->Signal(); }, |
| + Unretained(&task_ran))); |
| + task_ran.Wait(); |
| +} |
| + |
| } // namespace test |
| } // namespace base |