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

Side by Side Diff: third_party/leveldatabase/env_chromium_unittest.cc

Issue 2855953002: leveldb: Add DBTracker for exposing databases to Chrome's memory-infra. (Closed)
Patch Set: OpenDB and friends Created 3 years, 5 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 unified diff | Download patch
OLDNEW
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
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 VisitedDBs = std::set<DBTracker::TrackedDB*>;
209
210 static VisitedDBs VisitDatabases() {
211 VisitedDBs 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 private:
221 base::ScopedTempDir scoped_temp_dir_;
222 };
223
224 TEST_F(ChromiumEnvDBTrackerTest, OpenDatabase) {
225 struct KeyValue {
226 const char* key;
227 const char* value;
228 };
229 constexpr KeyValue db_data[] = {
230 {"banana", "yellow"}, {"sky", "blue"}, {"enthusiasm", ""},
231 };
232
233 // Open a new database using DBTracker::Open, write some data.
234 Options options;
235 options.create_if_missing = true;
236 std::string name = temp_path().AsUTF8Unsafe();
237 DBTracker::TrackedDB* tracked_db;
238 Status status =
239 DBTracker::GetInstance()->OpenDatabase(options, name, &tracked_db);
240 ASSERT_TRUE(status.ok()) << status.ToString();
241 for (const auto& kv : db_data) {
242 status = tracked_db->Put(WriteOptions(), kv.key, kv.value);
243 ASSERT_TRUE(status.ok()) << status.ToString();
244 }
245
246 // Close the database.
247 delete tracked_db;
248
249 // Open the database again with DB::Open, and check the data.
250 options.create_if_missing = false;
251 leveldb::DB* plain_db = nullptr;
252 status = leveldb::DB::Open(options, name, &plain_db);
253 ASSERT_TRUE(status.ok()) << status.ToString();
254 for (const auto& kv : db_data) {
255 std::string value;
256 status = plain_db->Get(ReadOptions(), kv.key, &value);
257 ASSERT_TRUE(status.ok()) << status.ToString();
258 ASSERT_EQ(value, kv.value);
259 }
260 delete plain_db;
261 }
262
263 TEST_F(ChromiumEnvDBTrackerTest, TrackedDBInfo) {
264 Options options;
265 options.create_if_missing = true;
266 std::string name = temp_path().AsUTF8Unsafe();
267 DBTracker::TrackedDB* db;
268 Status status = DBTracker::GetInstance()->OpenDatabase(options, name, &db);
269 ASSERT_TRUE(status.ok()) << status.ToString();
270
271 // Check that |db| reports info that was used to open it.
272 ASSERT_EQ(name, db->name());
273
274 delete db;
275 }
276
277 TEST_F(ChromiumEnvDBTrackerTest, VisitDatabases) {
278 std::vector<std::unique_ptr<DBTracker::TrackedDB>> live_dbs;
279
280 // Open several databases.
281 for (const char* tag : {"poets", "movies", "recipes", "novels"}) {
282 Options options;
283 options.create_if_missing = true;
284 std::string name = temp_path().Append(tag).AsUTF8Unsafe();
285 DBTracker::TrackedDB* db;
286 Status status = DBTracker::GetInstance()->OpenDatabase(options, name, &db);
287 ASSERT_TRUE(status.ok()) << status.ToString();
288 live_dbs.emplace_back(db);
289 }
290
291 // Checks that |visited_dbs| contains only live databases, and nothing else.
292 auto assert_databases_live = [&](const VisitedDBs& visited_dbs) {
293 for (const auto& live_db : live_dbs) {
294 ASSERT_EQ(1u, visited_dbs.count(live_db.get()))
295 << "Database " << std::hex << live_db.get() << " was not visited";
296 }
297 ASSERT_EQ(live_dbs.size(), visited_dbs.size())
298 << "Extra databases were visited";
299 };
300
301 // Check that all live databases are visited.
302 assert_databases_live(VisitDatabases());
303
304 // Close couple of a databases.
305 live_dbs.erase(live_dbs.begin());
306 live_dbs.erase(live_dbs.begin() + 1);
307
308 // Check that only remaining live databases are visited.
309 assert_databases_live(VisitDatabases());
310 }
311
312 TEST_F(ChromiumEnvDBTrackerTest, OpenDBTracking) {
313 Options options;
314 options.create_if_missing = true;
315 std::unique_ptr<leveldb::DB> db;
316 auto status = leveldb_env::OpenDB(options, temp_path().AsUTF8Unsafe(), &db);
317 ASSERT_TRUE(status.ok()) << status.ToString();
318
319 auto visited_dbs = VisitDatabases();
320
321 // Databases returned by OpenDB() should be tracked.
322 ASSERT_EQ(1u, visited_dbs.size());
323 ASSERT_EQ(db.get(), *visited_dbs.begin());
324 }
325
193 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } 326 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); }
OLDNEW
« 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