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

Unified Diff: third_party/leveldatabase/env_chromium_unittest.cc

Issue 2855953002: leveldb: Add DBTracker for exposing databases to Chrome's memory-infra. (Closed)
Patch Set: Address comments Created 3 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: third_party/leveldatabase/env_chromium_unittest.cc
diff --git a/third_party/leveldatabase/env_chromium_unittest.cc b/third_party/leveldatabase/env_chromium_unittest.cc
index 57bca249af8cdefad5c720aa7b16703dfc18bacc..f0abaf846fb78b351e78578301b5e5f6d1848643 100644
--- a/third_party/leveldatabase/env_chromium_unittest.cc
+++ b/third_party/leveldatabase/env_chromium_unittest.cc
@@ -2,12 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <map>
+#include <vector>
+
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/lazy_instance.h"
+#include "base/macros.h"
+#include "base/memory/ptr_util.h"
#include "base/test/test_suite.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/env_chromium.h"
@@ -190,4 +195,109 @@ TEST(ChromiumEnv, TestWriteBufferSize) {
EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB));
}
+TEST(ChromiumEnv, DBTrackerOpen) {
+ base::ScopedTempDir scoped_temp_dir;
+ ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
+ base::FilePath dir = scoped_temp_dir.GetPath();
+
+ struct KeyValue {
+ const char* key;
+ const char* value;
+ };
+ constexpr KeyValue db_data[] = {
+ {"banana", "yellow"}, {"sky", "blue"}, {"enthusiasm", ""},
+ };
+
+ // Open a new database using DBTracker::Open, write some data.
+ Options options;
+ options.create_if_missing = true;
+ DB* db;
+ Status status =
+ leveldb_env::DBTracker::Open(options, dir.AsUTF8Unsafe(), &db);
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ for (const auto& kv : db_data) {
+ status = db->Put(WriteOptions(), kv.key, kv.value);
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ }
+
+ // Close the database.
+ delete db;
+
+ // Open the database again with DB::Open, and check the data.
+ options.create_if_missing = false;
+ status = leveldb::DB::Open(options, dir.AsUTF8Unsafe(), &db);
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ for (const auto& kv : db_data) {
+ std::string value;
+ status = db->Get(ReadOptions(), kv.key, &value);
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ ASSERT_EQ(value, kv.value);
+ }
+ delete db;
+}
+
+TEST(ChromiumEnv, DBTrackerVisitDatabases) {
+ base::ScopedTempDir scoped_temp_dir;
+ ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
+
+ // Information available to the database visitor.
+ struct DBInfo {
+ std::string name;
+ };
+
+ struct LiveDB {
+ std::unique_ptr<DB> db;
+ DBInfo info;
+ };
+
+ std::vector<LiveDB> live_dbs;
+
+ // Open several databases.
+ for (const char* tag : {"poets", "movies", "recipes", "novels"}) {
+ Options options;
+ options.create_if_missing = true;
+ std::string name = scoped_temp_dir.GetPath().Append(tag).AsUTF8Unsafe();
+ DB* db;
+ Status status = leveldb_env::DBTracker::Open(options, name, &db);
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ LiveDB& live_db = *live_dbs.emplace(live_dbs.end());
+ live_db.db.reset(db);
+ live_db.info.name = name;
+ }
+
+ using VisitedDBs = std::map<DB*, DBInfo>;
+
+ // Visits databases and returns them.
+ auto visit_databases = []() -> VisitedDBs {
pwnall 2017/06/26 23:37:14 I'd prefer that you use a ChromiumEnvDBTrackerTest
DmitrySkiba 2017/06/28 20:35:29 Partially done. assert_databases_live() is still h
pwnall 2017/06/28 22:52:55 I think this'd be more readable if the method woul
+ VisitedDBs visited;
+ auto db_visitor = [&](leveldb_env::DBTracker::DBWrapper* db) {
+ DBInfo info = {db->name()};
+ ASSERT_TRUE(visited.insert({db, info}).second)
+ << "Database " << std::hex << db << " visited for the second time";
+ };
+ leveldb_env::DBTracker::GetInstance()->VisitDatabases(db_visitor);
+ return visited;
+ };
+
+ // Checks that |visited_dbs| contains only live databases, and nothing else.
+ auto assert_databases_live = [&](const VisitedDBs& visited_dbs) {
+ ASSERT_EQ(live_dbs.size(), visited_dbs.size());
+ for (const auto& live_db : live_dbs) {
+ auto visited_db = visited_dbs.find(live_db.db.get());
+ ASSERT_NE(visited_dbs.end(), visited_db);
+ ASSERT_EQ(live_db.info.name, visited_db->second.name);
+ }
+ };
+
+ // Check that all live databases are visited.
+ assert_databases_live(visit_databases());
+
+ // Close couple of databases.
+ live_dbs.erase(live_dbs.begin());
+ live_dbs.erase(live_dbs.begin() + 1);
+
+ // Check that only remaining live databases are visited.
+ assert_databases_live(visit_databases());
+}
+
int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); }
« third_party/leveldatabase/env_chromium.h ('K') | « third_party/leveldatabase/env_chromium.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698