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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 | 433 |
434 base::Thread db_thread("dbthread"); | 434 base::Thread db_thread("dbthread"); |
435 ASSERT_TRUE(db_thread.Start()); | 435 ASSERT_TRUE(db_thread.Start()); |
436 | 436 |
437 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( | 437 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( |
438 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); | 438 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); |
439 | 439 |
440 MockDatabaseCaller caller; | 440 MockDatabaseCaller caller; |
441 EXPECT_CALL(caller, InitCallback(_)); | 441 EXPECT_CALL(caller, InitCallback(_)); |
442 db->Init( | 442 db->Init( |
443 kTestLevelDBClientName, temp_dir.path(), | 443 kTestLevelDBClientName, temp_dir.GetPath(), |
444 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 444 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
445 | 445 |
446 db.reset(); | 446 db.reset(); |
447 | 447 |
448 base::RunLoop run_loop; | 448 base::RunLoop run_loop; |
449 db_thread.task_runner()->PostTaskAndReply( | 449 db_thread.task_runner()->PostTaskAndReply( |
450 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); | 450 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); |
451 run_loop.Run(); | 451 run_loop.Run(); |
452 } | 452 } |
453 | 453 |
454 // This tests that normal usage of the real database does not cause any | 454 // This tests that normal usage of the real database does not cause any |
455 // threading violations. | 455 // threading violations. |
456 TEST(ProtoDatabaseImplThreadingTest, TestDBDestroy) { | 456 TEST(ProtoDatabaseImplThreadingTest, TestDBDestroy) { |
457 base::MessageLoop main_loop; | 457 base::MessageLoop main_loop; |
458 | 458 |
459 ScopedTempDir temp_dir; | 459 ScopedTempDir temp_dir; |
460 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 460 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
461 | 461 |
462 base::Thread db_thread("dbthread"); | 462 base::Thread db_thread("dbthread"); |
463 ASSERT_TRUE(db_thread.Start()); | 463 ASSERT_TRUE(db_thread.Start()); |
464 | 464 |
465 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( | 465 std::unique_ptr<ProtoDatabaseImpl<TestProto>> db( |
466 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); | 466 new ProtoDatabaseImpl<TestProto>(db_thread.task_runner())); |
467 | 467 |
468 MockDatabaseCaller caller; | 468 MockDatabaseCaller caller; |
469 EXPECT_CALL(caller, InitCallback(_)); | 469 EXPECT_CALL(caller, InitCallback(_)); |
470 db->Init( | 470 db->Init( |
471 kTestLevelDBClientName, temp_dir.path(), | 471 kTestLevelDBClientName, temp_dir.GetPath(), |
472 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | 472 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); |
473 | 473 |
474 EXPECT_CALL(caller, DestroyCallback(_)); | 474 EXPECT_CALL(caller, DestroyCallback(_)); |
475 db->Destroy(base::Bind(&MockDatabaseCaller::DestroyCallback, | 475 db->Destroy(base::Bind(&MockDatabaseCaller::DestroyCallback, |
476 base::Unretained(&caller))); | 476 base::Unretained(&caller))); |
477 | 477 |
478 db.reset(); | 478 db.reset(); |
479 | 479 |
480 base::RunLoop run_loop; | 480 base::RunLoop run_loop; |
481 db_thread.task_runner()->PostTaskAndReply( | 481 db_thread.task_runner()->PostTaskAndReply( |
(...skipping 13 matching lines...) Expand all Loading... |
495 KeyValueVector save_entries; | 495 KeyValueVector save_entries; |
496 std::vector<std::string> load_entries; | 496 std::vector<std::string> load_entries; |
497 KeyVector remove_keys; | 497 KeyVector remove_keys; |
498 | 498 |
499 for (const auto& pair : model) { | 499 for (const auto& pair : model) { |
500 save_entries.push_back( | 500 save_entries.push_back( |
501 std::make_pair(pair.second.id(), pair.second.SerializeAsString())); | 501 std::make_pair(pair.second.id(), pair.second.SerializeAsString())); |
502 } | 502 } |
503 | 503 |
504 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 504 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
505 EXPECT_TRUE(db->Init(temp_dir.path())); | 505 EXPECT_TRUE(db->Init(temp_dir.GetPath())); |
506 EXPECT_TRUE(db->Save(save_entries, remove_keys)); | 506 EXPECT_TRUE(db->Save(save_entries, remove_keys)); |
507 | 507 |
508 if (close_after_save) { | 508 if (close_after_save) { |
509 db.reset(new LevelDB(kTestLevelDBClientName)); | 509 db.reset(new LevelDB(kTestLevelDBClientName)); |
510 EXPECT_TRUE(db->Init(temp_dir.path())); | 510 EXPECT_TRUE(db->Init(temp_dir.GetPath())); |
511 } | 511 } |
512 | 512 |
513 EXPECT_TRUE(db->Load(&load_entries)); | 513 EXPECT_TRUE(db->Load(&load_entries)); |
514 | 514 |
515 // Convert the strings back to TestProto. | 515 // Convert the strings back to TestProto. |
516 std::vector<TestProto> loaded_protos; | 516 std::vector<TestProto> loaded_protos; |
517 for (const auto& serialized_entry : load_entries) { | 517 for (const auto& serialized_entry : load_entries) { |
518 TestProto entry; | 518 TestProto entry; |
519 ASSERT_TRUE(entry.ParseFromString(serialized_entry)); | 519 ASSERT_TRUE(entry.ParseFromString(serialized_entry)); |
520 loaded_protos.push_back(entry); | 520 loaded_protos.push_back(entry); |
(...skipping 15 matching lines...) Expand all Loading... |
536 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 536 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
537 | 537 |
538 leveldb::Options options; | 538 leveldb::Options options; |
539 options.create_if_missing = false; | 539 options.create_if_missing = false; |
540 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 540 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
541 | 541 |
542 KeyValueVector save_entries; | 542 KeyValueVector save_entries; |
543 std::vector<std::string> load_entries; | 543 std::vector<std::string> load_entries; |
544 KeyVector remove_keys; | 544 KeyVector remove_keys; |
545 | 545 |
546 EXPECT_FALSE(db->InitWithOptions(temp_dir.path(), options)); | 546 EXPECT_FALSE(db->InitWithOptions(temp_dir.GetPath(), options)); |
547 EXPECT_FALSE(db->Load(&load_entries)); | 547 EXPECT_FALSE(db->Load(&load_entries)); |
548 EXPECT_FALSE(db->Save(save_entries, remove_keys)); | 548 EXPECT_FALSE(db->Save(save_entries, remove_keys)); |
549 } | 549 } |
550 | 550 |
551 TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { | 551 TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { |
552 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); | 552 std::unique_ptr<LevelDB> db(new LevelDB(kTestLevelDBClientName)); |
553 | 553 |
554 std::vector<std::string> load_entries; | 554 std::vector<std::string> load_entries; |
555 | 555 |
556 ASSERT_TRUE(db->Init(base::FilePath())); | 556 ASSERT_TRUE(db->Init(base::FilePath())); |
557 | 557 |
558 ASSERT_TRUE(db->Load(&load_entries)); | 558 ASSERT_TRUE(db->Load(&load_entries)); |
559 EXPECT_EQ(0u, load_entries.size()); | 559 EXPECT_EQ(0u, load_entries.size()); |
560 | 560 |
561 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); | 561 KeyValueVector save_entries(1, std::make_pair("foo", "bar")); |
562 KeyVector remove_keys; | 562 KeyVector remove_keys; |
563 | 563 |
564 ASSERT_TRUE(db->Save(save_entries, remove_keys)); | 564 ASSERT_TRUE(db->Save(save_entries, remove_keys)); |
565 | 565 |
566 std::vector<std::string> second_load_entries; | 566 std::vector<std::string> second_load_entries; |
567 | 567 |
568 ASSERT_TRUE(db->Load(&second_load_entries)); | 568 ASSERT_TRUE(db->Load(&second_load_entries)); |
569 EXPECT_EQ(1u, second_load_entries.size()); | 569 EXPECT_EQ(1u, second_load_entries.size()); |
570 } | 570 } |
571 | 571 |
572 } // namespace leveldb_proto | 572 } // namespace leveldb_proto |
OLD | NEW |