OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 // triggered. This is a regression test for http://crbug.com/117469. | 503 // triggered. This is a regression test for http://crbug.com/117469. |
504 TEST_F(SequencedWorkerPoolTest, SpuriousWorkSignal) { | 504 TEST_F(SequencedWorkerPoolTest, SpuriousWorkSignal) { |
505 EnsureAllWorkersCreated(); | 505 EnsureAllWorkersCreated(); |
506 int old_has_work_call_count = has_work_call_count(); | 506 int old_has_work_call_count = has_work_call_count(); |
507 pool()->SignalHasWorkForTesting(); | 507 pool()->SignalHasWorkForTesting(); |
508 // This is inherently racy, but can only produce false positives. | 508 // This is inherently racy, but can only produce false positives. |
509 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 509 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
510 EXPECT_EQ(old_has_work_call_count + 1, has_work_call_count()); | 510 EXPECT_EQ(old_has_work_call_count + 1, has_work_call_count()); |
511 } | 511 } |
512 | 512 |
| 513 void IsRunningOnCurrentThreadTask( |
| 514 SequencedWorkerPool::SequenceToken test_positive_token, |
| 515 SequencedWorkerPool::SequenceToken test_negative_token, |
| 516 SequencedWorkerPool* pool, |
| 517 SequencedWorkerPool* unused_pool) { |
| 518 EXPECT_TRUE(pool->RunsTasksOnCurrentThread()); |
| 519 EXPECT_TRUE(pool->IsRunningSequenceOnCurrentThread(test_positive_token)); |
| 520 EXPECT_FALSE(pool->IsRunningSequenceOnCurrentThread(test_negative_token)); |
| 521 EXPECT_FALSE(unused_pool->RunsTasksOnCurrentThread()); |
| 522 EXPECT_FALSE( |
| 523 unused_pool->IsRunningSequenceOnCurrentThread(test_positive_token)); |
| 524 EXPECT_FALSE( |
| 525 unused_pool->IsRunningSequenceOnCurrentThread(test_negative_token)); |
| 526 } |
| 527 |
| 528 // Verify correctness of the IsRunningSequenceOnCurrentThread method. |
| 529 TEST_F(SequencedWorkerPoolTest, IsRunningOnCurrentThread) { |
| 530 SequencedWorkerPool::SequenceToken token1 = pool()->GetSequenceToken(); |
| 531 SequencedWorkerPool::SequenceToken token2 = pool()->GetSequenceToken(); |
| 532 SequencedWorkerPool::SequenceToken unsequenced_token; |
| 533 |
| 534 scoped_refptr<SequencedWorkerPool> unused_pool = |
| 535 new SequencedWorkerPool(2, "unused_pool"); |
| 536 EXPECT_TRUE(token1.Equals(unused_pool->GetSequenceToken())); |
| 537 EXPECT_TRUE(token2.Equals(unused_pool->GetSequenceToken())); |
| 538 |
| 539 EXPECT_FALSE(pool()->RunsTasksOnCurrentThread()); |
| 540 EXPECT_FALSE(pool()->IsRunningSequenceOnCurrentThread(token1)); |
| 541 EXPECT_FALSE(pool()->IsRunningSequenceOnCurrentThread(token2)); |
| 542 EXPECT_FALSE(pool()->IsRunningSequenceOnCurrentThread(unsequenced_token)); |
| 543 EXPECT_FALSE(unused_pool->RunsTasksOnCurrentThread()); |
| 544 EXPECT_FALSE(unused_pool->IsRunningSequenceOnCurrentThread(token1)); |
| 545 EXPECT_FALSE(unused_pool->IsRunningSequenceOnCurrentThread(token2)); |
| 546 EXPECT_FALSE( |
| 547 unused_pool->IsRunningSequenceOnCurrentThread(unsequenced_token)); |
| 548 |
| 549 pool()->PostSequencedWorkerTask( |
| 550 token1, FROM_HERE, |
| 551 base::Bind(&IsRunningOnCurrentThreadTask, |
| 552 token1, token2, pool(), unused_pool)); |
| 553 pool()->PostSequencedWorkerTask( |
| 554 token2, FROM_HERE, |
| 555 base::Bind(&IsRunningOnCurrentThreadTask, |
| 556 token2, unsequenced_token, pool(), unused_pool)); |
| 557 pool()->PostWorkerTask( |
| 558 FROM_HERE, |
| 559 base::Bind(&IsRunningOnCurrentThreadTask, |
| 560 unsequenced_token, token1, pool(), unused_pool)); |
| 561 pool()->Shutdown(); |
| 562 unused_pool->Shutdown(); |
| 563 } |
| 564 |
513 class SequencedWorkerPoolTaskRunnerTestDelegate { | 565 class SequencedWorkerPoolTaskRunnerTestDelegate { |
514 public: | 566 public: |
515 SequencedWorkerPoolTaskRunnerTestDelegate() {} | 567 SequencedWorkerPoolTaskRunnerTestDelegate() {} |
516 | 568 |
517 ~SequencedWorkerPoolTaskRunnerTestDelegate() {} | 569 ~SequencedWorkerPoolTaskRunnerTestDelegate() {} |
518 | 570 |
519 void StartTaskRunner() { | 571 void StartTaskRunner() { |
520 pool_owner_.reset( | 572 pool_owner_.reset( |
521 new SequencedWorkerPoolOwner(10, "SequencedWorkerPoolTaskRunnerTest")); | 573 new SequencedWorkerPoolOwner(10, "SequencedWorkerPoolTaskRunnerTest")); |
522 } | 574 } |
(...skipping 19 matching lines...) Expand all Loading... |
542 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; | 594 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; |
543 }; | 595 }; |
544 | 596 |
545 INSTANTIATE_TYPED_TEST_CASE_P( | 597 INSTANTIATE_TYPED_TEST_CASE_P( |
546 SequencedWorkerPool, TaskRunnerTest, | 598 SequencedWorkerPool, TaskRunnerTest, |
547 SequencedWorkerPoolTaskRunnerTestDelegate); | 599 SequencedWorkerPoolTaskRunnerTestDelegate); |
548 | 600 |
549 } // namespace | 601 } // namespace |
550 | 602 |
551 } // namespace base | 603 } // namespace base |
OLD | NEW |