| 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" |
| (...skipping 71 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 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 92 base::MakeUnique<IndexedDBConnection>( |
| 93 db_, new MockIndexedDBDatabaseCallbacks())); |
| 93 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 94 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 94 id, connection->GetWeakPtr(), scope, | 95 id, connection->GetWeakPtr(), scope, |
| 95 blink::WebIDBTransactionModeReadWrite, | 96 blink::WebIDBTransactionModeReadWrite, |
| 96 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 97 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 97 db_->TransactionCreated(transaction.get()); | 98 db_->TransactionCreated(transaction.get()); |
| 98 | 99 |
| 99 // No conflicting transactions, so coordinator will start it immediately: | 100 // No conflicting transactions, so coordinator will start it immediately: |
| 100 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 101 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 101 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 102 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 102 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 103 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 125 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 126 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 126 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); | 127 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); |
| 127 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); | 128 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); |
| 128 } | 129 } |
| 129 | 130 |
| 130 TEST_F(IndexedDBTransactionTest, NoTimeoutReadOnly) { | 131 TEST_F(IndexedDBTransactionTest, NoTimeoutReadOnly) { |
| 131 const int64_t id = 0; | 132 const int64_t id = 0; |
| 132 const std::set<int64_t> scope; | 133 const std::set<int64_t> scope; |
| 133 const leveldb::Status commit_success = leveldb::Status::OK(); | 134 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 134 std::unique_ptr<IndexedDBConnection> connection( | 135 std::unique_ptr<IndexedDBConnection> connection( |
| 135 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 136 base::MakeUnique<IndexedDBConnection>( |
| 137 db_, new MockIndexedDBDatabaseCallbacks())); |
| 136 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 138 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 137 id, connection->GetWeakPtr(), scope, blink::WebIDBTransactionModeReadOnly, | 139 id, connection->GetWeakPtr(), scope, blink::WebIDBTransactionModeReadOnly, |
| 138 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 140 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 139 db_->TransactionCreated(transaction.get()); | 141 db_->TransactionCreated(transaction.get()); |
| 140 | 142 |
| 141 // No conflicting transactions, so coordinator will start it immediately: | 143 // No conflicting transactions, so coordinator will start it immediately: |
| 142 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 144 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 143 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 145 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 144 | 146 |
| 145 // 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. |
| 146 transaction->ScheduleTask(base::Bind( | 148 transaction->ScheduleTask(base::Bind( |
| 147 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); | 149 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); |
| 148 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 150 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 149 | 151 |
| 150 // Transaction is read-only, so no need to time it out. | 152 // Transaction is read-only, so no need to time it out. |
| 151 RunPostedTasks(); | 153 RunPostedTasks(); |
| 152 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 154 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 153 | 155 |
| 154 // Clean up to avoid leaks. | 156 // Clean up to avoid leaks. |
| 155 transaction->Abort(); | 157 transaction->Abort(); |
| 156 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 158 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 157 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 159 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 158 } | 160 } |
| 159 | 161 |
| 160 TEST_P(IndexedDBTransactionTestMode, ScheduleNormalTask) { | 162 TEST_P(IndexedDBTransactionTestMode, ScheduleNormalTask) { |
| 161 const int64_t id = 0; | 163 const int64_t id = 0; |
| 162 const std::set<int64_t> scope; | 164 const std::set<int64_t> scope; |
| 163 const leveldb::Status commit_success = leveldb::Status::OK(); | 165 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 164 std::unique_ptr<IndexedDBConnection> connection( | 166 std::unique_ptr<IndexedDBConnection> connection( |
| 165 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 167 base::MakeUnique<IndexedDBConnection>( |
| 168 db_, new MockIndexedDBDatabaseCallbacks())); |
| 166 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 169 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 167 id, connection->GetWeakPtr(), scope, GetParam(), | 170 id, connection->GetWeakPtr(), scope, GetParam(), |
| 168 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 171 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 169 | 172 |
| 170 EXPECT_FALSE(transaction->HasPendingTasks()); | 173 EXPECT_FALSE(transaction->HasPendingTasks()); |
| 171 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); | 174 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); |
| 172 EXPECT_TRUE(transaction->task_queue_.empty()); | 175 EXPECT_TRUE(transaction->task_queue_.empty()); |
| 173 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 176 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 174 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 177 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| 175 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... |
| 215 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 218 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 216 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); | 219 EXPECT_EQ(1, transaction->diagnostics().tasks_scheduled); |
| 217 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); | 220 EXPECT_EQ(1, transaction->diagnostics().tasks_completed); |
| 218 } | 221 } |
| 219 | 222 |
| 220 TEST_F(IndexedDBTransactionTest, SchedulePreemptiveTask) { | 223 TEST_F(IndexedDBTransactionTest, SchedulePreemptiveTask) { |
| 221 const int64_t id = 0; | 224 const int64_t id = 0; |
| 222 const std::set<int64_t> scope; | 225 const std::set<int64_t> scope; |
| 223 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); | 226 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); |
| 224 std::unique_ptr<IndexedDBConnection> connection( | 227 std::unique_ptr<IndexedDBConnection> connection( |
| 225 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 228 base::MakeUnique<IndexedDBConnection>( |
| 229 db_, new MockIndexedDBDatabaseCallbacks())); |
| 226 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 230 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 227 id, connection->GetWeakPtr(), scope, | 231 id, connection->GetWeakPtr(), scope, |
| 228 blink::WebIDBTransactionModeVersionChange, | 232 blink::WebIDBTransactionModeVersionChange, |
| 229 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); | 233 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); |
| 230 | 234 |
| 231 EXPECT_FALSE(transaction->HasPendingTasks()); | 235 EXPECT_FALSE(transaction->HasPendingTasks()); |
| 232 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); | 236 EXPECT_TRUE(transaction->IsTaskQueueEmpty()); |
| 233 EXPECT_TRUE(transaction->task_queue_.empty()); | 237 EXPECT_TRUE(transaction->task_queue_.empty()); |
| 234 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 238 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 235 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... |
| 275 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); | 279 EXPECT_TRUE(transaction->preemptive_task_queue_.empty()); |
| 276 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); | 280 EXPECT_EQ(0, transaction->diagnostics().tasks_scheduled); |
| 277 EXPECT_EQ(0, transaction->diagnostics().tasks_completed); | 281 EXPECT_EQ(0, transaction->diagnostics().tasks_completed); |
| 278 } | 282 } |
| 279 | 283 |
| 280 TEST_P(IndexedDBTransactionTestMode, AbortTasks) { | 284 TEST_P(IndexedDBTransactionTestMode, AbortTasks) { |
| 281 const int64_t id = 0; | 285 const int64_t id = 0; |
| 282 const std::set<int64_t> scope; | 286 const std::set<int64_t> scope; |
| 283 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); | 287 const leveldb::Status commit_failure = leveldb::Status::Corruption("Ouch."); |
| 284 std::unique_ptr<IndexedDBConnection> connection( | 288 std::unique_ptr<IndexedDBConnection> connection( |
| 285 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 289 base::MakeUnique<IndexedDBConnection>( |
| 290 db_, new MockIndexedDBDatabaseCallbacks())); |
| 286 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 291 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 287 id, connection->GetWeakPtr(), scope, GetParam(), | 292 id, connection->GetWeakPtr(), scope, GetParam(), |
| 288 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); | 293 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); |
| 289 db_->TransactionCreated(transaction.get()); | 294 db_->TransactionCreated(transaction.get()); |
| 290 | 295 |
| 291 AbortObserver observer; | 296 AbortObserver observer; |
| 292 transaction->ScheduleTask( | 297 transaction->ScheduleTask( |
| 293 base::Bind(&IndexedDBTransactionTest::AbortableOperation, | 298 base::Bind(&IndexedDBTransactionTest::AbortableOperation, |
| 294 base::Unretained(this), | 299 base::Unretained(this), |
| 295 base::Unretained(&observer))); | 300 base::Unretained(&observer))); |
| 296 | 301 |
| 297 // 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, |
| 298 // otherwise it will defer the commit. | 303 // otherwise it will defer the commit. |
| 299 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
| 300 | 305 |
| 301 EXPECT_FALSE(observer.abort_task_called()); | 306 EXPECT_FALSE(observer.abort_task_called()); |
| 302 transaction->Commit(); | 307 transaction->Commit(); |
| 303 EXPECT_TRUE(observer.abort_task_called()); | 308 EXPECT_TRUE(observer.abort_task_called()); |
| 304 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 309 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 305 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 310 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 306 } | 311 } |
| 307 | 312 |
| 308 TEST_P(IndexedDBTransactionTestMode, AbortPreemptive) { | 313 TEST_P(IndexedDBTransactionTestMode, AbortPreemptive) { |
| 309 const int64_t id = 0; | 314 const int64_t id = 0; |
| 310 const std::set<int64_t> scope; | 315 const std::set<int64_t> scope; |
| 311 const leveldb::Status commit_success = leveldb::Status::OK(); | 316 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 312 std::unique_ptr<IndexedDBConnection> connection( | 317 std::unique_ptr<IndexedDBConnection> connection( |
| 313 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 318 base::MakeUnique<IndexedDBConnection>( |
| 319 db_, new MockIndexedDBDatabaseCallbacks())); |
| 314 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 320 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 315 id, connection->GetWeakPtr(), scope, GetParam(), | 321 id, connection->GetWeakPtr(), scope, GetParam(), |
| 316 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 322 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 317 db_->TransactionCreated(transaction.get()); | 323 db_->TransactionCreated(transaction.get()); |
| 318 | 324 |
| 319 // No conflicting transactions, so coordinator will start it immediately: | 325 // No conflicting transactions, so coordinator will start it immediately: |
| 320 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 326 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 321 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 327 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 322 | 328 |
| 323 transaction->ScheduleTask( | 329 transaction->ScheduleTask( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 352 EXPECT_FALSE(transaction->HasPendingTasks()); | 358 EXPECT_FALSE(transaction->HasPendingTasks()); |
| 353 EXPECT_EQ(transaction->diagnostics().tasks_completed, | 359 EXPECT_EQ(transaction->diagnostics().tasks_completed, |
| 354 transaction->diagnostics().tasks_scheduled); | 360 transaction->diagnostics().tasks_scheduled); |
| 355 } | 361 } |
| 356 | 362 |
| 357 TEST_F(IndexedDBTransactionTest, IndexedDBObserver) { | 363 TEST_F(IndexedDBTransactionTest, IndexedDBObserver) { |
| 358 const int64_t id = 0; | 364 const int64_t id = 0; |
| 359 const std::set<int64_t> scope; | 365 const std::set<int64_t> scope; |
| 360 const leveldb::Status commit_success = leveldb::Status::OK(); | 366 const leveldb::Status commit_success = leveldb::Status::OK(); |
| 361 std::unique_ptr<IndexedDBConnection> connection( | 367 std::unique_ptr<IndexedDBConnection> connection( |
| 362 new IndexedDBConnection(db_, new MockIndexedDBDatabaseCallbacks())); | 368 base::MakeUnique<IndexedDBConnection>( |
| 369 db_, new MockIndexedDBDatabaseCallbacks())); |
| 363 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 370 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 364 id, connection->GetWeakPtr(), scope, | 371 id, connection->GetWeakPtr(), scope, |
| 365 blink::WebIDBTransactionModeReadWrite, | 372 blink::WebIDBTransactionModeReadWrite, |
| 366 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 373 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 367 db_->TransactionCreated(transaction.get()); | 374 db_->TransactionCreated(transaction.get()); |
| 368 | 375 |
| 369 EXPECT_EQ(0UL, transaction->pending_observers_.size()); | 376 EXPECT_EQ(0UL, transaction->pending_observers_.size()); |
| 370 EXPECT_EQ(0UL, connection->active_observers().size()); | 377 EXPECT_EQ(0UL, connection->active_observers().size()); |
| 371 | 378 |
| 372 // Add observers to pending observer list. | 379 // Add observers to pending observer list. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 400 | 407 |
| 401 static const blink::WebIDBTransactionMode kTestModes[] = { | 408 static const blink::WebIDBTransactionMode kTestModes[] = { |
| 402 blink::WebIDBTransactionModeReadOnly, blink::WebIDBTransactionModeReadWrite, | 409 blink::WebIDBTransactionModeReadOnly, blink::WebIDBTransactionModeReadWrite, |
| 403 blink::WebIDBTransactionModeVersionChange}; | 410 blink::WebIDBTransactionModeVersionChange}; |
| 404 | 411 |
| 405 INSTANTIATE_TEST_CASE_P(IndexedDBTransactions, | 412 INSTANTIATE_TEST_CASE_P(IndexedDBTransactions, |
| 406 IndexedDBTransactionTestMode, | 413 IndexedDBTransactionTestMode, |
| 407 ::testing::ValuesIn(kTestModes)); | 414 ::testing::ValuesIn(kTestModes)); |
| 408 | 415 |
| 409 } // namespace content | 416 } // namespace content |
| OLD | NEW |