| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/sync_driver/shared_change_processor.h" | 5 #include "components/sync_driver/shared_change_processor.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/location.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 13 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "components/sync_driver/data_type_error_handler_mock.h" | 16 #include "components/sync_driver/data_type_error_handler_mock.h" |
| 16 #include "components/sync_driver/generic_change_processor.h" | 17 #include "components/sync_driver/generic_change_processor.h" |
| 17 #include "components/sync_driver/generic_change_processor_factory.h" | 18 #include "components/sync_driver/generic_change_processor_factory.h" |
| 18 #include "components/sync_driver/sync_api_component_factory.h" | 19 #include "components/sync_driver/sync_api_component_factory.h" |
| 19 #include "sync/api/attachments/attachment_id.h" | 20 #include "sync/api/attachments/attachment_id.h" |
| 20 #include "sync/api/attachments/attachment_store.h" | 21 #include "sync/api/attachments/attachment_store.h" |
| 21 #include "sync/api/fake_syncable_service.h" | 22 #include "sync/api/fake_syncable_service.h" |
| 22 #include "sync/internal_api/public/attachments/attachment_service_impl.h" | 23 #include "sync/internal_api/public/attachments/attachment_service_impl.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 syncer::ModelType model_type, | 58 syncer::ModelType model_type, |
| 58 syncer::AttachmentService::Delegate* delegate) override { | 59 syncer::AttachmentService::Delegate* delegate) override { |
| 59 return syncer::AttachmentServiceImpl::CreateForTest(); | 60 return syncer::AttachmentServiceImpl::CreateForTest(); |
| 60 } | 61 } |
| 61 | 62 |
| 62 protected: | 63 protected: |
| 63 void SetUp() override { | 64 void SetUp() override { |
| 64 test_user_share_.SetUp(); | 65 test_user_share_.SetUp(); |
| 65 shared_change_processor_ = new SharedChangeProcessor(); | 66 shared_change_processor_ = new SharedChangeProcessor(); |
| 66 ASSERT_TRUE(backend_thread_.Start()); | 67 ASSERT_TRUE(backend_thread_.Start()); |
| 67 ASSERT_TRUE(backend_thread_.message_loop_proxy()->PostTask( | 68 ASSERT_TRUE(backend_thread_.task_runner()->PostTask( |
| 68 FROM_HERE, | 69 FROM_HERE, |
| 69 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService, | 70 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService, |
| 70 base::Unretained(this)))); | 71 base::Unretained(this)))); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void TearDown() override { | 74 void TearDown() override { |
| 74 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask( | 75 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( |
| 75 FROM_HERE, | 76 FROM_HERE, |
| 76 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService, | 77 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService, |
| 77 base::Unretained(this)))); | 78 base::Unretained(this)))); |
| 78 // This must happen before the DB thread is stopped since | 79 // This must happen before the DB thread is stopped since |
| 79 // |shared_change_processor_| may post tasks to delete its members | 80 // |shared_change_processor_| may post tasks to delete its members |
| 80 // on the correct thread. | 81 // on the correct thread. |
| 81 // | 82 // |
| 82 // TODO(akalin): Write deterministic tests for the destruction of | 83 // TODO(akalin): Write deterministic tests for the destruction of |
| 83 // |shared_change_processor_| on the UI and DB threads. | 84 // |shared_change_processor_| on the UI and DB threads. |
| 84 shared_change_processor_ = NULL; | 85 shared_change_processor_ = NULL; |
| 85 backend_thread_.Stop(); | 86 backend_thread_.Stop(); |
| 86 | 87 |
| 87 // Note: Stop() joins the threads, and that barrier prevents this read | 88 // Note: Stop() joins the threads, and that barrier prevents this read |
| 88 // from being moved (e.g by compiler optimization) in such a way that it | 89 // from being moved (e.g by compiler optimization) in such a way that it |
| 89 // would race with the write in ConnectOnDBThread (because by this time, | 90 // would race with the write in ConnectOnDBThread (because by this time, |
| 90 // everything that could have run on |backend_thread_| has done so). | 91 // everything that could have run on |backend_thread_| has done so). |
| 91 ASSERT_TRUE(did_connect_); | 92 ASSERT_TRUE(did_connect_); |
| 92 test_user_share_.TearDown(); | 93 test_user_share_.TearDown(); |
| 93 } | 94 } |
| 94 | 95 |
| 95 // Connect |shared_change_processor_| on the DB thread. | 96 // Connect |shared_change_processor_| on the DB thread. |
| 96 void Connect() { | 97 void Connect() { |
| 97 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask( | 98 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( |
| 98 FROM_HERE, | 99 FROM_HERE, |
| 99 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread, | 100 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread, |
| 100 base::Unretained(this), | 101 base::Unretained(this), shared_change_processor_))); |
| 101 shared_change_processor_))); | |
| 102 } | 102 } |
| 103 | 103 |
| 104 void SetAttachmentStore() { | 104 void SetAttachmentStore() { |
| 105 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask( | 105 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( |
| 106 FROM_HERE, | 106 FROM_HERE, |
| 107 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread, | 107 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread, |
| 108 base::Unretained(this)))); | 108 base::Unretained(this)))); |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool HasAttachmentService() { | 111 bool HasAttachmentService() { |
| 112 base::WaitableEvent event(false, false); | 112 base::WaitableEvent event(false, false); |
| 113 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask( | 113 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( |
| 114 FROM_HERE, | 114 FROM_HERE, |
| 115 base::Bind( | 115 base::Bind( |
| 116 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread, | 116 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread, |
| 117 base::Unretained(this), base::Unretained(&event)))); | 117 base::Unretained(this), base::Unretained(&event)))); |
| 118 event.Wait(); | 118 event.Wait(); |
| 119 return has_attachment_service_; | 119 return has_attachment_service_; |
| 120 } | 120 } |
| 121 | 121 |
| 122 private: | 122 private: |
| 123 // Used by SetUp(). | 123 // Used by SetUp(). |
| 124 void SetUpDBSyncableService() { | 124 void SetUpDBSyncableService() { |
| 125 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 125 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); |
| 126 DCHECK(!db_syncable_service_.get()); | 126 DCHECK(!db_syncable_service_.get()); |
| 127 db_syncable_service_.reset(new syncer::FakeSyncableService()); | 127 db_syncable_service_.reset(new syncer::FakeSyncableService()); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Used by TearDown(). | 130 // Used by TearDown(). |
| 131 void TearDownDBSyncableService() { | 131 void TearDownDBSyncableService() { |
| 132 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 132 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); |
| 133 DCHECK(db_syncable_service_.get()); | 133 DCHECK(db_syncable_service_.get()); |
| 134 db_syncable_service_.reset(); | 134 db_syncable_service_.reset(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void SetAttachmentStoreOnDBThread() { | 137 void SetAttachmentStoreOnDBThread() { |
| 138 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 138 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); |
| 139 DCHECK(db_syncable_service_.get()); | 139 DCHECK(db_syncable_service_.get()); |
| 140 db_syncable_service_->set_attachment_store( | 140 db_syncable_service_->set_attachment_store( |
| 141 syncer::AttachmentStore::CreateInMemoryStore()); | 141 syncer::AttachmentStore::CreateInMemoryStore()); |
| 142 } | 142 } |
| 143 | 143 |
| 144 // Used by Connect(). The SharedChangeProcessor is passed in | 144 // Used by Connect(). The SharedChangeProcessor is passed in |
| 145 // because we modify |shared_change_processor_| on the main thread | 145 // because we modify |shared_change_processor_| on the main thread |
| 146 // (in TearDown()). | 146 // (in TearDown()). |
| 147 void ConnectOnDBThread( | 147 void ConnectOnDBThread( |
| 148 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) { | 148 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) { |
| 149 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 149 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); |
| 150 EXPECT_TRUE(shared_change_processor->Connect( | 150 EXPECT_TRUE(shared_change_processor->Connect( |
| 151 this, &processor_factory_, test_user_share_.user_share(), | 151 this, &processor_factory_, test_user_share_.user_share(), |
| 152 &error_handler_, syncer::AUTOFILL, | 152 &error_handler_, syncer::AUTOFILL, |
| 153 base::WeakPtr<syncer::SyncMergeResult>())); | 153 base::WeakPtr<syncer::SyncMergeResult>())); |
| 154 did_connect_ = true; | 154 did_connect_ = true; |
| 155 } | 155 } |
| 156 | 156 |
| 157 void CheckAttachmentServiceOnDBThread(base::WaitableEvent* event) { | 157 void CheckAttachmentServiceOnDBThread(base::WaitableEvent* event) { |
| 158 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 158 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); |
| 159 DCHECK(db_syncable_service_.get()); | 159 DCHECK(db_syncable_service_.get()); |
| 160 has_attachment_service_ = !!db_syncable_service_->attachment_service(); | 160 has_attachment_service_ = !!db_syncable_service_->attachment_service(); |
| 161 event->Signal(); | 161 event->Signal(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 base::MessageLoop frontend_loop_; | 164 base::MessageLoop frontend_loop_; |
| 165 base::Thread backend_thread_; | 165 base::Thread backend_thread_; |
| 166 syncer::TestUserShare test_user_share_; | 166 syncer::TestUserShare test_user_share_; |
| 167 | 167 |
| 168 scoped_refptr<SharedChangeProcessor> shared_change_processor_; | 168 scoped_refptr<SharedChangeProcessor> shared_change_processor_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 187 // creates AttachmentService and passes it back to the syncable service. | 187 // creates AttachmentService and passes it back to the syncable service. |
| 188 TEST_F(SyncSharedChangeProcessorTest, ConnectWithAttachmentStore) { | 188 TEST_F(SyncSharedChangeProcessorTest, ConnectWithAttachmentStore) { |
| 189 SetAttachmentStore(); | 189 SetAttachmentStore(); |
| 190 Connect(); | 190 Connect(); |
| 191 EXPECT_TRUE(HasAttachmentService()); | 191 EXPECT_TRUE(HasAttachmentService()); |
| 192 } | 192 } |
| 193 | 193 |
| 194 } // namespace | 194 } // namespace |
| 195 | 195 |
| 196 } // namespace sync_driver | 196 } // namespace sync_driver |
| OLD | NEW |