Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Unified Diff: sync/internal_api/model_type_store_backend_unittest.cc

Issue 2077713002: [USS] Store supports hosting multiple datatypes per database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: can create multiple backend base on path Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698