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 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "components/leveldb_proto/leveldb_database.h" | 20 #include "components/leveldb_proto/leveldb_database.h" |
21 #include "components/leveldb_proto/testing/proto/test.pb.h" | 21 #include "components/leveldb_proto/testing/proto/test.pb.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
25 | 25 |
26 using base::MessageLoop; | 26 using base::MessageLoop; |
27 using base::ScopedTempDir; | 27 using base::ScopedTempDir; |
28 using testing::Invoke; | 28 using testing::Invoke; |
29 using testing::Return; | 29 using testing::Return; |
| 30 using testing::UnorderedElementsAre; |
30 using testing::_; | 31 using testing::_; |
31 | 32 |
32 namespace leveldb_proto { | 33 namespace leveldb_proto { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 typedef std::map<std::string, TestProto> EntryMap; | 37 typedef std::map<std::string, TestProto> EntryMap; |
37 | 38 |
38 const char kTestLevelDBClientName[] = "Test"; | 39 const char kTestLevelDBClientName[] = "Test"; |
39 | 40 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 ASSERT_TRUE(model.count(key)); | 240 ASSERT_TRUE(model.count(key)); |
240 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model)); | 241 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model)); |
241 EXPECT_CALL(caller, GetCallback1(true, _)) | 242 EXPECT_CALL(caller, GetCallback1(true, _)) |
242 .WillOnce(VerifyGetEntry(model[key])); | 243 .WillOnce(VerifyGetEntry(model[key])); |
243 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback, | 244 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback, |
244 base::Unretained(&caller))); | 245 base::Unretained(&caller))); |
245 | 246 |
246 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
247 } | 248 } |
248 | 249 |
| 250 TEST(ProtoDatabaseImplLevelDBTest, TestDBSaveAndLoadKeys) { |
| 251 base::MessageLoop main_loop; |
| 252 |
| 253 ScopedTempDir temp_dir; |
| 254 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 255 base::Thread db_thread("dbthread"); |
| 256 ASSERT_TRUE(db_thread.Start()); |
| 257 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( |
| 258 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); |
| 259 |
| 260 auto expect_init_success = |
| 261 base::Bind([](bool success) { EXPECT_TRUE(success); }); |
| 262 db->Init(kTestLevelDBClientName, temp_dir.path(), expect_init_success); |
| 263 |
| 264 base::RunLoop run_update_entries; |
| 265 auto expect_update_success = base::Bind( |
| 266 [](base::Closure signal, bool success) { |
| 267 EXPECT_TRUE(success); |
| 268 signal.Run(); |
| 269 }, |
| 270 run_update_entries.QuitClosure()); |
| 271 TestProto test_proto; |
| 272 test_proto.set_data("some data"); |
| 273 ProtoDatabase<TestProto>::KeyEntryVector data_set( |
| 274 {{"0", test_proto}, {"1", test_proto}, {"2", test_proto}}); |
| 275 db->UpdateEntries( |
| 276 base::MakeUnique<ProtoDatabase<TestProto>::KeyEntryVector>(data_set), |
| 277 base::MakeUnique<std::vector<std::string>>(), expect_update_success); |
| 278 run_update_entries.Run(); |
| 279 |
| 280 base::RunLoop run_load_keys; |
| 281 auto verify_loaded_keys = base::Bind( |
| 282 [](base::Closure signal, bool success, |
| 283 std::unique_ptr<std::vector<std::string>> keys) { |
| 284 EXPECT_TRUE(success); |
| 285 EXPECT_THAT(*keys, UnorderedElementsAre("0", "1", "2")); |
| 286 signal.Run(); |
| 287 }, |
| 288 run_load_keys.QuitClosure()); |
| 289 db->LoadKeys(verify_loaded_keys); |
| 290 run_load_keys.Run(); |
| 291 |
| 292 // Shutdown database. |
| 293 db.reset(); |
| 294 base::RunLoop run_destruction; |
| 295 db_thread.task_runner()->PostTaskAndReply( |
| 296 FROM_HERE, base::Bind(base::DoNothing), run_destruction.QuitClosure()); |
| 297 run_destruction.Run(); |
| 298 } |
| 299 |
249 TEST_F(ProtoDatabaseImplTest, TestDBGetNotFound) { | 300 TEST_F(ProtoDatabaseImplTest, TestDBGetNotFound) { |
250 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 301 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
251 | 302 |
252 MockDB* mock_db = new MockDB(); | 303 MockDB* mock_db = new MockDB(); |
253 MockDatabaseCaller caller; | 304 MockDatabaseCaller caller; |
254 EntryMap model = GetSmallModel(); | 305 EntryMap model = GetSmallModel(); |
255 | 306 |
256 EXPECT_CALL(*mock_db, Init(_)); | 307 EXPECT_CALL(*mock_db, Init(_)); |
257 EXPECT_CALL(caller, InitCallback(_)); | 308 EXPECT_CALL(caller, InitCallback(_)); |
258 db_->InitWithDatabase( | 309 db_->InitWithDatabase( |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 | 614 |
564 ASSERT_TRUE(db->Save(save_entries, remove_keys)); | 615 ASSERT_TRUE(db->Save(save_entries, remove_keys)); |
565 | 616 |
566 std::vector<std::string> second_load_entries; | 617 std::vector<std::string> second_load_entries; |
567 | 618 |
568 ASSERT_TRUE(db->Load(&second_load_entries)); | 619 ASSERT_TRUE(db->Load(&second_load_entries)); |
569 EXPECT_EQ(1u, second_load_entries.size()); | 620 EXPECT_EQ(1u, second_load_entries.size()); |
570 } | 621 } |
571 | 622 |
572 } // namespace leveldb_proto | 623 } // namespace leveldb_proto |
OLD | NEW |