Chromium Code Reviews| Index: components/sync/driver/glue/ui_model_worker_unittest.cc |
| diff --git a/components/sync/driver/glue/ui_model_worker_unittest.cc b/components/sync/driver/glue/ui_model_worker_unittest.cc |
| index d091f317a5c60f2896c0a05ec84abbea9095a730..02a8a5103a1f11d66e68a0626115cd89d6053a57 100644 |
| --- a/components/sync/driver/glue/ui_model_worker_unittest.cc |
| +++ b/components/sync/driver/glue/ui_model_worker_unittest.cc |
| @@ -4,11 +4,16 @@ |
| #include "components/sync/driver/glue/ui_model_worker.h" |
| +#include <memory> |
| + |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/location.h" |
| +#include "base/memory/ptr_util.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/run_loop.h" |
| +#include "base/test/test_timeouts.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/thread.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -32,6 +37,11 @@ WorkCallback ClosureToWorkCallback(base::Closure work) { |
| return base::Bind(&DoWork, base::ThreadTaskRunnerHandle::Get(), work); |
| } |
| +// Increments |counter|. |
| +void IncrementCounter(int* counter) { |
| + ++*counter; |
| +} |
| + |
| class SyncUIModelWorkerTest : public testing::Test { |
| public: |
| SyncUIModelWorkerTest() : sync_thread_("SyncThreadForTest") { |
| @@ -39,25 +49,66 @@ class SyncUIModelWorkerTest : public testing::Test { |
| worker_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get()); |
| } |
| - void PostWorkToSyncThread(WorkCallback work) { |
| + void PostWorkToSyncThread(base::Closure work) { |
| sync_thread_.task_runner()->PostTask( |
| FROM_HERE, |
| base::Bind(base::IgnoreResult(&UIModelWorker::DoWorkAndWaitUntilDone), |
| - worker_, work)); |
| + worker_, ClosureToWorkCallback(work))); |
| } |
| - private: |
| - base::MessageLoop ui_loop_; |
| + protected: |
| + std::unique_ptr<base::MessageLoop> ui_loop_ = |
| + base::MakeUnique<base::MessageLoop>(); |
| base::Thread sync_thread_; |
| scoped_refptr<UIModelWorker> worker_; |
| }; |
| +} // namespace |
| + |
| TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) { |
| base::RunLoop run_loop; |
| - PostWorkToSyncThread(ClosureToWorkCallback(run_loop.QuitClosure())); |
| + PostWorkToSyncThread(run_loop.QuitClosure()); |
| // This won't quit until the QuitClosure is run. |
| run_loop.Run(); |
| } |
| -} // namespace |
| +TEST_F(SyncUIModelWorkerTest, MultipleDoWork) { |
| + constexpr int kNumWorkCallbacks = 10; |
| + int counter = 0; |
| + for (int i = 0; i < kNumWorkCallbacks; ++i) { |
| + PostWorkToSyncThread( |
| + base::Bind(&IncrementCounter, base::Unretained(&counter))); |
| + } |
| + |
| + base::RunLoop run_loop; |
| + PostWorkToSyncThread(run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + |
| + EXPECT_EQ(kNumWorkCallbacks, counter); |
| +} |
| + |
| +TEST_F(SyncUIModelWorkerTest, JoinSyncThreadAfterUIMessageLoopDestruction) { |
| + PostWorkToSyncThread(base::Bind(&base::DoNothing)); |
| + |
| + // Allow the sync thread to post the WorkCallback to the UI MessageLoop. |
| + base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
|
maxbogue
2016/11/18 18:26:51
Ooo, interesting. Didn't know we had anything like
fdoray
2016/11/21 16:57:56
Yes, it's racy. I explained it in the comment.
Jo
|
| + |
| + // The sync thread shouldn't wait for the WorkCallback to run on the UI thread |
| + // after the UI MessageLoop is gone. |
| + ui_loop_.reset(); |
| + sync_thread_.Stop(); |
| +} |
| + |
| +TEST_F(SyncUIModelWorkerTest, JoinSyncThreadAfterRequestStop) { |
| + PostWorkToSyncThread(base::Bind(&base::DoNothing)); |
| + |
| + // Allow the sync thread to post the WorkCallback to the UI MessageLoop. |
| + base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| + |
| + // The sync thread shouldn't wait for the WorkCallback to run on the UI thread |
| + // after RequestStop() is called. |
| + worker_->RequestStop(); |
| + sync_thread_.Stop(); |
| +} |
| + |
| } // namespace syncer |