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

Side by Side Diff: content/browser/leveldb_wrapper_impl_unittest.cc

Issue 2725723002: Purge new localstorage memory cache when under memory pressure. (Closed)
Patch Set: Created 3 years, 9 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
« no previous file with comments | « content/browser/leveldb_wrapper_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/leveldb_wrapper_impl.h" 5 #include "content/browser/leveldb_wrapper_impl.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "components/leveldb/public/cpp/util.h" 9 #include "components/leveldb/public/cpp/util.h"
10 #include "content/public/test/test_browser_thread_bundle.h" 10 #include "content/public/test/test_browser_thread_bundle.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 base::Closure m_callback; 49 base::Closure m_callback;
50 }; 50 };
51 51
52 class MockDelegate : public LevelDBWrapperImpl::Delegate { 52 class MockDelegate : public LevelDBWrapperImpl::Delegate {
53 public: 53 public:
54 void OnNoBindings() override {} 54 void OnNoBindings() override {}
55 std::vector<leveldb::mojom::BatchedOperationPtr> PrepareToCommit() override { 55 std::vector<leveldb::mojom::BatchedOperationPtr> PrepareToCommit() override {
56 return std::vector<leveldb::mojom::BatchedOperationPtr>(); 56 return std::vector<leveldb::mojom::BatchedOperationPtr>();
57 } 57 }
58 void DidCommit(leveldb::mojom::DatabaseError error) override {} 58 void DidCommit(leveldb::mojom::DatabaseError error) override {}
59 void OnMapLoaded(leveldb::mojom::DatabaseError error) override {
60 map_load_count_++;
61 }
62
63 int map_load_count() const { return map_load_count_; }
64
65 private:
66 int map_load_count_ = 0;
59 }; 67 };
60 68
61 void GetCallback(const base::Closure& callback, 69 void GetCallback(const base::Closure& callback,
62 bool* success_out, 70 bool* success_out,
63 std::vector<uint8_t>* value_out, 71 std::vector<uint8_t>* value_out,
64 bool success, 72 bool success,
65 const std::vector<uint8_t>& value) { 73 const std::vector<uint8_t>& value) {
66 *success_out = success; 74 *success_out = success;
67 *value_out = value; 75 *value_out = value;
68 callback.Run(); 76 callback.Run();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 133
126 std::string get_mock_data(const std::string& key) { 134 std::string get_mock_data(const std::string& key) {
127 return has_mock_data(key) 135 return has_mock_data(key)
128 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)]) 136 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)])
129 : ""; 137 : "";
130 } 138 }
131 139
132 void clear_mock_data() { mock_data_.clear(); } 140 void clear_mock_data() { mock_data_.clear(); }
133 141
134 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); } 142 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); }
143 LevelDBWrapperImpl* wrapper_impl() { return &level_db_wrapper_; }
135 144
136 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) { 145 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) {
137 base::RunLoop run_loop; 146 base::RunLoop run_loop;
138 bool success = false; 147 bool success = false;
139 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(), 148 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(),
140 &success, result)); 149 &success, result));
141 run_loop.Run(); 150 run_loop.Run();
142 return success; 151 return success;
143 } 152 }
144 153
(...skipping 26 matching lines...) Expand all
171 kTestSource, 180 kTestSource,
172 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success)); 181 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success));
173 run_loop.Run(); 182 run_loop.Run();
174 return success; 183 return success;
175 } 184 }
176 185
177 void CommitChanges() { level_db_wrapper_.ScheduleImmediateCommit(); } 186 void CommitChanges() { level_db_wrapper_.ScheduleImmediateCommit(); }
178 187
179 const std::vector<Observation>& observations() { return observations_; } 188 const std::vector<Observation>& observations() { return observations_; }
180 189
190 MockDelegate* delegate() { return &delegate_; }
191
181 private: 192 private:
182 // LevelDBObserver: 193 // LevelDBObserver:
183 void KeyAdded(const std::vector<uint8_t>& key, 194 void KeyAdded(const std::vector<uint8_t>& key,
184 const std::vector<uint8_t>& value, 195 const std::vector<uint8_t>& value,
185 const std::string& source) override { 196 const std::string& source) override {
186 observations_.push_back({Observation::kAdd, Uint8VectorToStdString(key), "", 197 observations_.push_back({Observation::kAdd, Uint8VectorToStdString(key), "",
187 Uint8VectorToStdString(value), source}); 198 Uint8VectorToStdString(value), source});
188 } 199 }
189 void KeyChanged(const std::vector<uint8_t>& key, 200 void KeyChanged(const std::vector<uint8_t>& key,
190 const std::vector<uint8_t>& new_value, 201 const std::vector<uint8_t>& new_value,
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 484
474 // Reducing size should also succeed. 485 // Reducing size should also succeed.
475 value.clear(); 486 value.clear();
476 EXPECT_TRUE(PutSync(key, value)); 487 EXPECT_TRUE(PutSync(key, value));
477 488
478 // Increasing size should fail. 489 // Increasing size should fail.
479 value.resize(1, 'a'); 490 value.resize(1, 'a');
480 EXPECT_FALSE(PutSync(key, value)); 491 EXPECT_FALSE(PutSync(key, value));
481 } 492 }
482 493
494 TEST_F(LevelDBWrapperImplTest, GetAfterPurgeMemory) {
495 std::vector<uint8_t> result;
496 EXPECT_TRUE(GetSync(StdStringToUint8Vector("123"), &result));
497 EXPECT_EQ(StdStringToUint8Vector("123data"), result);
498 EXPECT_EQ(delegate()->map_load_count(), 1);
499
500 // Reading again doesn't load map again.
501 EXPECT_TRUE(GetSync(StdStringToUint8Vector("123"), &result));
502 EXPECT_EQ(delegate()->map_load_count(), 1);
503
504 wrapper_impl()->PurgeMemory();
505
506 // Now reading should still work, and load map again.
507 result.clear();
508 EXPECT_TRUE(GetSync(StdStringToUint8Vector("123"), &result));
509 EXPECT_EQ(StdStringToUint8Vector("123data"), result);
510 EXPECT_EQ(delegate()->map_load_count(), 2);
511 }
512
513 TEST_F(LevelDBWrapperImplTest, PurgeMemoryWithPendingChanges) {
514 std::vector<uint8_t> key = StdStringToUint8Vector("123");
515 std::vector<uint8_t> value = StdStringToUint8Vector("foo");
516 EXPECT_TRUE(PutSync(key, value));
517 EXPECT_EQ(delegate()->map_load_count(), 1);
518
519 // Purge memory, and read. Should not actually have purged, so should not have
520 // triggered a load.
521 wrapper_impl()->PurgeMemory();
522
523 std::vector<uint8_t> result;
524 EXPECT_TRUE(GetSync(key, &result));
525 EXPECT_EQ(value, result);
526 EXPECT_EQ(delegate()->map_load_count(), 1);
527 }
528
483 } // namespace content 529 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/leveldb_wrapper_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698