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

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

Issue 2593503005: Don't abuse LevelDBObserver interface to pass GetAll result. (Closed)
Patch Set: modify sanity_check test to give async callbacks a chance to cause problems Created 3 years, 12 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 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/run_loop.h" 7 #include "base/run_loop.h"
8 #include "components/leveldb/public/cpp/util.h" 8 #include "components/leveldb/public/cpp/util.h"
9 #include "content/public/test/test_browser_thread_bundle.h" 9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "mojo/public/cpp/bindings/associated_binding.h" 10 #include "mojo/public/cpp/bindings/associated_binding.h"
11 #include "mojo/public/cpp/bindings/strong_associated_binding.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 using leveldb::StdStringToUint8Vector; 14 using leveldb::StdStringToUint8Vector;
14 using leveldb::Uint8VectorToStdString; 15 using leveldb::Uint8VectorToStdString;
15 16
16 namespace content { 17 namespace content {
17 18
18 namespace { 19 namespace {
19 20
20 const char* kTestPrefix = "abc"; 21 const char* kTestPrefix = "abc";
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 157
157 void IteratorPrev(const base::UnguessableToken& iterator, 158 void IteratorPrev(const base::UnguessableToken& iterator,
158 const IteratorPrevCallback& callback) override { 159 const IteratorPrevCallback& callback) override {
159 FAIL(); 160 FAIL();
160 } 161 }
161 162
162 private: 163 private:
163 std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data_; 164 std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data_;
164 }; 165 };
165 166
167 class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback {
168 public:
169 static mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo CreateAndBind(
170 mojo::AssociatedGroup* associated_group,
171 bool* result,
172 const base::Closure& callback) {
173 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo ptr_info;
174 mojom::LevelDBWrapperGetAllCallbackAssociatedRequest request;
175 associated_group->CreateAssociatedInterface(
176 mojo::AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request);
177 mojo::MakeStrongAssociatedBinding(
178 base::WrapUnique(new GetAllCallback(result, callback)),
179 std::move(request));
180 return ptr_info;
181 }
182
183 private:
184 GetAllCallback(bool* result, const base::Closure& callback)
185 : m_result(result), m_callback(callback) {}
186 void Complete(bool success) override {
187 *m_result = success;
188 m_callback.Run();
189 }
190
191 bool* m_result;
192 base::Closure m_callback;
193 };
194
166 void NoOp() {} 195 void NoOp() {}
167 196
168 void GetCallback(const base::Closure& callback, 197 void GetCallback(const base::Closure& callback,
169 bool* success_out, 198 bool* success_out,
170 std::vector<uint8_t>* value_out, 199 std::vector<uint8_t>* value_out,
171 bool success, 200 bool success,
172 const std::vector<uint8_t>& value) { 201 const std::vector<uint8_t>& value) {
173 *success_out = success; 202 *success_out = success;
174 *value_out = value; 203 *value_out = value;
175 callback.Run(); 204 callback.Run();
(...skipping 28 matching lines...) Expand all
204 10 * 1024 * 1024 /* max_bytes_per_hour */, 233 10 * 1024 * 1024 /* max_bytes_per_hour */,
205 60 /* max_commits_per_hour */, 234 60 /* max_commits_per_hour */,
206 base::Bind(&NoOp)), 235 base::Bind(&NoOp)),
207 observer_binding_(this) { 236 observer_binding_(this) {
208 set_mock_data(std::string(kTestPrefix) + "def", "defdata"); 237 set_mock_data(std::string(kTestPrefix) + "def", "defdata");
209 set_mock_data(std::string(kTestPrefix) + "123", "123data"); 238 set_mock_data(std::string(kTestPrefix) + "123", "123data");
210 set_mock_data("123", "baddata"); 239 set_mock_data("123", "baddata");
211 240
212 level_db_wrapper_.Bind(mojo::MakeRequest(&level_db_wrapper_ptr_)); 241 level_db_wrapper_.Bind(mojo::MakeRequest(&level_db_wrapper_ptr_));
213 mojom::LevelDBObserverAssociatedPtrInfo ptr_info; 242 mojom::LevelDBObserverAssociatedPtrInfo ptr_info;
214 observer_binding_.Bind(&ptr_info, level_db_wrapper_ptr_.associated_group()); 243 observer_binding_.Bind(&ptr_info, associated_group());
215 level_db_wrapper_ptr_->AddObserver(std::move(ptr_info)); 244 level_db_wrapper_ptr_->AddObserver(std::move(ptr_info));
216 } 245 }
217 246
218 void set_mock_data(const std::string& key, const std::string& value) { 247 void set_mock_data(const std::string& key, const std::string& value) {
219 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); 248 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value);
220 } 249 }
221 250
222 void set_mock_data(const std::vector<uint8_t>& key, 251 void set_mock_data(const std::vector<uint8_t>& key,
223 const std::vector<uint8_t>& value) { 252 const std::vector<uint8_t>& value) {
224 mock_data_[key] = value; 253 mock_data_[key] = value;
225 } 254 }
226 255
227 bool has_mock_data(const std::string& key) { 256 bool has_mock_data(const std::string& key) {
228 return mock_data_.find(StdStringToUint8Vector(key)) != mock_data_.end(); 257 return mock_data_.find(StdStringToUint8Vector(key)) != mock_data_.end();
229 } 258 }
230 259
231 std::string get_mock_data(const std::string& key) { 260 std::string get_mock_data(const std::string& key) {
232 return has_mock_data(key) 261 return has_mock_data(key)
233 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)]) 262 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)])
234 : ""; 263 : "";
235 } 264 }
236 265
237 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); } 266 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); }
267 mojo::AssociatedGroup* associated_group() {
268 return level_db_wrapper_ptr_.associated_group();
269 }
238 270
239 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) { 271 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) {
240 base::RunLoop run_loop; 272 base::RunLoop run_loop;
241 bool success = false; 273 bool success = false;
242 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(), 274 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(),
243 &success, result)); 275 &success, result));
244 run_loop.Run(); 276 run_loop.Run();
245 return success; 277 return success;
246 } 278 }
247 279
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 } 334 }
303 void KeyDeleted(const std::vector<uint8_t>& key, 335 void KeyDeleted(const std::vector<uint8_t>& key,
304 const std::vector<uint8_t>& old_value, 336 const std::vector<uint8_t>& old_value,
305 const std::string& source) override { 337 const std::string& source) override {
306 observations_.push_back({Observation::kDelete, Uint8VectorToStdString(key), 338 observations_.push_back({Observation::kDelete, Uint8VectorToStdString(key),
307 Uint8VectorToStdString(old_value), "", source}); 339 Uint8VectorToStdString(old_value), "", source});
308 } 340 }
309 void AllDeleted(const std::string& source) override { 341 void AllDeleted(const std::string& source) override {
310 observations_.push_back({Observation::kDeleteAll, "", "", "", source}); 342 observations_.push_back({Observation::kDeleteAll, "", "", "", source});
311 } 343 }
312 void GetAllComplete(const std::string& source) override {}
313 344
314 TestBrowserThreadBundle thread_bundle_; 345 TestBrowserThreadBundle thread_bundle_;
315 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; 346 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_;
316 MockLevelDBDatabase db_; 347 MockLevelDBDatabase db_;
317 LevelDBWrapperImpl level_db_wrapper_; 348 LevelDBWrapperImpl level_db_wrapper_;
318 mojom::LevelDBWrapperPtr level_db_wrapper_ptr_; 349 mojom::LevelDBWrapperPtr level_db_wrapper_ptr_;
319 mojo::AssociatedBinding<mojom::LevelDBObserver> observer_binding_; 350 mojo::AssociatedBinding<mojom::LevelDBObserver> observer_binding_;
320 std::vector<Observation> observations_; 351 std::vector<Observation> observations_;
321 }; 352 };
322 353
(...skipping 23 matching lines...) Expand all
346 EXPECT_TRUE(PutSync(key, value)); 377 EXPECT_TRUE(PutSync(key, value));
347 378
348 std::vector<uint8_t> result; 379 std::vector<uint8_t> result;
349 EXPECT_TRUE(GetSync(key, &result)); 380 EXPECT_TRUE(GetSync(key, &result));
350 EXPECT_EQ(value, result); 381 EXPECT_EQ(value, result);
351 } 382 }
352 383
353 TEST_F(LevelDBWrapperImplTest, GetAll) { 384 TEST_F(LevelDBWrapperImplTest, GetAll) {
354 leveldb::mojom::DatabaseError status; 385 leveldb::mojom::DatabaseError status;
355 std::vector<mojom::KeyValuePtr> data; 386 std::vector<mojom::KeyValuePtr> data;
356 EXPECT_TRUE(wrapper()->GetAll(kTestSource, &status, &data)); 387 base::RunLoop run_loop;
388 bool result = false;
389 EXPECT_TRUE(wrapper()->GetAll(
390 GetAllCallback::CreateAndBind(associated_group(), &result,
391 run_loop.QuitClosure()),
392 &status, &data));
357 EXPECT_EQ(leveldb::mojom::DatabaseError::OK, status); 393 EXPECT_EQ(leveldb::mojom::DatabaseError::OK, status);
358 EXPECT_EQ(2u, data.size()); 394 EXPECT_EQ(2u, data.size());
395 EXPECT_FALSE(result);
396 run_loop.Run();
397 EXPECT_TRUE(result);
359 } 398 }
360 399
361 TEST_F(LevelDBWrapperImplTest, CommitPutToDB) { 400 TEST_F(LevelDBWrapperImplTest, CommitPutToDB) {
362 std::string key1 = "123"; 401 std::string key1 = "123";
363 std::string value1 = "foo"; 402 std::string value1 = "foo";
364 std::string key2 = "abc"; 403 std::string key2 = "abc";
365 std::string value2 = "data abc"; 404 std::string value2 = "data abc";
366 405
367 EXPECT_TRUE( 406 EXPECT_TRUE(
368 PutSync(StdStringToUint8Vector(key1), StdStringToUint8Vector(value1))); 407 PutSync(StdStringToUint8Vector(key1), StdStringToUint8Vector(value1)));
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 // Reducing size should also succeed. 560 // Reducing size should also succeed.
522 value.clear(); 561 value.clear();
523 EXPECT_TRUE(PutSync(key, value)); 562 EXPECT_TRUE(PutSync(key, value));
524 563
525 // Increasing size should fail. 564 // Increasing size should fail.
526 value.resize(1, 'a'); 565 value.resize(1, 'a');
527 EXPECT_FALSE(PutSync(key, value)); 566 EXPECT_FALSE(PutSync(key, value));
528 } 567 }
529 568
530 } // namespace content 569 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698