| 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 <set> |
| 6 #include <vector> |
| 7 |
| 5 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 6 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 7 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
| 11 #include "base/test/test_suite.h" | 16 #include "base/test/test_suite.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/leveldatabase/env_chromium.h" | 18 #include "third_party/leveldatabase/env_chromium.h" |
| 14 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 15 | 20 |
| 16 #define FPL FILE_PATH_LITERAL | 21 #define FPL FILE_PATH_LITERAL |
| 17 | 22 |
| 18 using leveldb::DB; | 23 using leveldb::DB; |
| 19 using leveldb::Env; | 24 using leveldb::Env; |
| 20 using leveldb::Options; | 25 using leveldb::Options; |
| 21 using leveldb::ReadOptions; | 26 using leveldb::ReadOptions; |
| 22 using leveldb::Slice; | 27 using leveldb::Slice; |
| 23 using leveldb::Status; | 28 using leveldb::Status; |
| 24 using leveldb::WritableFile; | 29 using leveldb::WritableFile; |
| 25 using leveldb::WriteOptions; | 30 using leveldb::WriteOptions; |
| 26 using leveldb_env::ChromiumEnv; | 31 using leveldb_env::ChromiumEnv; |
| 32 using leveldb_env::DBTracker; |
| 27 using leveldb_env::MethodID; | 33 using leveldb_env::MethodID; |
| 28 | 34 |
| 29 TEST(ErrorEncoding, OnlyAMethod) { | 35 TEST(ErrorEncoding, OnlyAMethod) { |
| 30 const MethodID in_method = leveldb_env::kSequentialFileRead; | 36 const MethodID in_method = leveldb_env::kSequentialFileRead; |
| 31 const Status s = MakeIOError("Somefile.txt", "message", in_method); | 37 const Status s = MakeIOError("Somefile.txt", "message", in_method); |
| 32 MethodID method; | 38 MethodID method; |
| 33 base::File::Error error = base::File::FILE_ERROR_MAX; | 39 base::File::Error error = base::File::FILE_ERROR_MAX; |
| 34 EXPECT_EQ(leveldb_env::METHOD_ONLY, ParseMethodAndError(s, &method, &error)); | 40 EXPECT_EQ(leveldb_env::METHOD_ONLY, ParseMethodAndError(s, &method, &error)); |
| 35 EXPECT_EQ(in_method, method); | 41 EXPECT_EQ(in_method, method); |
| 36 EXPECT_EQ(base::File::FILE_ERROR_MAX, error); | 42 EXPECT_EQ(base::File::FILE_ERROR_MAX, error); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // The disk size equating to the max buffer size | 189 // The disk size equating to the max buffer size |
| 184 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(40 * MB)); | 190 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(40 * MB)); |
| 185 | 191 |
| 186 // Make sure sizes larger than 40MB are clamped to max buffer size. | 192 // Make sure sizes larger than 40MB are clamped to max buffer size. |
| 187 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(80 * MB)); | 193 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(80 * MB)); |
| 188 | 194 |
| 189 // Check for very large disk size (catch overflow). | 195 // Check for very large disk size (catch overflow). |
| 190 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB)); | 196 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB)); |
| 191 } | 197 } |
| 192 | 198 |
| 199 class ChromiumEnvDBTrackerTest : public ::testing::Test { |
| 200 protected: |
| 201 void SetUp() override { |
| 202 testing::Test::SetUp(); |
| 203 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 204 } |
| 205 |
| 206 const base::FilePath& temp_path() const { return scoped_temp_dir_.GetPath(); } |
| 207 |
| 208 using VisitedDBSet = std::set<DBTracker::TrackedDB*>; |
| 209 |
| 210 static VisitedDBSet VisitDatabases() { |
| 211 VisitedDBSet visited; |
| 212 auto db_visitor = [&](DBTracker::TrackedDB* db) { |
| 213 ASSERT_TRUE(visited.insert(db).second) |
| 214 << "Database " << std::hex << db << " visited for the second time"; |
| 215 }; |
| 216 DBTracker::GetInstance()->VisitDatabases(db_visitor); |
| 217 return visited; |
| 218 } |
| 219 |
| 220 using LiveDBSet = std::vector<std::unique_ptr<DBTracker::TrackedDB>>; |
| 221 |
| 222 void AssertEqualSets(const LiveDBSet& live_dbs, |
| 223 const VisitedDBSet& visited_dbs) { |
| 224 for (const auto& live_db : live_dbs) { |
| 225 ASSERT_EQ(1u, visited_dbs.count(live_db.get())) |
| 226 << "Database " << std::hex << live_db.get() << " was not visited"; |
| 227 } |
| 228 ASSERT_EQ(live_dbs.size(), visited_dbs.size()) |
| 229 << "Extra databases were visited"; |
| 230 } |
| 231 |
| 232 private: |
| 233 base::ScopedTempDir scoped_temp_dir_; |
| 234 }; |
| 235 |
| 236 TEST_F(ChromiumEnvDBTrackerTest, OpenDatabase) { |
| 237 struct KeyValue { |
| 238 const char* key; |
| 239 const char* value; |
| 240 }; |
| 241 constexpr KeyValue db_data[] = { |
| 242 {"banana", "yellow"}, {"sky", "blue"}, {"enthusiasm", ""}, |
| 243 }; |
| 244 |
| 245 // Open a new database using DBTracker::Open, write some data. |
| 246 Options options; |
| 247 options.create_if_missing = true; |
| 248 std::string name = temp_path().AsUTF8Unsafe(); |
| 249 DBTracker::TrackedDB* tracked_db; |
| 250 Status status = |
| 251 DBTracker::GetInstance()->OpenDatabase(options, name, &tracked_db); |
| 252 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 253 for (const auto& kv : db_data) { |
| 254 status = tracked_db->Put(WriteOptions(), kv.key, kv.value); |
| 255 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 256 } |
| 257 |
| 258 // Close the database. |
| 259 delete tracked_db; |
| 260 |
| 261 // Open the database again with DB::Open, and check the data. |
| 262 options.create_if_missing = false; |
| 263 leveldb::DB* plain_db = nullptr; |
| 264 status = leveldb::DB::Open(options, name, &plain_db); |
| 265 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 266 for (const auto& kv : db_data) { |
| 267 std::string value; |
| 268 status = plain_db->Get(ReadOptions(), kv.key, &value); |
| 269 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 270 ASSERT_EQ(value, kv.value); |
| 271 } |
| 272 delete plain_db; |
| 273 } |
| 274 |
| 275 TEST_F(ChromiumEnvDBTrackerTest, TrackedDBInfo) { |
| 276 Options options; |
| 277 options.create_if_missing = true; |
| 278 std::string name = temp_path().AsUTF8Unsafe(); |
| 279 DBTracker::TrackedDB* db; |
| 280 Status status = DBTracker::GetInstance()->OpenDatabase(options, name, &db); |
| 281 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 282 |
| 283 // Check that |db| reports info that was used to open it. |
| 284 ASSERT_EQ(name, db->name()); |
| 285 |
| 286 delete db; |
| 287 } |
| 288 |
| 289 TEST_F(ChromiumEnvDBTrackerTest, VisitDatabases) { |
| 290 LiveDBSet live_dbs; |
| 291 |
| 292 // Open several databases. |
| 293 for (const char* tag : {"poets", "movies", "recipes", "novels"}) { |
| 294 Options options; |
| 295 options.create_if_missing = true; |
| 296 std::string name = temp_path().AppendASCII(tag).AsUTF8Unsafe(); |
| 297 DBTracker::TrackedDB* db; |
| 298 Status status = DBTracker::GetInstance()->OpenDatabase(options, name, &db); |
| 299 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 300 live_dbs.emplace_back(db); |
| 301 } |
| 302 |
| 303 // Check that all live databases are visited. |
| 304 AssertEqualSets(live_dbs, VisitDatabases()); |
| 305 |
| 306 // Close couple of a databases. |
| 307 live_dbs.erase(live_dbs.begin()); |
| 308 live_dbs.erase(live_dbs.begin() + 1); |
| 309 |
| 310 // Check that only remaining live databases are visited. |
| 311 AssertEqualSets(live_dbs, VisitDatabases()); |
| 312 } |
| 313 |
| 314 TEST_F(ChromiumEnvDBTrackerTest, OpenDBTracking) { |
| 315 Options options; |
| 316 options.create_if_missing = true; |
| 317 std::unique_ptr<leveldb::DB> db; |
| 318 auto status = leveldb_env::OpenDB(options, temp_path().AsUTF8Unsafe(), &db); |
| 319 ASSERT_TRUE(status.ok()) << status.ToString(); |
| 320 |
| 321 auto visited_dbs = VisitDatabases(); |
| 322 |
| 323 // Databases returned by OpenDB() should be tracked. |
| 324 ASSERT_EQ(1u, visited_dbs.size()); |
| 325 ASSERT_EQ(db.get(), *visited_dbs.begin()); |
| 326 } |
| 327 |
| 193 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } | 328 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } |
| OLD | NEW |