Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: components/sync/driver/shared_change_processor_unittest.cc

Issue 2533083002: [Sync] SyncEngine refactor part 1: interfaces. (Closed)
Patch Set: Rebase. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 29 matching lines...) Expand all
40 ~TestSyncApiComponentFactory() override {} 40 ~TestSyncApiComponentFactory() override {}
41 41
42 // SyncApiComponentFactory implementation. 42 // SyncApiComponentFactory implementation.
43 void RegisterDataTypes( 43 void RegisterDataTypes(
44 SyncService* sync_service, 44 SyncService* sync_service,
45 const RegisterDataTypesMethod& register_platform_types_method) override {} 45 const RegisterDataTypesMethod& register_platform_types_method) override {}
46 DataTypeManager* CreateDataTypeManager( 46 DataTypeManager* CreateDataTypeManager(
47 const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener, 47 const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener,
48 const DataTypeController::TypeMap* controllers, 48 const DataTypeController::TypeMap* controllers,
49 const DataTypeEncryptionHandler* encryption_handler, 49 const DataTypeEncryptionHandler* encryption_handler,
50 SyncBackendHost* backend, 50 SyncEngine* engine,
51 DataTypeManagerObserver* observer) override { 51 DataTypeManagerObserver* observer) override {
52 return nullptr; 52 return nullptr;
53 } 53 }
54 SyncBackendHost* CreateSyncBackendHost( 54 SyncEngine* CreateSyncEngine(const std::string& name,
55 const std::string& name, 55 invalidation::InvalidationService* invalidator,
56 invalidation::InvalidationService* invalidator, 56 const base::WeakPtr<SyncPrefs>& sync_prefs,
57 const base::WeakPtr<SyncPrefs>& sync_prefs, 57 const base::FilePath& sync_folder) override {
58 const base::FilePath& sync_folder) override {
59 return nullptr; 58 return nullptr;
60 } 59 }
61 std::unique_ptr<LocalDeviceInfoProvider> CreateLocalDeviceInfoProvider() 60 std::unique_ptr<LocalDeviceInfoProvider> CreateLocalDeviceInfoProvider()
62 override { 61 override {
63 return nullptr; 62 return nullptr;
64 } 63 }
65 SyncApiComponentFactory::SyncComponents CreateBookmarkSyncComponents( 64 SyncApiComponentFactory::SyncComponents CreateBookmarkSyncComponents(
66 SyncService* sync_service, 65 SyncService* sync_service,
67 std::unique_ptr<DataTypeErrorHandler> error_handler) override { 66 std::unique_ptr<DataTypeErrorHandler> error_handler) override {
68 return SyncApiComponentFactory::SyncComponents(nullptr, nullptr); 67 return SyncApiComponentFactory::SyncComponents(nullptr, nullptr);
69 } 68 }
70 std::unique_ptr<AttachmentService> CreateAttachmentService( 69 std::unique_ptr<AttachmentService> CreateAttachmentService(
71 std::unique_ptr<AttachmentStoreForSync> attachment_store, 70 std::unique_ptr<AttachmentStoreForSync> attachment_store,
72 const UserShare& user_share, 71 const UserShare& user_share,
73 const std::string& store_birthday, 72 const std::string& store_birthday,
74 ModelType model_type, 73 ModelType model_type,
75 AttachmentService::Delegate* delegate) override { 74 AttachmentService::Delegate* delegate) override {
76 return AttachmentService::CreateForTest(); 75 return AttachmentService::CreateForTest();
77 } 76 }
78 }; 77 };
79 78
80 class SyncSharedChangeProcessorTest : public testing::Test, 79 class SyncSharedChangeProcessorTest : public testing::Test,
81 public FakeSyncClient { 80 public FakeSyncClient {
82 public: 81 public:
83 SyncSharedChangeProcessorTest() 82 SyncSharedChangeProcessorTest()
84 : FakeSyncClient(&factory_), 83 : FakeSyncClient(&factory_),
85 backend_thread_("dbthread"), 84 model_thread_("dbthread"),
86 did_connect_(false), 85 did_connect_(false),
87 has_attachment_service_(false) {} 86 has_attachment_service_(false) {}
88 87
89 ~SyncSharedChangeProcessorTest() override { 88 ~SyncSharedChangeProcessorTest() override {
90 EXPECT_FALSE(db_syncable_service_.get()); 89 EXPECT_FALSE(db_syncable_service_.get());
91 } 90 }
92 91
93 // FakeSyncClient override. 92 // FakeSyncClient override.
94 base::WeakPtr<SyncableService> GetSyncableServiceForType( 93 base::WeakPtr<SyncableService> GetSyncableServiceForType(
95 ModelType type) override { 94 ModelType type) override {
96 return db_syncable_service_->AsWeakPtr(); 95 return db_syncable_service_->AsWeakPtr();
97 } 96 }
98 97
99 protected: 98 protected:
100 void SetUp() override { 99 void SetUp() override {
101 test_user_share_.SetUp(); 100 test_user_share_.SetUp();
102 shared_change_processor_ = new SharedChangeProcessor(AUTOFILL); 101 shared_change_processor_ = new SharedChangeProcessor(AUTOFILL);
103 ASSERT_TRUE(backend_thread_.Start()); 102 ASSERT_TRUE(model_thread_.Start());
104 ASSERT_TRUE(backend_thread_.task_runner()->PostTask( 103 ASSERT_TRUE(model_thread_.task_runner()->PostTask(
105 FROM_HERE, 104 FROM_HERE,
106 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService, 105 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
107 base::Unretained(this)))); 106 base::Unretained(this))));
108 } 107 }
109 108
110 void TearDown() override { 109 void TearDown() override {
111 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( 110 EXPECT_TRUE(model_thread_.task_runner()->PostTask(
112 FROM_HERE, 111 FROM_HERE,
113 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService, 112 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService,
114 base::Unretained(this)))); 113 base::Unretained(this))));
115 // This must happen before the DB thread is stopped since 114 // This must happen before the DB thread is stopped since
116 // |shared_change_processor_| may post tasks to delete its members 115 // |shared_change_processor_| may post tasks to delete its members
117 // on the correct thread. 116 // on the correct thread.
118 // 117 //
119 // TODO(akalin): Write deterministic tests for the destruction of 118 // TODO(akalin): Write deterministic tests for the destruction of
120 // |shared_change_processor_| on the UI and DB threads. 119 // |shared_change_processor_| on the UI and DB threads.
121 shared_change_processor_ = nullptr; 120 shared_change_processor_ = nullptr;
122 backend_thread_.Stop(); 121 model_thread_.Stop();
123 122
124 // Note: Stop() joins the threads, and that barrier prevents this read 123 // Note: Stop() joins the threads, and that barrier prevents this read
125 // from being moved (e.g by compiler optimization) in such a way that it 124 // from being moved (e.g by compiler optimization) in such a way that it
126 // would race with the write in ConnectOnDBThread (because by this time, 125 // would race with the write in ConnectOnDBThread (because by this time,
127 // everything that could have run on |backend_thread_| has done so). 126 // everything that could have run on |model_thread_| has done so).
128 ASSERT_TRUE(did_connect_); 127 ASSERT_TRUE(did_connect_);
129 test_user_share_.TearDown(); 128 test_user_share_.TearDown();
130 } 129 }
131 130
132 // Connect |shared_change_processor_| on the DB thread. 131 // Connect |shared_change_processor_| on the DB thread.
133 void Connect() { 132 void Connect() {
134 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( 133 EXPECT_TRUE(model_thread_.task_runner()->PostTask(
135 FROM_HERE, 134 FROM_HERE,
136 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread, 135 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
137 base::Unretained(this), shared_change_processor_))); 136 base::Unretained(this), shared_change_processor_)));
138 } 137 }
139 138
140 void SetAttachmentStore() { 139 void SetAttachmentStore() {
141 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( 140 EXPECT_TRUE(model_thread_.task_runner()->PostTask(
142 FROM_HERE, 141 FROM_HERE,
143 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread, 142 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread,
144 base::Unretained(this)))); 143 base::Unretained(this))));
145 } 144 }
146 145
147 bool HasAttachmentService() { 146 bool HasAttachmentService() {
148 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, 147 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
149 base::WaitableEvent::InitialState::NOT_SIGNALED); 148 base::WaitableEvent::InitialState::NOT_SIGNALED);
150 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( 149 EXPECT_TRUE(model_thread_.task_runner()->PostTask(
151 FROM_HERE, 150 FROM_HERE,
152 base::Bind( 151 base::Bind(
153 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread, 152 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread,
154 base::Unretained(this), base::Unretained(&event)))); 153 base::Unretained(this), base::Unretained(&event))));
155 event.Wait(); 154 event.Wait();
156 return has_attachment_service_; 155 return has_attachment_service_;
157 } 156 }
158 157
159 private: 158 private:
160 // Used by SetUp(). 159 // Used by SetUp().
161 void SetUpDBSyncableService() { 160 void SetUpDBSyncableService() {
162 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); 161 DCHECK(model_thread_.task_runner()->BelongsToCurrentThread());
163 DCHECK(!db_syncable_service_.get()); 162 DCHECK(!db_syncable_service_.get());
164 db_syncable_service_ = base::MakeUnique<FakeSyncableService>(); 163 db_syncable_service_ = base::MakeUnique<FakeSyncableService>();
165 } 164 }
166 165
167 // Used by TearDown(). 166 // Used by TearDown().
168 void TearDownDBSyncableService() { 167 void TearDownDBSyncableService() {
169 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); 168 DCHECK(model_thread_.task_runner()->BelongsToCurrentThread());
170 DCHECK(db_syncable_service_.get()); 169 DCHECK(db_syncable_service_.get());
171 db_syncable_service_.reset(); 170 db_syncable_service_.reset();
172 } 171 }
173 172
174 void SetAttachmentStoreOnDBThread() { 173 void SetAttachmentStoreOnDBThread() {
175 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); 174 DCHECK(model_thread_.task_runner()->BelongsToCurrentThread());
176 DCHECK(db_syncable_service_.get()); 175 DCHECK(db_syncable_service_.get());
177 db_syncable_service_->set_attachment_store( 176 db_syncable_service_->set_attachment_store(
178 AttachmentStore::CreateInMemoryStore()); 177 AttachmentStore::CreateInMemoryStore());
179 } 178 }
180 179
181 // Used by Connect(). The SharedChangeProcessor is passed in 180 // Used by Connect(). The SharedChangeProcessor is passed in
182 // because we modify |shared_change_processor_| on the main thread 181 // because we modify |shared_change_processor_| on the main thread
183 // (in TearDown()). 182 // (in TearDown()).
184 void ConnectOnDBThread( 183 void ConnectOnDBThread(
185 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) { 184 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
186 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); 185 DCHECK(model_thread_.task_runner()->BelongsToCurrentThread());
187 EXPECT_TRUE(shared_change_processor->Connect( 186 EXPECT_TRUE(shared_change_processor->Connect(
188 this, &processor_factory_, test_user_share_.user_share(), 187 this, &processor_factory_, test_user_share_.user_share(),
189 base::MakeUnique<DataTypeErrorHandlerMock>(), 188 base::MakeUnique<DataTypeErrorHandlerMock>(),
190 base::WeakPtr<SyncMergeResult>())); 189 base::WeakPtr<SyncMergeResult>()));
191 did_connect_ = true; 190 did_connect_ = true;
192 } 191 }
193 192
194 void CheckAttachmentServiceOnDBThread(base::WaitableEvent* event) { 193 void CheckAttachmentServiceOnDBThread(base::WaitableEvent* event) {
195 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); 194 DCHECK(model_thread_.task_runner()->BelongsToCurrentThread());
196 DCHECK(db_syncable_service_.get()); 195 DCHECK(db_syncable_service_.get());
197 has_attachment_service_ = !!db_syncable_service_->attachment_service(); 196 has_attachment_service_ = !!db_syncable_service_->attachment_service();
198 event->Signal(); 197 event->Signal();
199 } 198 }
200 199
201 base::MessageLoop frontend_loop_; 200 base::MessageLoop frontend_loop_;
202 base::Thread backend_thread_; 201 base::Thread model_thread_;
203 TestUserShare test_user_share_; 202 TestUserShare test_user_share_;
204 TestSyncApiComponentFactory factory_; 203 TestSyncApiComponentFactory factory_;
205 204
206 scoped_refptr<SharedChangeProcessor> shared_change_processor_; 205 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
207 206
208 GenericChangeProcessorFactory processor_factory_; 207 GenericChangeProcessorFactory processor_factory_;
209 bool did_connect_; 208 bool did_connect_;
210 bool has_attachment_service_; 209 bool has_attachment_service_;
211 210
212 // Used only on DB thread. 211 // Used only on DB thread.
(...skipping 11 matching lines...) Expand all
224 // creates AttachmentService and passes it back to the syncable service. 223 // creates AttachmentService and passes it back to the syncable service.
225 TEST_F(SyncSharedChangeProcessorTest, ConnectWithAttachmentStore) { 224 TEST_F(SyncSharedChangeProcessorTest, ConnectWithAttachmentStore) {
226 SetAttachmentStore(); 225 SetAttachmentStore();
227 Connect(); 226 Connect();
228 EXPECT_TRUE(HasAttachmentService()); 227 EXPECT_TRUE(HasAttachmentService());
229 } 228 }
230 229
231 } // namespace 230 } // namespace
232 231
233 } // namespace syncer 232 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/driver/proxy_data_type_controller.cc ('k') | components/sync/driver/startup_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698