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/leveldb_proto/proto_database_impl.h" | 5 #include "components/leveldb_proto/proto_database_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
8 #include <map> | 9 #include <map> |
| 10 #include <memory> |
9 #include <utility> | 11 #include <utility> |
10 | 12 |
11 #include "base/bind.h" | 13 #include "base/bind.h" |
12 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
14 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/ptr_util.h" |
15 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
16 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
17 #include "components/leveldb_proto/leveldb_database.h" | 20 #include "components/leveldb_proto/leveldb_database.h" |
18 #include "components/leveldb_proto/testing/proto/test.pb.h" | 21 #include "components/leveldb_proto/testing/proto/test.pb.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
22 | 25 |
23 using base::MessageLoop; | 26 using base::MessageLoop; |
24 using base::ScopedTempDir; | 27 using base::ScopedTempDir; |
(...skipping 20 matching lines...) Expand all Loading... |
45 ON_CALL(*this, Save(_, _)).WillByDefault(Return(true)); | 48 ON_CALL(*this, Save(_, _)).WillByDefault(Return(true)); |
46 ON_CALL(*this, Load(_)).WillByDefault(Return(true)); | 49 ON_CALL(*this, Load(_)).WillByDefault(Return(true)); |
47 } | 50 } |
48 }; | 51 }; |
49 | 52 |
50 class MockDatabaseCaller { | 53 class MockDatabaseCaller { |
51 public: | 54 public: |
52 MOCK_METHOD1(InitCallback, void(bool)); | 55 MOCK_METHOD1(InitCallback, void(bool)); |
53 MOCK_METHOD1(DestroyCallback, void(bool)); | 56 MOCK_METHOD1(DestroyCallback, void(bool)); |
54 MOCK_METHOD1(SaveCallback, void(bool)); | 57 MOCK_METHOD1(SaveCallback, void(bool)); |
55 void LoadCallback(bool success, scoped_ptr<std::vector<TestProto> > entries) { | 58 void LoadCallback(bool success, |
| 59 std::unique_ptr<std::vector<TestProto>> entries) { |
56 LoadCallback1(success, entries.get()); | 60 LoadCallback1(success, entries.get()); |
57 } | 61 } |
58 MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*)); | 62 MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*)); |
59 }; | 63 }; |
60 | 64 |
61 } // namespace | 65 } // namespace |
62 | 66 |
63 EntryMap GetSmallModel() { | 67 EntryMap GetSmallModel() { |
64 EntryMap model; | 68 EntryMap model; |
65 | 69 |
(...skipping 28 matching lines...) Expand all Loading... |
94 main_loop_.reset(new MessageLoop()); | 98 main_loop_.reset(new MessageLoop()); |
95 db_.reset(new ProtoDatabaseImpl<TestProto>(main_loop_->task_runner())); | 99 db_.reset(new ProtoDatabaseImpl<TestProto>(main_loop_->task_runner())); |
96 } | 100 } |
97 | 101 |
98 void TearDown() override { | 102 void TearDown() override { |
99 db_.reset(); | 103 db_.reset(); |
100 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
101 main_loop_.reset(); | 105 main_loop_.reset(); |
102 } | 106 } |
103 | 107 |
104 scoped_ptr<ProtoDatabaseImpl<TestProto> > db_; | 108 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db_; |
105 scoped_ptr<MessageLoop> main_loop_; | 109 std::unique_ptr<MessageLoop> main_loop_; |
106 }; | 110 }; |
107 | 111 |
108 // Test that ProtoDatabaseImpl calls Init on the underlying database and that | 112 // Test that ProtoDatabaseImpl calls Init on the underlying database and that |
109 // the caller's InitCallback is called with the correct value. | 113 // the caller's InitCallback is called with the correct value. |
110 TEST_F(ProtoDatabaseImplTest, TestDBInitSuccess) { | 114 TEST_F(ProtoDatabaseImplTest, TestDBInitSuccess) { |
111 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 115 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
112 | 116 |
113 MockDB* mock_db = new MockDB(); | 117 MockDB* mock_db = new MockDB(); |
114 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true)); | 118 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true)); |
115 | 119 |
116 MockDatabaseCaller caller; | 120 MockDatabaseCaller caller; |
117 EXPECT_CALL(caller, InitCallback(true)); | 121 EXPECT_CALL(caller, InitCallback(true)); |
118 | 122 |
119 db_->InitWithDatabase( | 123 db_->InitWithDatabase( |
120 make_scoped_ptr(mock_db), path, | 124 base::WrapUnique(mock_db), path, |
121 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 125 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
122 | 126 |
123 base::RunLoop().RunUntilIdle(); | 127 base::RunLoop().RunUntilIdle(); |
124 } | 128 } |
125 | 129 |
126 TEST_F(ProtoDatabaseImplTest, TestDBInitFailure) { | 130 TEST_F(ProtoDatabaseImplTest, TestDBInitFailure) { |
127 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 131 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
128 | 132 |
129 MockDB* mock_db = new MockDB(); | 133 MockDB* mock_db = new MockDB(); |
130 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(false)); | 134 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(false)); |
131 | 135 |
132 MockDatabaseCaller caller; | 136 MockDatabaseCaller caller; |
133 EXPECT_CALL(caller, InitCallback(false)); | 137 EXPECT_CALL(caller, InitCallback(false)); |
134 | 138 |
135 db_->InitWithDatabase( | 139 db_->InitWithDatabase( |
136 make_scoped_ptr(mock_db), path, | 140 base::WrapUnique(mock_db), path, |
137 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 141 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
138 | 142 |
139 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
140 } | 144 } |
141 | 145 |
142 ACTION_P(AppendLoadEntries, model) { | 146 ACTION_P(AppendLoadEntries, model) { |
143 std::vector<std::string>* output = arg0; | 147 std::vector<std::string>* output = arg0; |
144 for (const auto& pair : model) | 148 for (const auto& pair : model) |
145 output->push_back(pair.second.SerializeAsString()); | 149 output->push_back(pair.second.SerializeAsString()); |
146 | 150 |
(...skipping 12 matching lines...) Expand all Loading... |
159 TEST_F(ProtoDatabaseImplTest, TestDBLoadSuccess) { | 163 TEST_F(ProtoDatabaseImplTest, TestDBLoadSuccess) { |
160 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 164 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
161 | 165 |
162 MockDB* mock_db = new MockDB(); | 166 MockDB* mock_db = new MockDB(); |
163 MockDatabaseCaller caller; | 167 MockDatabaseCaller caller; |
164 EntryMap model = GetSmallModel(); | 168 EntryMap model = GetSmallModel(); |
165 | 169 |
166 EXPECT_CALL(*mock_db, Init(_)); | 170 EXPECT_CALL(*mock_db, Init(_)); |
167 EXPECT_CALL(caller, InitCallback(_)); | 171 EXPECT_CALL(caller, InitCallback(_)); |
168 db_->InitWithDatabase( | 172 db_->InitWithDatabase( |
169 make_scoped_ptr(mock_db), path, | 173 base::WrapUnique(mock_db), path, |
170 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 174 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
171 | 175 |
172 EXPECT_CALL(*mock_db, Load(_)).WillOnce(AppendLoadEntries(model)); | 176 EXPECT_CALL(*mock_db, Load(_)).WillOnce(AppendLoadEntries(model)); |
173 EXPECT_CALL(caller, LoadCallback1(true, _)) | 177 EXPECT_CALL(caller, LoadCallback1(true, _)) |
174 .WillOnce(VerifyLoadEntries(testing::ByRef(model))); | 178 .WillOnce(VerifyLoadEntries(testing::ByRef(model))); |
175 db_->LoadEntries( | 179 db_->LoadEntries( |
176 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); | 180 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); |
177 | 181 |
178 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
179 } | 183 } |
180 | 184 |
181 TEST_F(ProtoDatabaseImplTest, TestDBLoadFailure) { | 185 TEST_F(ProtoDatabaseImplTest, TestDBLoadFailure) { |
182 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 186 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
183 | 187 |
184 MockDB* mock_db = new MockDB(); | 188 MockDB* mock_db = new MockDB(); |
185 MockDatabaseCaller caller; | 189 MockDatabaseCaller caller; |
186 | 190 |
187 EXPECT_CALL(*mock_db, Init(_)); | 191 EXPECT_CALL(*mock_db, Init(_)); |
188 EXPECT_CALL(caller, InitCallback(_)); | 192 EXPECT_CALL(caller, InitCallback(_)); |
189 db_->InitWithDatabase( | 193 db_->InitWithDatabase( |
190 make_scoped_ptr(mock_db), path, | 194 base::WrapUnique(mock_db), path, |
191 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 195 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
192 | 196 |
193 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false)); | 197 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false)); |
194 EXPECT_CALL(caller, LoadCallback1(false, _)); | 198 EXPECT_CALL(caller, LoadCallback1(false, _)); |
195 db_->LoadEntries( | 199 db_->LoadEntries( |
196 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); | 200 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); |
197 | 201 |
198 base::RunLoop().RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
199 } | 203 } |
200 | 204 |
(...skipping 22 matching lines...) Expand all Loading... |
223 TEST_F(ProtoDatabaseImplTest, TestDBSaveSuccess) { | 227 TEST_F(ProtoDatabaseImplTest, TestDBSaveSuccess) { |
224 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 228 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
225 | 229 |
226 MockDB* mock_db = new MockDB(); | 230 MockDB* mock_db = new MockDB(); |
227 MockDatabaseCaller caller; | 231 MockDatabaseCaller caller; |
228 EntryMap model = GetSmallModel(); | 232 EntryMap model = GetSmallModel(); |
229 | 233 |
230 EXPECT_CALL(*mock_db, Init(_)); | 234 EXPECT_CALL(*mock_db, Init(_)); |
231 EXPECT_CALL(caller, InitCallback(_)); | 235 EXPECT_CALL(caller, InitCallback(_)); |
232 db_->InitWithDatabase( | 236 db_->InitWithDatabase( |
233 make_scoped_ptr(mock_db), path, | 237 base::WrapUnique(mock_db), path, |
234 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 238 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
235 | 239 |
236 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( | 240 std::unique_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( |
237 new ProtoDatabase<TestProto>::KeyEntryVector()); | 241 new ProtoDatabase<TestProto>::KeyEntryVector()); |
238 for (const auto& pair : model) | 242 for (const auto& pair : model) |
239 entries->push_back(std::make_pair(pair.second.id(), pair.second)); | 243 entries->push_back(std::make_pair(pair.second.id(), pair.second)); |
240 | 244 |
241 scoped_ptr<KeyVector> keys_to_remove(new KeyVector()); | 245 std::unique_ptr<KeyVector> keys_to_remove(new KeyVector()); |
242 | 246 |
243 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyUpdateEntries(model)); | 247 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyUpdateEntries(model)); |
244 EXPECT_CALL(caller, SaveCallback(true)); | 248 EXPECT_CALL(caller, SaveCallback(true)); |
245 db_->UpdateEntries( | 249 db_->UpdateEntries( |
246 std::move(entries), std::move(keys_to_remove), | 250 std::move(entries), std::move(keys_to_remove), |
247 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); | 251 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); |
248 | 252 |
249 base::RunLoop().RunUntilIdle(); | 253 base::RunLoop().RunUntilIdle(); |
250 } | 254 } |
251 | 255 |
252 TEST_F(ProtoDatabaseImplTest, TestDBSaveFailure) { | 256 TEST_F(ProtoDatabaseImplTest, TestDBSaveFailure) { |
253 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 257 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
254 | 258 |
255 MockDB* mock_db = new MockDB(); | 259 MockDB* mock_db = new MockDB(); |
256 MockDatabaseCaller caller; | 260 MockDatabaseCaller caller; |
257 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( | 261 std::unique_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( |
258 new ProtoDatabase<TestProto>::KeyEntryVector()); | 262 new ProtoDatabase<TestProto>::KeyEntryVector()); |
259 scoped_ptr<KeyVector> keys_to_remove(new KeyVector()); | 263 std::unique_ptr<KeyVector> keys_to_remove(new KeyVector()); |
260 | 264 |
261 EXPECT_CALL(*mock_db, Init(_)); | 265 EXPECT_CALL(*mock_db, Init(_)); |
262 EXPECT_CALL(caller, InitCallback(_)); | 266 EXPECT_CALL(caller, InitCallback(_)); |
263 db_->InitWithDatabase( | 267 db_->InitWithDatabase( |
264 make_scoped_ptr(mock_db), path, | 268 base::WrapUnique(mock_db), path, |
265 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 269 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
266 | 270 |
267 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false)); | 271 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false)); |
268 EXPECT_CALL(caller, SaveCallback(false)); | 272 EXPECT_CALL(caller, SaveCallback(false)); |
269 db_->UpdateEntries( | 273 db_->UpdateEntries( |
270 std::move(entries), std::move(keys_to_remove), | 274 std::move(entries), std::move(keys_to_remove), |
271 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); | 275 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); |
272 | 276 |
273 base::RunLoop().RunUntilIdle(); | 277 base::RunLoop().RunUntilIdle(); |
274 } | 278 } |
275 | 279 |
276 // Test that ProtoDatabaseImpl calls Save on the underlying database with the | 280 // Test that ProtoDatabaseImpl calls Save on the underlying database with the |
277 // correct entries to delete and that the caller's SaveCallback is called with | 281 // correct entries to delete and that the caller's SaveCallback is called with |
278 // the correct success value. | 282 // the correct success value. |
279 TEST_F(ProtoDatabaseImplTest, TestDBRemoveSuccess) { | 283 TEST_F(ProtoDatabaseImplTest, TestDBRemoveSuccess) { |
280 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 284 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
281 | 285 |
282 MockDB* mock_db = new MockDB(); | 286 MockDB* mock_db = new MockDB(); |
283 MockDatabaseCaller caller; | 287 MockDatabaseCaller caller; |
284 EntryMap model = GetSmallModel(); | 288 EntryMap model = GetSmallModel(); |
285 | 289 |
286 EXPECT_CALL(*mock_db, Init(_)); | 290 EXPECT_CALL(*mock_db, Init(_)); |
287 EXPECT_CALL(caller, InitCallback(_)); | 291 EXPECT_CALL(caller, InitCallback(_)); |
288 db_->InitWithDatabase( | 292 db_->InitWithDatabase( |
289 make_scoped_ptr(mock_db), path, | 293 base::WrapUnique(mock_db), path, |
290 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 294 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
291 | 295 |
292 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( | 296 std::unique_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( |
293 new ProtoDatabase<TestProto>::KeyEntryVector()); | 297 new ProtoDatabase<TestProto>::KeyEntryVector()); |
294 scoped_ptr<KeyVector> keys_to_remove(new KeyVector()); | 298 std::unique_ptr<KeyVector> keys_to_remove(new KeyVector()); |
295 for (const auto& pair : model) | 299 for (const auto& pair : model) |
296 keys_to_remove->push_back(pair.second.id()); | 300 keys_to_remove->push_back(pair.second.id()); |
297 | 301 |
298 KeyVector keys_copy(*keys_to_remove.get()); | 302 KeyVector keys_copy(*keys_to_remove.get()); |
299 EXPECT_CALL(*mock_db, Save(_, keys_copy)).WillOnce(Return(true)); | 303 EXPECT_CALL(*mock_db, Save(_, keys_copy)).WillOnce(Return(true)); |
300 EXPECT_CALL(caller, SaveCallback(true)); | 304 EXPECT_CALL(caller, SaveCallback(true)); |
301 db_->UpdateEntries( | 305 db_->UpdateEntries( |
302 std::move(entries), std::move(keys_to_remove), | 306 std::move(entries), std::move(keys_to_remove), |
303 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); | 307 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); |
304 | 308 |
305 base::RunLoop().RunUntilIdle(); | 309 base::RunLoop().RunUntilIdle(); |
306 } | 310 } |
307 | 311 |
308 TEST_F(ProtoDatabaseImplTest, TestDBRemoveFailure) { | 312 TEST_F(ProtoDatabaseImplTest, TestDBRemoveFailure) { |
309 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 313 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
310 | 314 |
311 MockDB* mock_db = new MockDB(); | 315 MockDB* mock_db = new MockDB(); |
312 MockDatabaseCaller caller; | 316 MockDatabaseCaller caller; |
313 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( | 317 std::unique_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries( |
314 new ProtoDatabase<TestProto>::KeyEntryVector()); | 318 new ProtoDatabase<TestProto>::KeyEntryVector()); |
315 scoped_ptr<KeyVector> keys_to_remove(new KeyVector()); | 319 std::unique_ptr<KeyVector> keys_to_remove(new KeyVector()); |
316 | 320 |
317 EXPECT_CALL(*mock_db, Init(_)); | 321 EXPECT_CALL(*mock_db, Init(_)); |
318 EXPECT_CALL(caller, InitCallback(_)); | 322 EXPECT_CALL(caller, InitCallback(_)); |
319 db_->InitWithDatabase( | 323 db_->InitWithDatabase( |
320 make_scoped_ptr(mock_db), path, | 324 base::WrapUnique(mock_db), path, |
321 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 325 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
322 | 326 |
323 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false)); | 327 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false)); |
324 EXPECT_CALL(caller, SaveCallback(false)); | 328 EXPECT_CALL(caller, SaveCallback(false)); |
325 db_->UpdateEntries( | 329 db_->UpdateEntries( |
326 std::move(entries), std::move(keys_to_remove), | 330 std::move(entries), std::move(keys_to_remove), |
327 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); | 331 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); |
328 | 332 |
329 base::RunLoop().RunUntilIdle(); | 333 base::RunLoop().RunUntilIdle(); |
330 } | 334 } |
331 | 335 |
332 // This tests that normal usage of the real database does not cause any | 336 // This tests that normal usage of the real database does not cause any |
333 // threading violations. | 337 // threading violations. |
334 TEST(ProtoDatabaseImplThreadingTest, TestDBDestruction) { | 338 TEST(ProtoDatabaseImplThreadingTest, TestDBDestruction) { |
335 base::MessageLoop main_loop; | 339 base::MessageLoop main_loop; |
336 | 340 |
337 ScopedTempDir temp_dir; | 341 ScopedTempDir temp_dir; |
338 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 342 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
339 | 343 |
340 base::Thread db_thread("dbthread"); | 344 base::Thread db_thread("dbthread"); |
341 ASSERT_TRUE(db_thread.Start()); | 345 ASSERT_TRUE(db_thread.Start()); |
342 | 346 |
343 scoped_ptr<ProtoDatabaseImpl<TestProto>> db( | 347 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( |
344 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); | 348 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); |
345 | 349 |
346 MockDatabaseCaller caller; | 350 MockDatabaseCaller caller; |
347 EXPECT_CALL(caller, InitCallback(_)); | 351 EXPECT_CALL(caller, InitCallback(_)); |
348 db->Init( | 352 db->Init( |
349 kTestLevelDBClientName, temp_dir.path(), | 353 kTestLevelDBClientName, temp_dir.path(), |
350 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 354 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
351 | 355 |
352 db.reset(); | 356 db.reset(); |
353 | 357 |
354 base::RunLoop run_loop; | 358 base::RunLoop run_loop; |
355 db_thread.task_runner()->PostTaskAndReply( | 359 db_thread.task_runner()->PostTaskAndReply( |
356 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); | 360 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); |
357 run_loop.Run(); | 361 run_loop.Run(); |
358 } | 362 } |
359 | 363 |
360 // This tests that normal usage of the real database does not cause any | 364 // This tests that normal usage of the real database does not cause any |
361 // threading violations. | 365 // threading violations. |
362 TEST(ProtoDatabaseImplThreadingTest, TestDBDestroy) { | 366 TEST(ProtoDatabaseImplThreadingTest, TestDBDestroy) { |
363 base::MessageLoop main_loop; | 367 base::MessageLoop main_loop; |
364 | 368 |
365 ScopedTempDir temp_dir; | 369 ScopedTempDir temp_dir; |
366 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 370 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
367 | 371 |
368 base::Thread db_thread("dbthread"); | 372 base::Thread db_thread("dbthread"); |
369 ASSERT_TRUE(db_thread.Start()); | 373 ASSERT_TRUE(db_thread.Start()); |
370 | 374 |
371 scoped_ptr<ProtoDatabaseImpl<TestProto>> db( | 375 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( |
372 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); | 376 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); |
373 | 377 |
374 MockDatabaseCaller caller; | 378 MockDatabaseCaller caller; |
375 EXPECT_CALL(caller, InitCallback(_)); | 379 EXPECT_CALL(caller, InitCallback(_)); |
376 db->Init( | 380 db->Init( |
377 kTestLevelDBClientName, temp_dir.path(), | 381 kTestLevelDBClientName, temp_dir.path(), |
378 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 382 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
379 | 383 |
380 EXPECT_CALL(caller, DestroyCallback(_)); | 384 EXPECT_CALL(caller, DestroyCallback(_)); |
381 db->Destroy(base::Bind(&MockDatabaseCaller::DestroyCallback, | 385 db->Destroy(base::Bind(&MockDatabaseCaller::DestroyCallback, |
(...skipping 18 matching lines...) Expand all Loading... |
400 | 404 |
401 KeyValueVector save_entries; | 405 KeyValueVector save_entries; |
402 std::vector<std::string> load_entries; | 406 std::vector<std::string> load_entries; |
403 KeyVector remove_keys; | 407 KeyVector remove_keys; |
404 | 408 |
405 for (const auto& pair : model) { | 409 for (const auto& pair : model) { |
406 save_entries.push_back( | 410 save_entries.push_back( |
407 std::make_pair(pair.second.id(), pair.second.SerializeAsString())); | 411 std::make_pair(pair.second.id(), pair.second.SerializeAsString())); |
408 } | 412 } |
409 | 413 |
410 scoped_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 414 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
411 EXPECT_TRUE(db->Init(temp_dir.path())); | 415 EXPECT_TRUE(db->Init(temp_dir.path())); |
412 EXPECT_TRUE(db->Save(save_entries, remove_keys)); | 416 EXPECT_TRUE(db->Save(save_entries, remove_keys)); |
413 | 417 |
414 if (close_after_save) { | 418 if (close_after_save) { |
415 db.reset(new LevelDB(kTestLevelDBClientName)); | 419 db.reset(new LevelDB(kTestLevelDBClientName)); |
416 EXPECT_TRUE(db->Init(temp_dir.path())); | 420 EXPECT_TRUE(db->Init(temp_dir.path())); |
417 } | 421 } |
418 | 422 |
419 EXPECT_TRUE(db->Load(&load_entries)); | 423 EXPECT_TRUE(db->Load(&load_entries)); |
420 | 424 |
(...skipping 15 matching lines...) Expand all Loading... |
436 TEST(ProtoDatabaseImplLevelDBTest, TestDBCloseAndReopen) { | 440 TEST(ProtoDatabaseImplLevelDBTest, TestDBCloseAndReopen) { |
437 TestLevelDBSaveAndLoad(true); | 441 TestLevelDBSaveAndLoad(true); |
438 } | 442 } |
439 | 443 |
440 TEST(ProtoDatabaseImplLevelDBTest, TestDBInitFail) { | 444 TEST(ProtoDatabaseImplLevelDBTest, TestDBInitFail) { |
441 ScopedTempDir temp_dir; | 445 ScopedTempDir temp_dir; |
442 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 446 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
443 | 447 |
444 leveldb::Options options; | 448 leveldb::Options options; |
445 options.create_if_missing = false; | 449 options.create_if_missing = false; |
446 scoped_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 450 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
447 | 451 |
448 KeyValueVector save_entries; | 452 KeyValueVector save_entries; |
449 std::vector<std::string> load_entries; | 453 std::vector<std::string> load_entries; |
450 KeyVector remove_keys; | 454 KeyVector remove_keys; |
451 | 455 |
452 EXPECT_FALSE(db->InitWithOptions(temp_dir.path(), options)); | 456 EXPECT_FALSE(db->InitWithOptions(temp_dir.path(), options)); |
453 EXPECT_FALSE(db->Load(&load_entries)); | 457 EXPECT_FALSE(db->Load(&load_entries)); |
454 EXPECT_FALSE(db->Save(save_entries, remove_keys)); | 458 EXPECT_FALSE(db->Save(save_entries, remove_keys)); |
455 } | 459 } |
456 | 460 |
457 TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { | 461 TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { |
458 scoped_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 462 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
459 | 463 |
460 std::vector<std::string> load_entries; | 464 std::vector<std::string> load_entries; |
461 | 465 |
462 ASSERT_TRUE(db->Init(base::FilePath())); | 466 ASSERT_TRUE(db->Init(base::FilePath())); |
463 | 467 |
464 ASSERT_TRUE(db->Load(&load_entries)); | 468 ASSERT_TRUE(db->Load(&load_entries)); |
465 EXPECT_EQ(0u, load_entries.size()); | 469 EXPECT_EQ(0u, load_entries.size()); |
466 | 470 |
467 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); | 471 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); |
468 KeyVector remove_keys; | 472 KeyVector remove_keys; |
469 | 473 |
470 ASSERT_TRUE(db->Save(save_entries, remove_keys)); | 474 ASSERT_TRUE(db->Save(save_entries, remove_keys)); |
471 | 475 |
472 std::vector<std::string> second_load_entries; | 476 std::vector<std::string> second_load_entries; |
473 | 477 |
474 ASSERT_TRUE(db->Load(&second_load_entries)); | 478 ASSERT_TRUE(db->Load(&second_load_entries)); |
475 EXPECT_EQ(1u, second_load_entries.size()); | 479 EXPECT_EQ(1u, second_load_entries.size()); |
476 } | 480 } |
477 | 481 |
478 } // namespace leveldb_proto | 482 } // namespace leveldb_proto |
OLD | NEW |