| 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 "base/trace_event/memory_dump_manager.h" |
| 21 #include "base/trace_event/process_memory_dump.h" |
| 20 #include "components/leveldb_proto/leveldb_database.h" | 22 #include "components/leveldb_proto/leveldb_database.h" |
| 21 #include "components/leveldb_proto/testing/proto/test.pb.h" | 23 #include "components/leveldb_proto/testing/proto/test.pb.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| 25 | 27 |
| 26 using base::MessageLoop; | 28 using base::MessageLoop; |
| 27 using base::ScopedTempDir; | 29 using base::ScopedTempDir; |
| 28 using testing::Invoke; | 30 using testing::Invoke; |
| 29 using testing::Return; | 31 using testing::Return; |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 base::RunLoop run_loop; | 482 base::RunLoop run_loop; |
| 481 db_thread.task_runner()->PostTaskAndReply( | 483 db_thread.task_runner()->PostTaskAndReply( |
| 482 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); | 484 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); |
| 483 run_loop.Run(); | 485 run_loop.Run(); |
| 484 } | 486 } |
| 485 | 487 |
| 486 // Test that the LevelDB properly saves entries and that load returns the saved | 488 // Test that the LevelDB properly saves entries and that load returns the saved |
| 487 // entries. If |close_after_save| is true, the database will be closed after | 489 // entries. If |close_after_save| is true, the database will be closed after |
| 488 // saving and then re-opened to ensure that the data is properly persisted. | 490 // saving and then re-opened to ensure that the data is properly persisted. |
| 489 void TestLevelDBSaveAndLoad(bool close_after_save) { | 491 void TestLevelDBSaveAndLoad(bool close_after_save) { |
| 492 base::MessageLoop main_loop; |
| 493 |
| 490 ScopedTempDir temp_dir; | 494 ScopedTempDir temp_dir; |
| 491 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 495 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 492 | 496 |
| 493 EntryMap model = GetSmallModel(); | 497 EntryMap model = GetSmallModel(); |
| 494 | 498 |
| 495 KeyValueVector save_entries; | 499 KeyValueVector save_entries; |
| 496 std::vector<std::string> load_entries; | 500 std::vector<std::string> load_entries; |
| 497 KeyVector remove_keys; | 501 KeyVector remove_keys; |
| 498 | 502 |
| 499 for (const auto& pair : model) { | 503 for (const auto& pair : model) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 KeyValueVector save_entries; | 546 KeyValueVector save_entries; |
| 543 std::vector<std::string> load_entries; | 547 std::vector<std::string> load_entries; |
| 544 KeyVector remove_keys; | 548 KeyVector remove_keys; |
| 545 | 549 |
| 546 EXPECT_FALSE(db->InitWithOptions(temp_dir.path(), options)); | 550 EXPECT_FALSE(db->InitWithOptions(temp_dir.path(), options)); |
| 547 EXPECT_FALSE(db->Load(&load_entries)); | 551 EXPECT_FALSE(db->Load(&load_entries)); |
| 548 EXPECT_FALSE(db->Save(save_entries, remove_keys)); | 552 EXPECT_FALSE(db->Save(save_entries, remove_keys)); |
| 549 } | 553 } |
| 550 | 554 |
| 551 TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { | 555 TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { |
| 556 base::MessageLoop main_loop; |
| 557 |
| 552 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 558 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
| 553 | 559 |
| 554 std::vector<std::string> load_entries; | 560 std::vector<std::string> load_entries; |
| 555 | 561 |
| 556 ASSERT_TRUE(db->Init(base::FilePath())); | 562 ASSERT_TRUE(db->Init(base::FilePath())); |
| 557 | 563 |
| 558 ASSERT_TRUE(db->Load(&load_entries)); | 564 ASSERT_TRUE(db->Load(&load_entries)); |
| 559 EXPECT_EQ(0u, load_entries.size()); | 565 EXPECT_EQ(0u, load_entries.size()); |
| 560 | 566 |
| 561 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); | 567 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); |
| 562 KeyVector remove_keys; | 568 KeyVector remove_keys; |
| 563 | 569 |
| 564 ASSERT_TRUE(db->Save(save_entries, remove_keys)); | 570 ASSERT_TRUE(db->Save(save_entries, remove_keys)); |
| 565 | 571 |
| 566 std::vector<std::string> second_load_entries; | 572 std::vector<std::string> second_load_entries; |
| 567 | 573 |
| 568 ASSERT_TRUE(db->Load(&second_load_entries)); | 574 ASSERT_TRUE(db->Load(&second_load_entries)); |
| 569 EXPECT_EQ(1u, second_load_entries.size()); | 575 EXPECT_EQ(1u, second_load_entries.size()); |
| 570 } | 576 } |
| 571 | 577 |
| 578 TEST(ProtoDatabaseImplLevelDBTest, TestOnMemoryDumpEmitsData) { |
| 579 base::MessageLoop main_loop; |
| 580 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
| 581 std::vector<std::string> load_entries; |
| 582 ASSERT_TRUE(db->Init(base::FilePath())); |
| 583 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); |
| 584 KeyVector remove_keys; |
| 585 ASSERT_TRUE(db->Save(save_entries, remove_keys)); |
| 586 |
| 587 base::trace_event::MemoryDumpArgs dump_args = { |
| 588 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| 589 std::unique_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump( |
| 590 new base::trace_event::ProcessMemoryDump(nullptr, dump_args)); |
| 591 db->OnMemoryDump(dump_args, process_memory_dump.get()); |
| 592 |
| 593 const auto& allocator_dumps = process_memory_dump->allocator_dumps(); |
| 594 const char* system_allocator_pool_name = |
| 595 base::trace_event::MemoryDumpManager::GetInstance() |
| 596 ->system_allocator_pool_name(); |
| 597 size_t expected_dump_count = system_allocator_pool_name ? 2 : 1; |
| 598 EXPECT_EQ(expected_dump_count, allocator_dumps.size()); |
| 599 for (const auto& dump : allocator_dumps) { |
| 600 ASSERT_TRUE(dump.first.find("leveldb/leveldb_proto/") == 0 || |
| 601 dump.first.find(system_allocator_pool_name) == 0); |
| 602 } |
| 603 } |
| 604 |
| 572 } // namespace leveldb_proto | 605 } // namespace leveldb_proto |
| OLD | NEW |