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> |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "components/leveldb_proto/leveldb_database.h" | 22 #include "components/leveldb_proto/leveldb_database.h" |
23 #include "components/leveldb_proto/testing/proto/test.pb.h" | 23 #include "components/leveldb_proto/testing/proto/test.pb.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
27 | 27 |
28 using base::MessageLoop; | 28 using base::MessageLoop; |
29 using base::ScopedTempDir; | 29 using base::ScopedTempDir; |
30 using testing::Invoke; | 30 using testing::Invoke; |
31 using testing::Return; | 31 using testing::Return; |
| 32 using testing::UnorderedElementsAre; |
32 using testing::_; | 33 using testing::_; |
33 | 34 |
34 namespace leveldb_proto { | 35 namespace leveldb_proto { |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
38 typedef std::map<std::string, TestProto> EntryMap; | 39 typedef std::map<std::string, TestProto> EntryMap; |
39 | 40 |
40 const char kTestLevelDBClientName[] = "Test"; | 41 const char kTestLevelDBClientName[] = "Test"; |
41 | 42 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 ASSERT_TRUE(model.count(key)); | 242 ASSERT_TRUE(model.count(key)); |
242 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model)); | 243 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model)); |
243 EXPECT_CALL(caller, GetCallback1(true, _)) | 244 EXPECT_CALL(caller, GetCallback1(true, _)) |
244 .WillOnce(VerifyGetEntry(model[key])); | 245 .WillOnce(VerifyGetEntry(model[key])); |
245 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback, | 246 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback, |
246 base::Unretained(&caller))); | 247 base::Unretained(&caller))); |
247 | 248 |
248 base::RunLoop().RunUntilIdle(); | 249 base::RunLoop().RunUntilIdle(); |
249 } | 250 } |
250 | 251 |
| 252 TEST(ProtoDatabaseImplLevelDBTest, TestDBSaveAndLoadKeys) { |
| 253 base::MessageLoop main_loop; |
| 254 |
| 255 ScopedTempDir temp_dir; |
| 256 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 257 base::Thread db_thread("dbthread"); |
| 258 ASSERT_TRUE(db_thread.Start()); |
| 259 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( |
| 260 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); |
| 261 |
| 262 auto expect_init_success = |
| 263 base::Bind([](bool success) { EXPECT_TRUE(success); }); |
| 264 db->Init(kTestLevelDBClientName, temp_dir.GetPath(), expect_init_success); |
| 265 |
| 266 base::RunLoop run_update_entries; |
| 267 auto expect_update_success = base::Bind( |
| 268 [](base::Closure signal, bool success) { |
| 269 EXPECT_TRUE(success); |
| 270 signal.Run(); |
| 271 }, |
| 272 run_update_entries.QuitClosure()); |
| 273 TestProto test_proto; |
| 274 test_proto.set_data("some data"); |
| 275 ProtoDatabase<TestProto>::KeyEntryVector data_set( |
| 276 {{"0", test_proto}, {"1", test_proto}, {"2", test_proto}}); |
| 277 db->UpdateEntries( |
| 278 base::MakeUnique<ProtoDatabase<TestProto>::KeyEntryVector>(data_set), |
| 279 base::MakeUnique<std::vector<std::string>>(), expect_update_success); |
| 280 run_update_entries.Run(); |
| 281 |
| 282 base::RunLoop run_load_keys; |
| 283 auto verify_loaded_keys = base::Bind( |
| 284 [](base::Closure signal, bool success, |
| 285 std::unique_ptr<std::vector<std::string>> keys) { |
| 286 EXPECT_TRUE(success); |
| 287 EXPECT_THAT(*keys, UnorderedElementsAre("0", "1", "2")); |
| 288 signal.Run(); |
| 289 }, |
| 290 run_load_keys.QuitClosure()); |
| 291 db->LoadKeys(verify_loaded_keys); |
| 292 run_load_keys.Run(); |
| 293 |
| 294 // Shutdown database. |
| 295 db.reset(); |
| 296 base::RunLoop run_destruction; |
| 297 db_thread.task_runner()->PostTaskAndReply( |
| 298 FROM_HERE, base::Bind(base::DoNothing), run_destruction.QuitClosure()); |
| 299 run_destruction.Run(); |
| 300 } |
| 301 |
251 TEST_F(ProtoDatabaseImplTest, TestDBGetNotFound) { | 302 TEST_F(ProtoDatabaseImplTest, TestDBGetNotFound) { |
252 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 303 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
253 | 304 |
254 MockDB* mock_db = new MockDB(); | 305 MockDB* mock_db = new MockDB(); |
255 MockDatabaseCaller caller; | 306 MockDatabaseCaller caller; |
256 EntryMap model = GetSmallModel(); | 307 EntryMap model = GetSmallModel(); |
257 | 308 |
258 EXPECT_CALL(*mock_db, Init(_)); | 309 EXPECT_CALL(*mock_db, Init(_)); |
259 EXPECT_CALL(caller, InitCallback(_)); | 310 EXPECT_CALL(caller, InitCallback(_)); |
260 db_->InitWithDatabase( | 311 db_->InitWithDatabase( |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 ->system_allocator_pool_name(); | 647 ->system_allocator_pool_name(); |
597 size_t expected_dump_count = system_allocator_pool_name ? 2 : 1; | 648 size_t expected_dump_count = system_allocator_pool_name ? 2 : 1; |
598 EXPECT_EQ(expected_dump_count, allocator_dumps.size()); | 649 EXPECT_EQ(expected_dump_count, allocator_dumps.size()); |
599 for (const auto& dump : allocator_dumps) { | 650 for (const auto& dump : allocator_dumps) { |
600 ASSERT_TRUE(dump.first.find("leveldb/leveldb_proto/") == 0 || | 651 ASSERT_TRUE(dump.first.find("leveldb/leveldb_proto/") == 0 || |
601 dump.first.find(system_allocator_pool_name) == 0); | 652 dump.first.find(system_allocator_pool_name) == 0); |
602 } | 653 } |
603 } | 654 } |
604 | 655 |
605 } // namespace leveldb_proto | 656 } // namespace leveldb_proto |
OLD | NEW |