Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "third_party/leveldatabase/env_chromium.h" | |
| 5 #include "base/files/file.h" | 6 #include "base/files/file.h" |
| 6 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/macros.h" | |
| 11 #include "base/test/test_suite.h" | 13 #include "base/test/test_suite.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/leveldatabase/env_chromium.h" | |
| 14 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 15 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 15 | 16 |
| 16 #define FPL FILE_PATH_LITERAL | 17 #define FPL FILE_PATH_LITERAL |
| 17 | 18 |
| 18 using leveldb::DB; | 19 using leveldb::DB; |
| 19 using leveldb::Env; | 20 using leveldb::Env; |
| 20 using leveldb::Options; | 21 using leveldb::Options; |
| 21 using leveldb::ReadOptions; | 22 using leveldb::ReadOptions; |
| 22 using leveldb::Slice; | 23 using leveldb::Slice; |
| 23 using leveldb::Status; | 24 using leveldb::Status; |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 // The disk size equating to the max buffer size | 184 // The disk size equating to the max buffer size |
| 184 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(40 * MB)); | 185 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(40 * MB)); |
| 185 | 186 |
| 186 // Make sure sizes larger than 40MB are clamped to max buffer size. | 187 // Make sure sizes larger than 40MB are clamped to max buffer size. |
| 187 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(80 * MB)); | 188 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(80 * MB)); |
| 188 | 189 |
| 189 // Check for very large disk size (catch overflow). | 190 // Check for very large disk size (catch overflow). |
| 190 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB)); | 191 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB)); |
| 191 } | 192 } |
| 192 | 193 |
| 194 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.
| |
| 195 base::ScopedTempDir scoped_temp_dir; | |
| 196 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | |
| 197 base::FilePath dir = scoped_temp_dir.GetPath(); | |
| 198 | |
| 199 struct KeyValue { | |
| 200 const char* key; | |
| 201 const char* value; | |
| 202 }; | |
| 203 constexpr KeyValue db_data[] = { | |
| 204 {"banana", "yellow"}, {"sky", "blue"}, {"enthusiasm", ""}, | |
| 205 }; | |
| 206 | |
| 207 // 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.
| |
| 208 Options options; | |
| 209 options.create_if_missing = true; | |
| 210 DB* db; | |
| 211 Status status = | |
| 212 leveldb_env::DBRegistry::Open(options, dir.AsUTF8Unsafe(), &db); | |
| 213 ASSERT_TRUE(status.ok()) << status.ToString(); | |
| 214 for (const auto& kv : db_data) { | |
| 215 status = db->Put(WriteOptions(), kv.key, kv.value); | |
| 216 ASSERT_TRUE(status.ok()) << status.ToString(); | |
| 217 } | |
| 218 | |
| 219 // Close the database | |
| 220 delete db; | |
| 221 | |
| 222 // Open the database again with DB::Open, and check the data | |
| 223 options.create_if_missing = false; | |
| 224 status = leveldb::DB::Open(options, dir.AsUTF8Unsafe(), &db); | |
| 225 ASSERT_TRUE(status.ok()) << status.ToString(); | |
| 226 for (const auto& kv : db_data) { | |
| 227 std::string value; | |
| 228 status = db->Get(ReadOptions(), kv.key, &value); | |
| 229 ASSERT_TRUE(status.ok()) << status.ToString(); | |
| 230 ASSERT_EQ(value, kv.value); | |
| 231 } | |
| 232 delete db; | |
| 233 } | |
| 234 | |
| 235 TEST(ChromiumEnv, DBRegistryVisitDatabases) { | |
| 236 base::ScopedTempDir scoped_temp_dir; | |
| 237 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | |
| 238 | |
| 239 struct NamedDB { | |
| 240 const char* tag; | |
| 241 std::string name; | |
| 242 std::unique_ptr<DB> db; | |
| 243 }; | |
| 244 NamedDB named_dbs[] = { | |
| 245 {"poets"}, {"movies"}, {"recipes"}, {"misconceptions"}, | |
| 246 }; | |
| 247 | |
| 248 // Open databases | |
| 249 for (auto& named_db : named_dbs) { | |
| 250 Options options; | |
| 251 options.create_if_missing = true; | |
| 252 std::string name = | |
| 253 scoped_temp_dir.GetPath().Append(named_db.tag).AsUTF8Unsafe(); | |
| 254 DB* db; | |
| 255 Status status = leveldb_env::DBRegistry::Open(options, name, &db); | |
| 256 ASSERT_TRUE(status.ok()) << status.ToString(); | |
| 257 named_db.name = name; | |
| 258 named_db.db.reset(db); | |
| 259 } | |
| 260 | |
| 261 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.
| |
| 262 auto db_visitor = [&](leveldb_env::DBRegistry::DBWrapper* db) { | |
| 263 for (const auto& named_db : named_dbs) { | |
| 264 if (db->name() == named_db.name) { | |
| 265 ASSERT_EQ(named_db.db.get(), db); | |
| 266 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,
| |
| 267 return; | |
| 268 } | |
| 269 } | |
| 270 ASSERT_TRUE(false) << "Visited unknown database '" << db->name() << "'"; | |
| 271 }; | |
| 272 | |
| 273 // Check that VisitDatabases() visits all opened databases | |
| 274 visited_db_count = 0; | |
| 275 leveldb_env::DBRegistry::GetInstance()->VisitDatabases(db_visitor); | |
| 276 ASSERT_EQ(visited_db_count, arraysize(named_dbs)); | |
| 277 | |
| 278 // 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".
| |
| 279 named_dbs[0].db.reset(); | |
| 280 named_dbs[2].db.reset(); | |
| 281 | |
| 282 // Check that VisitDatabases() visits only the remaining ones | |
| 283 visited_db_count = 0; | |
| 284 leveldb_env::DBRegistry::GetInstance()->VisitDatabases(db_visitor); | |
| 285 ASSERT_EQ(visited_db_count, arraysize(named_dbs) - 2); | |
| 286 } | |
| 287 | |
| 193 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } | 288 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } |
| OLD | NEW |