| Index: sync/internal_api/model_type_store_backend_unittest.cc
|
| diff --git a/sync/internal_api/model_type_store_backend_unittest.cc b/sync/internal_api/model_type_store_backend_unittest.cc
|
| index fe068f0e2d2ce51b9107dd604de655a4c5c6f0e8..44944d57739a643ff75ad76e60db37429e5e18a4 100644
|
| --- a/sync/internal_api/model_type_store_backend_unittest.cc
|
| +++ b/sync/internal_api/model_type_store_backend_unittest.cc
|
| @@ -9,6 +9,7 @@
|
| #include "base/bind.h"
|
| #include "base/message_loop/message_loop.h"
|
| #include "base/run_loop.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "third_party/leveldatabase/src/include/leveldb/env.h"
|
| #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
|
| @@ -21,26 +22,46 @@ class ModelTypeStoreBackendTest : public testing::Test {
|
| in_memory_env_ = ModelTypeStoreBackend::CreateInMemoryEnv();
|
| }
|
|
|
| - void TearDown() override { in_memory_env_.reset(); }
|
| + void TearDown() override {
|
| + // ModelTypeStoreBackend::env will be required when
|
| + // ModelTypeStoreBackend::db_ is deleting. so delete ModelTypeStoreBackend
|
| + // first, then env.
|
| + ModelTypeStoreBackend::backend_map_.Get().erase(path_);
|
| + in_memory_env_.reset();
|
| + }
|
|
|
| - std::unique_ptr<ModelTypeStoreBackend> CreateBackend() {
|
| - std::unique_ptr<ModelTypeStoreBackend> backend(new ModelTypeStoreBackend());
|
| - std::string path;
|
| - in_memory_env_->GetTestDirectory(&path);
|
| - path += "/test_db";
|
| - ModelTypeStore::Result result = backend->Init(path, in_memory_env_.get());
|
| - EXPECT_EQ(ModelTypeStore::Result::SUCCESS, result);
|
| + scoped_refptr<ModelTypeStoreBackend> CreateBackend() {
|
| + in_memory_env_->GetTestDirectory(&path_);
|
| + path_ += "/test_db";
|
| + // In-memory store backend works on the same thread as test.
|
| + scoped_refptr<base::SequencedTaskRunner> task_runner =
|
| + base::ThreadTaskRunnerHandle::Get();
|
| + scoped_refptr<ModelTypeStoreBackend> backend =
|
| + ModelTypeStoreBackend::GetOrCreateBackend(path_, in_memory_env_.get(),
|
| + task_runner);
|
| + PumpLoop();
|
| + EXPECT_TRUE(backend.get());
|
| + EXPECT_EQ(ModelTypeStore::Result::SUCCESS, backend->InitResult());
|
| return backend;
|
| }
|
|
|
| + void PumpLoop() {
|
| + base::RunLoop run_loop;
|
| + run_loop.RunUntilIdle();
|
| + }
|
| +
|
| protected:
|
| std::unique_ptr<leveldb::Env> in_memory_env_;
|
| +
|
| + std::string path_;
|
| +
|
| + base::MessageLoop sync_loop_;
|
| };
|
|
|
| // Test that after record is written to backend it can be read back even after
|
| // backend is destroyed and recreated in the same environment.
|
| TEST_F(ModelTypeStoreBackendTest, WriteThenRead) {
|
| - std::unique_ptr<ModelTypeStoreBackend> backend = CreateBackend();
|
| + scoped_refptr<ModelTypeStoreBackend> backend = CreateBackend();
|
|
|
| // Write record.
|
| std::unique_ptr<leveldb::WriteBatch> write_batch(new leveldb::WriteBatch());
|
| @@ -69,7 +90,7 @@ TEST_F(ModelTypeStoreBackendTest, WriteThenRead) {
|
|
|
| // Test that ReadAllRecordsWithPrefix correclty filters records by prefix.
|
| TEST_F(ModelTypeStoreBackendTest, ReadAllRecordsWithPrefix) {
|
| - std::unique_ptr<ModelTypeStoreBackend> backend = CreateBackend();
|
| + scoped_refptr<ModelTypeStoreBackend> backend = CreateBackend();
|
|
|
| std::unique_ptr<leveldb::WriteBatch> write_batch(new leveldb::WriteBatch());
|
| write_batch->Put("prefix1:id1", "data1");
|
| @@ -89,7 +110,7 @@ TEST_F(ModelTypeStoreBackendTest, ReadAllRecordsWithPrefix) {
|
| // Test that deleted records are correctly marked as milling in results of
|
| // ReadRecordsWithPrefix.
|
| TEST_F(ModelTypeStoreBackendTest, ReadDeletedRecord) {
|
| - std::unique_ptr<ModelTypeStoreBackend> backend = CreateBackend();
|
| + scoped_refptr<ModelTypeStoreBackend> backend = CreateBackend();
|
|
|
| // Create records, ensure they are returned by ReadRecordsWithPrefix.
|
| std::unique_ptr<leveldb::WriteBatch> write_batch(new leveldb::WriteBatch());
|
| @@ -128,4 +149,24 @@ TEST_F(ModelTypeStoreBackendTest, ReadDeletedRecord) {
|
| ASSERT_EQ("id2", missing_id_list[0]);
|
| }
|
|
|
| +// Test that Only one backend got create when we ask two backend, and after
|
| +// disconnect two backend, the backend will be deleted.
|
| +TEST_F(ModelTypeStoreBackendTest, TwoBeckendTest) {
|
| + scoped_refptr<ModelTypeStoreBackend> backend = CreateBackend();
|
| + scoped_refptr<ModelTypeStoreBackend> backend_second = CreateBackend();
|
| + std::string path = backend->path_;
|
| + ASSERT_EQ(backend.get(), backend_second.get());
|
| + ASSERT_FALSE(backend_second->HasOneRef());
|
| +
|
| + ModelTypeStoreBackend::Disconnect(backend);
|
| + ASSERT_FALSE(backend.get());
|
| + ASSERT_TRUE(backend_second.get());
|
| + ASSERT_FALSE(backend_second->HasOneRef());
|
| +
|
| + ModelTypeStoreBackend::Disconnect(backend_second);
|
| + ASSERT_FALSE(backend_second.get());
|
| + ASSERT_EQ(ModelTypeStoreBackend::backend_map_.Get().end(),
|
| + ModelTypeStoreBackend::backend_map_.Get().find(path));
|
| +}
|
| +
|
| } // namespace syncer_v2
|
|
|