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

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: Address comments 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 <map>
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;
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 // The disk size equating to the max buffer size 188 // The disk size equating to the max buffer size
184 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(40 * MB)); 189 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(40 * MB));
185 190
186 // Make sure sizes larger than 40MB are clamped to max buffer size. 191 // Make sure sizes larger than 40MB are clamped to max buffer size.
187 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(80 * MB)); 192 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(80 * MB));
188 193
189 // Check for very large disk size (catch overflow). 194 // Check for very large disk size (catch overflow).
190 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB)); 195 EXPECT_EQ(size_t(4 * MB), leveldb_env::WriteBufferSize(100 * MB * MB));
191 } 196 }
192 197
198 TEST(ChromiumEnv, DBTrackerOpen) {
199 base::ScopedTempDir scoped_temp_dir;
200 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
201 base::FilePath dir = scoped_temp_dir.GetPath();
202
203 struct KeyValue {
204 const char* key;
205 const char* value;
206 };
207 constexpr KeyValue db_data[] = {
208 {"banana", "yellow"}, {"sky", "blue"}, {"enthusiasm", ""},
209 };
210
211 // Open a new database using DBTracker::Open, write some data.
212 Options options;
213 options.create_if_missing = true;
214 DB* db;
215 Status status =
216 leveldb_env::DBTracker::Open(options, dir.AsUTF8Unsafe(), &db);
217 ASSERT_TRUE(status.ok()) << status.ToString();
218 for (const auto& kv : db_data) {
219 status = db->Put(WriteOptions(), kv.key, kv.value);
220 ASSERT_TRUE(status.ok()) << status.ToString();
221 }
222
223 // Close the database.
224 delete db;
225
226 // Open the database again with DB::Open, and check the data.
227 options.create_if_missing = false;
228 status = leveldb::DB::Open(options, dir.AsUTF8Unsafe(), &db);
229 ASSERT_TRUE(status.ok()) << status.ToString();
230 for (const auto& kv : db_data) {
231 std::string value;
232 status = db->Get(ReadOptions(), kv.key, &value);
233 ASSERT_TRUE(status.ok()) << status.ToString();
234 ASSERT_EQ(value, kv.value);
235 }
236 delete db;
237 }
238
239 TEST(ChromiumEnv, DBTrackerVisitDatabases) {
240 base::ScopedTempDir scoped_temp_dir;
241 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
242
243 // Information available to the database visitor.
244 struct DBInfo {
245 std::string name;
246 };
247
248 struct LiveDB {
249 std::unique_ptr<DB> db;
250 DBInfo info;
251 };
252
253 std::vector<LiveDB> live_dbs;
254
255 // Open several databases.
256 for (const char* tag : {"poets", "movies", "recipes", "novels"}) {
257 Options options;
258 options.create_if_missing = true;
259 std::string name = scoped_temp_dir.GetPath().Append(tag).AsUTF8Unsafe();
260 DB* db;
261 Status status = leveldb_env::DBTracker::Open(options, name, &db);
262 ASSERT_TRUE(status.ok()) << status.ToString();
263 LiveDB& live_db = *live_dbs.emplace(live_dbs.end());
264 live_db.db.reset(db);
265 live_db.info.name = name;
266 }
267
268 using VisitedDBs = std::map<DB*, DBInfo>;
269
270 // Visits databases and returns them.
271 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
272 VisitedDBs visited;
273 auto db_visitor = [&](leveldb_env::DBTracker::DBWrapper* db) {
274 DBInfo info = {db->name()};
275 ASSERT_TRUE(visited.insert({db, info}).second)
276 << "Database " << std::hex << db << " visited for the second time";
277 };
278 leveldb_env::DBTracker::GetInstance()->VisitDatabases(db_visitor);
279 return visited;
280 };
281
282 // Checks that |visited_dbs| contains only live databases, and nothing else.
283 auto assert_databases_live = [&](const VisitedDBs& visited_dbs) {
284 ASSERT_EQ(live_dbs.size(), visited_dbs.size());
285 for (const auto& live_db : live_dbs) {
286 auto visited_db = visited_dbs.find(live_db.db.get());
287 ASSERT_NE(visited_dbs.end(), visited_db);
288 ASSERT_EQ(live_db.info.name, visited_db->second.name);
289 }
290 };
291
292 // Check that all live databases are visited.
293 assert_databases_live(visit_databases());
294
295 // Close couple of databases.
296 live_dbs.erase(live_dbs.begin());
297 live_dbs.erase(live_dbs.begin() + 1);
298
299 // Check that only remaining live databases are visited.
300 assert_databases_live(visit_databases());
301 }
302
193 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } 303 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