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

Side by Side Diff: content/browser/indexed_db/indexed_db_factory_unittest.cc

Issue 2472213003: [IndexedDB] Refactoring to remove ref ptrs and host transaction ids. (Closed)
Patch Set: comments & rebase Created 4 years, 1 month 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 248
249 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context()); 249 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context());
250 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = 250 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks =
251 new LookingForQuotaErrorMockCallbacks; 251 new LookingForQuotaErrorMockCallbacks;
252 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = 252 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks =
253 new IndexedDBDatabaseCallbacks(nullptr, 0, nullptr); 253 new IndexedDBDatabaseCallbacks(nullptr, 0, nullptr);
254 const base::string16 name(ASCIIToUTF16("name")); 254 const base::string16 name(ASCIIToUTF16("name"));
255 std::unique_ptr<IndexedDBPendingConnection> connection( 255 std::unique_ptr<IndexedDBPendingConnection> connection(
256 base::MakeUnique<IndexedDBPendingConnection>( 256 base::MakeUnique<IndexedDBPendingConnection>(
257 callbacks, dummy_database_callbacks, 0 /* child_process_id */, 257 callbacks, dummy_database_callbacks, 0 /* child_process_id */,
258 2 /* transaction_id */, 1 /* version */)); 258 url::Origin(), 2 /* transaction_id */, 1 /* version */));
259 factory->Open(name, std::move(connection), nullptr /* request_context */, 259 factory->Open(name, std::move(connection), nullptr /* request_context */,
260 origin, temp_directory.GetPath()); 260 origin, temp_directory.GetPath());
261 EXPECT_TRUE(callbacks->error_called()); 261 EXPECT_TRUE(callbacks->error_called());
262 } 262 }
263 263
264 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) { 264 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) {
265 const Origin origin(GURL("http://localhost:81")); 265 const Origin origin(GURL("http://localhost:81"));
266 266
267 base::ScopedTempDir temp_directory; 267 base::ScopedTempDir temp_directory;
268 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 268 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
269 269
270 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 270 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
271 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 271 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
272 new MockIndexedDBDatabaseCallbacks()); 272 new MockIndexedDBDatabaseCallbacks());
273 const int64_t transaction_id = 1; 273 const int64_t transaction_id = 1;
274 std::unique_ptr<IndexedDBPendingConnection> connection( 274 std::unique_ptr<IndexedDBPendingConnection> connection(
275 base::MakeUnique<IndexedDBPendingConnection>( 275 base::MakeUnique<IndexedDBPendingConnection>(
276 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, 276 callbacks, db_callbacks, 0 /* child_process_id */, url::Origin(),
277 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); 277 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION));
278 factory()->Open(ASCIIToUTF16("db"), std::move(connection), 278 factory()->Open(ASCIIToUTF16("db"), std::move(connection),
279 nullptr /* request_context */, origin, 279 nullptr /* request_context */, origin,
280 temp_directory.GetPath()); 280 temp_directory.GetPath());
281 281
282 EXPECT_TRUE(callbacks->connection()); 282 EXPECT_TRUE(callbacks->connection());
283 283
284 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 284 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
285 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 285 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
286 286
287 callbacks->connection()->ForceClose(); 287 callbacks->connection()->ForceClose();
288 288
289 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 289 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
290 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 290 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
291 } 291 }
292 292
293 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) { 293 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) {
294 const Origin origin(GURL("http://localhost:81")); 294 const Origin origin(GURL("http://localhost:81"));
295 295
296 base::ScopedTempDir temp_directory; 296 base::ScopedTempDir temp_directory;
297 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 297 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
298 298
299 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 299 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
300 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 300 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
301 new MockIndexedDBDatabaseCallbacks()); 301 new MockIndexedDBDatabaseCallbacks());
302 const int64_t transaction_id = 1; 302 const int64_t transaction_id = 1;
303 std::unique_ptr<IndexedDBPendingConnection> connection( 303 std::unique_ptr<IndexedDBPendingConnection> connection(
304 base::MakeUnique<IndexedDBPendingConnection>( 304 base::MakeUnique<IndexedDBPendingConnection>(
305 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, 305 callbacks, db_callbacks, 0 /* child_process_id */, url::Origin(),
306 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); 306 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION));
307 factory()->Open(ASCIIToUTF16("db"), std::move(connection), 307 factory()->Open(ASCIIToUTF16("db"), std::move(connection),
308 nullptr /* request_context */, origin, 308 nullptr /* request_context */, origin,
309 temp_directory.GetPath()); 309 temp_directory.GetPath());
310 310
311 EXPECT_TRUE(callbacks->connection()); 311 EXPECT_TRUE(callbacks->connection());
312 IndexedDBBackingStore* store = 312 IndexedDBBackingStore* store =
313 callbacks->connection()->database()->backing_store(); 313 callbacks->connection()->database()->backing_store();
314 EXPECT_FALSE(store->HasOneRef()); // Factory and database. 314 EXPECT_FALSE(store->HasOneRef()); // Factory and database.
315 315
316 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 316 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 383
384 base::ScopedTempDir temp_directory; 384 base::ScopedTempDir temp_directory;
385 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 385 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
386 386
387 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 387 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
388 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 388 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
389 new MockIndexedDBDatabaseCallbacks()); 389 new MockIndexedDBDatabaseCallbacks());
390 const int64_t transaction_id = 1; 390 const int64_t transaction_id = 1;
391 std::unique_ptr<IndexedDBPendingConnection> connection( 391 std::unique_ptr<IndexedDBPendingConnection> connection(
392 base::MakeUnique<IndexedDBPendingConnection>( 392 base::MakeUnique<IndexedDBPendingConnection>(
393 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, 393 callbacks, db_callbacks, 0 /* child_process_id */, url::Origin(),
394 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); 394 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION));
395 factory()->Open(ASCIIToUTF16("db"), std::move(connection), 395 factory()->Open(ASCIIToUTF16("db"), std::move(connection),
396 nullptr /* request_context */, origin, 396 nullptr /* request_context */, origin,
397 temp_directory.GetPath()); 397 temp_directory.GetPath());
398 398
399 EXPECT_TRUE(callbacks->connection()); 399 EXPECT_TRUE(callbacks->connection());
400 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 400 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
401 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 401 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
402 402
403 callbacks->connection()->Close(); 403 callbacks->connection()->Close();
404 404
(...skipping 14 matching lines...) Expand all
419 UpgradeNeededCallbacks() {} 419 UpgradeNeededCallbacks() {}
420 420
421 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, 421 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection,
422 const IndexedDBDatabaseMetadata& metadata) override { 422 const IndexedDBDatabaseMetadata& metadata) override {
423 EXPECT_TRUE(connection_.get()); 423 EXPECT_TRUE(connection_.get());
424 EXPECT_FALSE(connection.get()); 424 EXPECT_FALSE(connection.get());
425 } 425 }
426 426
427 void OnUpgradeNeeded(int64_t old_version, 427 void OnUpgradeNeeded(int64_t old_version,
428 std::unique_ptr<IndexedDBConnection> connection, 428 std::unique_ptr<IndexedDBConnection> connection,
429 IndexedDBTransaction* transaction,
429 const content::IndexedDBDatabaseMetadata& metadata, 430 const content::IndexedDBDatabaseMetadata& metadata,
430 const IndexedDBDataLossInfo& data_loss_info) override { 431 const IndexedDBDataLossInfo& data_loss_info) override {
432 transaction_ = transaction;
431 connection_ = std::move(connection); 433 connection_ = std::move(connection);
432 } 434 }
433 435
436 IndexedDBTransaction* transaction() const { return transaction_; }
437
434 protected: 438 protected:
435 ~UpgradeNeededCallbacks() override {} 439 ~UpgradeNeededCallbacks() override {}
436 440
437 private: 441 private:
442 IndexedDBTransaction* transaction_ = nullptr;
443
438 DISALLOW_COPY_AND_ASSIGN(UpgradeNeededCallbacks); 444 DISALLOW_COPY_AND_ASSIGN(UpgradeNeededCallbacks);
439 }; 445 };
440 446
441 class ErrorCallbacks : public MockIndexedDBCallbacks { 447 class ErrorCallbacks : public MockIndexedDBCallbacks {
442 public: 448 public:
443 ErrorCallbacks() : MockIndexedDBCallbacks(false), saw_error_(false) {} 449 ErrorCallbacks() : MockIndexedDBCallbacks(false), saw_error_(false) {}
444 450
445 void OnError(const IndexedDBDatabaseError& error) override { 451 void OnError(const IndexedDBDatabaseError& error) override {
446 saw_error_ = true; 452 saw_error_ = true;
447 } 453 }
(...skipping 13 matching lines...) Expand all
461 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 467 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
462 468
463 const base::string16 db_name(ASCIIToUTF16("db")); 469 const base::string16 db_name(ASCIIToUTF16("db"));
464 const int64_t db_version = 2; 470 const int64_t db_version = 2;
465 const int64_t transaction_id = 1; 471 const int64_t transaction_id = 1;
466 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks( 472 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks(
467 new MockIndexedDBDatabaseCallbacks()); 473 new MockIndexedDBDatabaseCallbacks());
468 474
469 // Open at version 2, then close. 475 // Open at version 2, then close.
470 { 476 {
471 scoped_refptr<MockIndexedDBCallbacks> callbacks( 477 scoped_refptr<UpgradeNeededCallbacks> callbacks(
472 new UpgradeNeededCallbacks()); 478 new UpgradeNeededCallbacks());
473 std::unique_ptr<IndexedDBPendingConnection> connection( 479 std::unique_ptr<IndexedDBPendingConnection> connection(
474 base::MakeUnique<IndexedDBPendingConnection>( 480 base::MakeUnique<IndexedDBPendingConnection>(
475 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, 481 callbacks, db_callbacks, 0 /* child_process_id */, url::Origin(),
476 db_version)); 482 transaction_id, db_version));
477 factory()->Open(db_name, std::move(connection), 483 factory()->Open(db_name, std::move(connection),
478 nullptr /* request_context */, origin, 484 nullptr /* request_context */, origin,
479 temp_directory.GetPath()); 485 temp_directory.GetPath());
480 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name)); 486 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name));
481 487
482 // Pump the message loop so the upgrade transaction can run. 488 // Pump the message loop so the upgrade transaction can run.
483 base::RunLoop().RunUntilIdle(); 489 base::RunLoop().RunUntilIdle();
484 EXPECT_TRUE(callbacks->connection()); 490 EXPECT_TRUE(callbacks->connection());
485 callbacks->connection()->database()->Commit(transaction_id); 491 EXPECT_NE(callbacks->transaction(), nullptr);
492 callbacks->connection()->database()->Commit(callbacks->transaction());
486 493
487 callbacks->connection()->Close(); 494 callbacks->connection()->Close();
488 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); 495 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name));
489 } 496 }
490 497
491 // Open at version < 2, which will fail; ensure factory doesn't retain 498 // Open at version < 2, which will fail; ensure factory doesn't retain
492 // the database object. 499 // the database object.
493 { 500 {
494 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks()); 501 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks());
495 std::unique_ptr<IndexedDBPendingConnection> connection( 502 std::unique_ptr<IndexedDBPendingConnection> connection(
496 base::MakeUnique<IndexedDBPendingConnection>( 503 base::MakeUnique<IndexedDBPendingConnection>(
497 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, 504 callbacks, db_callbacks, 0 /* child_process_id */, url::Origin(),
498 db_version - 1)); 505 transaction_id, db_version - 1));
499 factory()->Open(db_name, std::move(connection), 506 factory()->Open(db_name, std::move(connection),
500 nullptr /* request_context */, origin, 507 nullptr /* request_context */, origin,
501 temp_directory.GetPath()); 508 temp_directory.GetPath());
502 EXPECT_TRUE(callbacks->saw_error()); 509 EXPECT_TRUE(callbacks->saw_error());
503 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); 510 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name));
504 } 511 }
505 512
506 // Terminate all pending-close timers. 513 // Terminate all pending-close timers.
507 factory()->ForceClose(origin); 514 factory()->ForceClose(origin);
508 } 515 }
509 516
510 } // namespace content 517 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698