| OLD | NEW |
| 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 "content/browser/indexed_db/indexed_db_transaction.h" | 5 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "content/browser/indexed_db/indexed_db_connection.h" | 15 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 16 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" | 16 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
| 17 #include "content/browser/indexed_db/indexed_db_observer.h" | 17 #include "content/browser/indexed_db/indexed_db_observer.h" |
| 18 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" | 18 #include "content/browser/indexed_db/mock_indexed_db_change_handler.h" |
| 19 #include "content/browser/indexed_db/mock_indexed_db_factory.h" | 19 #include "content/browser/indexed_db/mock_indexed_db_factory.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 class AbortObserver { | 24 class AbortObserver { |
| 25 public: | 25 public: |
| 26 AbortObserver() : abort_task_called_(false) {} | 26 AbortObserver() : abort_task_called_(false) {} |
| 27 | 27 |
| 28 void AbortTask(IndexedDBTransaction* transaction) { | 28 void AbortTask(IndexedDBTransaction* transaction) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 IndexedDBTransactionTestMode() {} | 82 IndexedDBTransactionTestMode() {} |
| 83 private: | 83 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(IndexedDBTransactionTestMode); | 84 DISALLOW_COPY_AND_ASSIGN(IndexedDBTransactionTestMode); |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 TEST_F(IndexedDBTransactionTest, Timeout) { | 87 TEST_F(IndexedDBTransactionTest, Timeout) { |
| 88 const int64_t id = 0; | 88 const int64_t id = 0; |
| 89 const std::set<int64_t> scope; | 89 const std::set<int64_t> scope; |
| 90 const leveldb::Status commit_success = leveldb::Status::OK(); | 90 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 91 std::unique_ptr<IndexedDBConnection> connection( | 91 std::unique_ptr<IndexedDBConnection> connection( |
| 92 base::MakeUnique<IndexedDBConnection>( | 92 base::MakeUnique<IndexedDBConnection>(db_, |
| 93 db_, new MockIndexedDBDatabaseCallbacks())); | 93 new MockIndexedDBChangeHandler())); |
| 94 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 94 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 95 id, connection->GetWeakPtr(), scope, | 95 id, connection->GetWeakPtr(), scope, |
| 96 blink::WebIDBTransactionModeReadWrite, | 96 blink::WebIDBTransactionModeReadWrite, |
| 97 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 97 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 98 db_->TransactionCreated(transaction.get()); | 98 db_->TransactionCreated(transaction.get()); |
| 99 | 99 |
| 100 // No conflicting transactions, so coordinator will start it immediately: | 100 // No conflicting transactions, so coordinator will start it immediately: |
| 101 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 101 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 102 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 102 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 103 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 103 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 126 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 126 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 127 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); | 127 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); |
| 128 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); | 128 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(IndexedDBTransactionTest, NoTimeoutReadOnly) { | 131 TEST_F(IndexedDBTransactionTest, NoTimeoutReadOnly) { |
| 132 const int64_t id = 0; | 132 const int64_t id = 0; |
| 133 const std::set<int64_t> scope; | 133 const std::set<int64_t> scope; |
| 134 const leveldb::Status commit_success = leveldb::Status::OK(); | 134 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 135 std::unique_ptr<IndexedDBConnection> connection( | 135 std::unique_ptr<IndexedDBConnection> connection( |
| 136 base::MakeUnique<IndexedDBConnection>( | 136 base::MakeUnique<IndexedDBConnection>(db_, |
| 137 db_, new MockIndexedDBDatabaseCallbacks())); | 137 new MockIndexedDBChangeHandler())); |
| 138 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 138 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 139 id, connection->GetWeakPtr(), scope, blink::WebIDBTransactionModeReadOnly, | 139 id, connection->GetWeakPtr(), scope, blink::WebIDBTransactionModeReadOnly, |
| 140 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 140 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 141 db_->TransactionCreated(transaction.get()); | 141 db_->TransactionCreated(transaction.get()); |
| 142 | 142 |
| 143 // No conflicting transactions, so coordinator will start it immediately: | 143 // No conflicting transactions, so coordinator will start it immediately: |
| 144 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 144 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 145 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 145 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 146 | 146 |
| 147 // Schedule a task - timer won't be started until it's processed. | 147 // Schedule a task - timer won't be started until it's processed. |
| 148 transaction->ScheduleTask(base::Bind( | 148 transaction->ScheduleTask(base::Bind( |
| 149 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); | 149 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); |
| 150 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 150 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 151 | 151 |
| 152 // Transaction is read-only, so no need to time it out. | 152 // Transaction is read-only, so no need to time it out. |
| 153 RunPostedTasks(); | 153 RunPostedTasks(); |
| 154 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 154 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 155 | 155 |
| 156 // Clean up to avoid leaks. | 156 // Clean up to avoid leaks. |
| 157 transaction->Abort(); | 157 transaction->Abort(); |
| 158 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 158 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 159 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 159 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 160 } | 160 } |
| 161 | 161 |
| 162 TEST_P(IndexedDBTransactionTestMode, ScheduleNormalTask) { | 162 TEST_P(IndexedDBTransactionTestMode, ScheduleNormalTask) { |
| 163 const int64_t id = 0; | 163 const int64_t id = 0; |
| 164 const std::set<int64_t> scope; | 164 const std::set<int64_t> scope; |
| 165 const leveldb::Status commit_success = leveldb::Status::OK(); | 165 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 166 std::unique_ptr<IndexedDBConnection> connection( | 166 std::unique_ptr<IndexedDBConnection> connection( |
| 167 base::MakeUnique<IndexedDBConnection>( | 167 base::MakeUnique<IndexedDBConnection>(db_, |
| 168 db_, new MockIndexedDBDatabaseCallbacks())); | 168 new MockIndexedDBChangeHandler())); |
| 169 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 169 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 170 id, connection->GetWeakPtr(), scope, GetParam(), | 170 id, connection->GetWeakPtr(), scope, GetParam(), |
| 171 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 171 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 172 | 172 |
| 173 EXPECT_FALSE(transaction->HasPendingTasks()); | 173 EXPECT_FALSE(transaction->HasPendingTasks()); |
| 174 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); | 174 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); |
| 175 EXPECT_TRUE(transaction->task_queue_.empty()); | 175 EXPECT_TRUE(transaction->task_queue_.empty()); |
| 176 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 176 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 177 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 177 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| 178 EXPECT_EQ(0, transaction->diagnostics().tasks_completed); | 178 EXPECT_EQ(0, transaction->diagnostics().tasks_completed); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 218 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 219 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); | 219 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); |
| 220 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); | 220 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); |
| 221 } | 221 } |
| 222 | 222 |
| 223 TEST_F(IndexedDBTransactionTest, SchedulePreemptiveTask) { | 223 TEST_F(IndexedDBTransactionTest, SchedulePreemptiveTask) { |
| 224 const int64_t id = 0; | 224 const int64_t id = 0; |
| 225 const std::set<int64_t> scope; | 225 const std::set<int64_t> scope; |
| 226 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); | 226 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); |
| 227 std::unique_ptr<IndexedDBConnection> connection( | 227 std::unique_ptr<IndexedDBConnection> connection( |
| 228 base::MakeUnique<IndexedDBConnection>( | 228 base::MakeUnique<IndexedDBConnection>(db_, |
| 229 db_, new MockIndexedDBDatabaseCallbacks())); | 229 new MockIndexedDBChangeHandler())); |
| 230 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 230 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 231 id, connection->GetWeakPtr(), scope, | 231 id, connection->GetWeakPtr(), scope, |
| 232 blink::WebIDBTransactionModeVersionChange, | 232 blink::WebIDBTransactionModeVersionChange, |
| 233 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); | 233 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); |
| 234 | 234 |
| 235 EXPECT_FALSE(transaction->HasPendingTasks()); | 235 EXPECT_FALSE(transaction->HasPendingTasks()); |
| 236 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); | 236 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); |
| 237 EXPECT_TRUE(transaction->task_queue_.empty()); | 237 EXPECT_TRUE(transaction->task_queue_.empty()); |
| 238 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 238 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 239 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 239 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 279 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 280 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 280 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| 281 EXPECT_EQ(0, transaction->diagnostics().tasks_completed); | 281 EXPECT_EQ(0, transaction->diagnostics().tasks_completed); |
| 282 } | 282 } |
| 283 | 283 |
| 284 TEST_P(IndexedDBTransactionTestMode, AbortTasks) { | 284 TEST_P(IndexedDBTransactionTestMode, AbortTasks) { |
| 285 const int64_t id = 0; | 285 const int64_t id = 0; |
| 286 const std::set<int64_t> scope; | 286 const std::set<int64_t> scope; |
| 287 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); | 287 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); |
| 288 std::unique_ptr<IndexedDBConnection> connection( | 288 std::unique_ptr<IndexedDBConnection> connection( |
| 289 base::MakeUnique<IndexedDBConnection>( | 289 base::MakeUnique<IndexedDBConnection>(db_, |
| 290 db_, new MockIndexedDBDatabaseCallbacks())); | 290 new MockIndexedDBChangeHandler())); |
| 291 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 291 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 292 id, connection->GetWeakPtr(), scope, GetParam(), | 292 id, connection->GetWeakPtr(), scope, GetParam(), |
| 293 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); | 293 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); |
| 294 db_->TransactionCreated(transaction.get()); | 294 db_->TransactionCreated(transaction.get()); |
| 295 | 295 |
| 296 AbortObserver observer; | 296 AbortObserver observer; |
| 297 transaction->ScheduleTask( | 297 transaction->ScheduleTask( |
| 298 base::Bind(&IndexedDBTransactionTest::AbortableOperation, | 298 base::Bind(&IndexedDBTransactionTest::AbortableOperation, |
| 299 base::Unretained(this), | 299 base::Unretained(this), |
| 300 base::Unretained(&observer))); | 300 base::Unretained(&observer))); |
| 301 | 301 |
| 302 // Pump the message loop so that the transaction completes all pending tasks, | 302 // Pump the message loop so that the transaction completes all pending tasks, |
| 303 // otherwise it will defer the commit. | 303 // otherwise it will defer the commit. |
| 304 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
| 305 | 305 |
| 306 EXPECT_FALSE(observer.abort_task_called()); | 306 EXPECT_FALSE(observer.abort_task_called()); |
| 307 transaction->Commit(); | 307 transaction->Commit(); |
| 308 EXPECT_TRUE(observer.abort_task_called()); | 308 EXPECT_TRUE(observer.abort_task_called()); |
| 309 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 309 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 310 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 310 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 311 } | 311 } |
| 312 | 312 |
| 313 TEST_P(IndexedDBTransactionTestMode, AbortPreemptive) { | 313 TEST_P(IndexedDBTransactionTestMode, AbortPreemptive) { |
| 314 const int64_t id = 0; | 314 const int64_t id = 0; |
| 315 const std::set<int64_t> scope; | 315 const std::set<int64_t> scope; |
| 316 const leveldb::Status commit_success = leveldb::Status::OK(); | 316 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 317 std::unique_ptr<IndexedDBConnection> connection( | 317 std::unique_ptr<IndexedDBConnection> connection( |
| 318 base::MakeUnique<IndexedDBConnection>( | 318 base::MakeUnique<IndexedDBConnection>(db_, |
| 319 db_, new MockIndexedDBDatabaseCallbacks())); | 319 new MockIndexedDBChangeHandler())); |
| 320 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 320 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 321 id, connection->GetWeakPtr(), scope, GetParam(), | 321 id, connection->GetWeakPtr(), scope, GetParam(), |
| 322 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 322 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 323 db_->TransactionCreated(transaction.get()); | 323 db_->TransactionCreated(transaction.get()); |
| 324 | 324 |
| 325 // No conflicting transactions, so coordinator will start it immediately: | 325 // No conflicting transactions, so coordinator will start it immediately: |
| 326 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 326 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 327 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 327 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 328 | 328 |
| 329 transaction->ScheduleTask( | 329 transaction->ScheduleTask( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 358 EXPECT_FALSE(transaction->HasPendingTasks()); | 358 EXPECT_FALSE(transaction->HasPendingTasks()); |
| 359 EXPECT_EQ(transaction->diagnostics().tasks_completed, | 359 EXPECT_EQ(transaction->diagnostics().tasks_completed, |
| 360 transaction->diagnostics().tasks_scheduled); | 360 transaction->diagnostics().tasks_scheduled); |
| 361 } | 361 } |
| 362 | 362 |
| 363 TEST_F(IndexedDBTransactionTest, IndexedDBObserver) { | 363 TEST_F(IndexedDBTransactionTest, IndexedDBObserver) { |
| 364 const int64_t id = 0; | 364 const int64_t id = 0; |
| 365 const std::set<int64_t> scope; | 365 const std::set<int64_t> scope; |
| 366 const leveldb::Status commit_success = leveldb::Status::OK(); | 366 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 367 std::unique_ptr<IndexedDBConnection> connection( | 367 std::unique_ptr<IndexedDBConnection> connection( |
| 368 base::MakeUnique<IndexedDBConnection>( | 368 base::MakeUnique<IndexedDBConnection>(db_, |
| 369 db_, new MockIndexedDBDatabaseCallbacks())); | 369 new MockIndexedDBChangeHandler())); |
| 370 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 370 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 371 id, connection->GetWeakPtr(), scope, | 371 id, connection->GetWeakPtr(), scope, |
| 372 blink::WebIDBTransactionModeReadWrite, | 372 blink::WebIDBTransactionModeReadWrite, |
| 373 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 373 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 374 db_->TransactionCreated(transaction.get()); | 374 db_->TransactionCreated(transaction.get()); |
| 375 | 375 |
| 376 EXPECT_EQ(0UL, transaction->pending_observers_.size()); | 376 EXPECT_EQ(0UL, transaction->pending_observers_.size()); |
| 377 EXPECT_EQ(0UL, connection->active_observers().size()); | 377 EXPECT_EQ(0UL, connection->active_observers().size()); |
| 378 | 378 |
| 379 // Add observers to pending observer list. | 379 // Add observers to pending observer list. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 407 | 407 |
| 408 static const blink::WebIDBTransactionMode kTestModes[] = { | 408 static const blink::WebIDBTransactionMode kTestModes[] = { |
| 409 blink::WebIDBTransactionModeReadOnly, blink::WebIDBTransactionModeReadWrite, | 409 blink::WebIDBTransactionModeReadOnly, blink::WebIDBTransactionModeReadWrite, |
| 410 blink::WebIDBTransactionModeVersionChange}; | 410 blink::WebIDBTransactionModeVersionChange}; |
| 411 | 411 |
| 412 INSTANTIATE_TEST_CASE_P(IndexedDBTransactions, | 412 INSTANTIATE_TEST_CASE_P(IndexedDBTransactions, |
| 413 IndexedDBTransactionTestMode, | 413 IndexedDBTransactionTestMode, |
| 414 ::testing::ValuesIn(kTestModes)); | 414 ::testing::ValuesIn(kTestModes)); |
| 415 | 415 |
| 416 } // namespace content | 416 } // namespace content |
| OLD | NEW |