Chromium Code Reviews| 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..78cc87013d9457d350546a38b154bd4e92f8812b 100644 |
| --- a/third_party/leveldatabase/env_chromium_unittest.cc |
| +++ b/third_party/leveldatabase/env_chromium_unittest.cc |
| @@ -2,15 +2,16 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "third_party/leveldatabase/env_chromium.h" |
| #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/test/test_suite.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| -#include "third_party/leveldatabase/env_chromium.h" |
| #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| #define FPL FILE_PATH_LITERAL |
| @@ -190,4 +191,98 @@ TEST(ChromiumEnv, TestWriteBufferSize) { |
| EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB)); |
| } |
| +TEST(ChromiumEnv, DBRegistryOpen) { |
|
pwnall
2017/06/22 02:50:22
Thank you for tests!
FYI, leveldb tests don't run
DmitrySkiba
2017/06/26 20:45:19
Acknowledged.
|
| + 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 DBRegistry::Open, write some data |
|
pwnall
2017/06/22 02:50:22
Periods at the end of sentences?
DmitrySkiba
2017/06/26 20:45:19
Done.
|
| + Options options; |
| + options.create_if_missing = true; |
| + DB* db; |
| + Status status = |
| + leveldb_env::DBRegistry::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, DBRegistryVisitDatabases) { |
| + base::ScopedTempDir scoped_temp_dir; |
| + ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| + |
| + struct NamedDB { |
| + const char* tag; |
| + std::string name; |
| + std::unique_ptr<DB> db; |
| + }; |
| + NamedDB named_dbs[] = { |
| + {"poets"}, {"movies"}, {"recipes"}, {"misconceptions"}, |
| + }; |
| + |
| + // Open databases |
| + for (auto& named_db : named_dbs) { |
| + Options options; |
| + options.create_if_missing = true; |
| + std::string name = |
| + scoped_temp_dir.GetPath().Append(named_db.tag).AsUTF8Unsafe(); |
| + DB* db; |
| + Status status = leveldb_env::DBRegistry::Open(options, name, &db); |
| + ASSERT_TRUE(status.ok()) << status.ToString(); |
| + named_db.name = name; |
| + named_db.db.reset(db); |
| + } |
| + |
| + size_t visited_db_count; |
|
pwnall
2017/06/22 02:50:22
Can you initialize this to 0, so a reader doesn't
DmitrySkiba
2017/06/26 20:45:19
Acknowledged.
|
| + auto db_visitor = [&](leveldb_env::DBRegistry::DBWrapper* db) { |
| + for (const auto& named_db : named_dbs) { |
| + if (db->name() == named_db.name) { |
| + ASSERT_EQ(named_db.db.get(), db); |
| + visited_db_count += 1; |
|
pwnall
2017/06/22 02:50:22
This algorithm doesn't catch the case where the vi
DmitrySkiba
2017/06/26 20:45:19
Good catch. Redid the test. The motivation behind
pwnall
2017/06/26 23:37:13
Thanks for explaining! It makes sense to keep it,
|
| + return; |
| + } |
| + } |
| + ASSERT_TRUE(false) << "Visited unknown database '" << db->name() << "'"; |
| + }; |
| + |
| + // Check that VisitDatabases() visits all opened databases |
| + visited_db_count = 0; |
| + leveldb_env::DBRegistry::GetInstance()->VisitDatabases(db_visitor); |
| + ASSERT_EQ(visited_db_count, arraysize(named_dbs)); |
| + |
| + // Close couple databases |
|
pwnall
2017/06/22 02:50:22
a couple of?
DmitrySkiba
2017/06/26 20:45:19
Done.
pwnall
2017/06/26 23:37:13
Almost? You still need the "a".
|
| + named_dbs[0].db.reset(); |
| + named_dbs[2].db.reset(); |
| + |
| + // Check that VisitDatabases() visits only the remaining ones |
| + visited_db_count = 0; |
| + leveldb_env::DBRegistry::GetInstance()->VisitDatabases(db_visitor); |
| + ASSERT_EQ(visited_db_count, arraysize(named_dbs) - 2); |
| +} |
| + |
| int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } |