Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/task_scheduler/priority_queue.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/task_scheduler/sequence.h" | |
| 13 #include "base/task_scheduler/task.h" | |
| 14 #include "base/task_scheduler/task_traits.h" | |
| 15 #include "base/threading/platform_thread.h" | |
| 16 #include "base/threading/simple_thread.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace base { | |
| 22 namespace internal { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class PriorityQueueCallbackMock { | |
| 27 public: | |
| 28 PriorityQueueCallbackMock() = default; | |
| 29 MOCK_METHOD0(SequenceInsertedInPriorityQueue, void()); | |
| 30 | |
| 31 private: | |
| 32 DISALLOW_COPY_AND_ASSIGN(PriorityQueueCallbackMock); | |
| 33 }; | |
| 34 | |
| 35 class ThreadBeginningTransaction : public SimpleThread { | |
| 36 public: | |
| 37 explicit ThreadBeginningTransaction(PriorityQueue* priority_queue) | |
| 38 : SimpleThread("ThreadBeginningTransaction"), | |
| 39 priority_queue_(priority_queue), | |
| 40 transaction_began_(false) {} | |
| 41 | |
| 42 // SimpleThread: | |
| 43 void Run() override { | |
| 44 scoped_ptr<PriorityQueue::Transaction> transaction = | |
| 45 priority_queue_->BeginTransaction(); | |
| 46 transaction_began_ = true; | |
| 47 } | |
| 48 | |
| 49 bool transaction_began() const { return transaction_began_; } | |
| 50 | |
| 51 private: | |
| 52 PriorityQueue* const priority_queue_; | |
| 53 bool transaction_began_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(ThreadBeginningTransaction); | |
| 56 }; | |
| 57 | |
| 58 void ExpectSequenceAndSortKey(const PriorityQueue::SequenceAndSortKey& expected, | |
| 59 const PriorityQueue::SequenceAndSortKey& actual) { | |
| 60 EXPECT_EQ(expected.sequence, actual.sequence); | |
| 61 EXPECT_EQ(expected.sort_key.priority, actual.sort_key.priority); | |
| 62 EXPECT_EQ(expected.sort_key.next_task_sequenced_time, | |
| 63 actual.sort_key.next_task_sequenced_time); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 TEST(TaskSchedulerPriorityQueueTest, PushPopPeek) { | |
| 69 // Create test sequences. | |
| 70 scoped_refptr<Sequence> sequence_a = new Sequence; | |
| 71 sequence_a->PushTask(make_scoped_ptr( | |
| 72 new Task(FROM_HERE, Closure(), | |
| 73 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE)))); | |
| 74 SequenceSortKey sort_key_a = sequence_a->GetSortKey(); | |
| 75 | |
| 76 scoped_refptr<Sequence> sequence_b = new Sequence; | |
| 77 sequence_b->PushTask(make_scoped_ptr( | |
| 78 new Task(FROM_HERE, Closure(), | |
| 79 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING)))); | |
| 80 SequenceSortKey sort_key_b = sequence_b->GetSortKey(); | |
| 81 | |
| 82 scoped_refptr<Sequence> sequence_c = new Sequence; | |
| 83 sequence_c->PushTask(make_scoped_ptr( | |
| 84 new Task(FROM_HERE, Closure(), | |
| 85 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING)))); | |
| 86 SequenceSortKey sort_key_c = sequence_c->GetSortKey(); | |
| 87 | |
| 88 scoped_refptr<Sequence> sequence_d = new Sequence; | |
| 89 sequence_d->PushTask(make_scoped_ptr( | |
| 90 new Task(FROM_HERE, Closure(), | |
| 91 TaskTraits().WithPriority(TaskPriority::BACKGROUND)))); | |
| 92 SequenceSortKey sort_key_d = sequence_d->GetSortKey(); | |
| 93 | |
| 94 // Create a priority queue and a transaction. | |
| 95 testing::StrictMock<PriorityQueueCallbackMock> mock; | |
| 96 PriorityQueue pq( | |
| 97 Bind(&PriorityQueueCallbackMock::SequenceInsertedInPriorityQueue, | |
| 98 Unretained(&mock))); | |
| 99 scoped_ptr<PriorityQueue::Transaction> transaction(pq.BeginTransaction()); | |
| 100 ExpectSequenceAndSortKey(PriorityQueue::SequenceAndSortKey(), | |
| 101 transaction->Peek()); | |
| 102 | |
| 103 // Push sequences in the priority queue. | |
| 104 transaction->Push(make_scoped_ptr( | |
| 105 new PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a))); | |
| 106 ExpectSequenceAndSortKey( | |
| 107 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a), | |
| 108 transaction->Peek()); | |
| 109 | |
| 110 transaction->Push(make_scoped_ptr( | |
| 111 new PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b))); | |
| 112 ExpectSequenceAndSortKey( | |
| 113 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
| 114 transaction->Peek()); | |
| 115 | |
| 116 transaction->Push(make_scoped_ptr( | |
| 117 new PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c))); | |
| 118 ExpectSequenceAndSortKey( | |
| 119 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
| 120 transaction->Peek()); | |
| 121 | |
| 122 transaction->Push(make_scoped_ptr( | |
| 123 new PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d))); | |
| 124 ExpectSequenceAndSortKey( | |
| 125 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
| 126 transaction->Peek()); | |
| 127 | |
| 128 // Pop sequences from the priority queue. | |
| 129 transaction->Pop(); | |
| 130 ExpectSequenceAndSortKey( | |
| 131 PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c), | |
| 132 transaction->Peek()); | |
| 133 | |
| 134 transaction->Pop(); | |
| 135 ExpectSequenceAndSortKey( | |
| 136 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a), | |
| 137 transaction->Peek()); | |
| 138 | |
| 139 transaction->Pop(); | |
| 140 ExpectSequenceAndSortKey( | |
| 141 PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d), | |
| 142 transaction->Peek()); | |
| 143 | |
| 144 transaction->Pop(); | |
| 145 ExpectSequenceAndSortKey(PriorityQueue::SequenceAndSortKey(), | |
| 146 transaction->Peek()); | |
| 147 | |
| 148 // Expect 4 calls to mock.SequenceInsertedInPriorityQueue() when the | |
| 149 // Transaction is destroyed. | |
| 150 EXPECT_CALL(mock, SequenceInsertedInPriorityQueue()).Times(4); | |
| 151 transaction.reset(); | |
| 152 } | |
| 153 | |
| 154 // Check that creating Transactions on the same thread for 2 unrelated | |
| 155 // PriorityQueues causes a crash. | |
| 156 TEST(TaskSchedulerPriorityQueueTest, IllegalTwoTransactionsSameThread) { | |
| 157 PriorityQueue pq_a(Bind(&DoNothing)); | |
| 158 PriorityQueue pq_b(Bind(&DoNothing)); | |
| 159 | |
| 160 EXPECT_DEBUG_DEATH({ | |
| 161 scoped_ptr<PriorityQueue::Transaction> transaction_a = | |
| 162 pq_a.BeginTransaction(); | |
| 163 scoped_ptr<PriorityQueue::Transaction> transaction_b = | |
| 164 pq_b.BeginTransaction(); | |
| 165 }, ""); | |
| 166 } | |
| 167 | |
| 168 // Check that there is no crash when Transactions are created on the same thread | |
| 169 // for 2 PriorityQueues which have a predecessor relationship. | |
| 170 TEST(TaskSchedulerPriorityQueueTest, LegalTwoTransactionsSameThread) { | |
| 171 PriorityQueue pq_a(Bind(&DoNothing)); | |
| 172 PriorityQueue pq_b(Bind(&DoNothing), &pq_a); | |
| 173 | |
| 174 // This shouldn't crash. | |
| 175 scoped_ptr<PriorityQueue::Transaction> transaction_a = | |
| 176 pq_a.BeginTransaction(); | |
| 177 scoped_ptr<PriorityQueue::Transaction> transaction_b = | |
| 178 pq_b.BeginTransaction(); | |
| 179 } | |
| 180 | |
| 181 // Check that it is possible to begin multiple Transactions for the same | |
| 182 // PriorityQueue on different threads. The call to BeginTransaction() on the | |
| 183 // second thread should block until the Transaction has ended on the first | |
| 184 // thread. | |
| 185 TEST(TaskSchedulerPriorityQueueTest, TwoTransactionsTwoThreads) { | |
| 186 PriorityQueue pq(Bind(&DoNothing)); | |
| 187 | |
| 188 // Call BeginTransaction() on this thread and keep the Transaction alive. | |
| 189 scoped_ptr<PriorityQueue::Transaction> transaction = pq.BeginTransaction(); | |
| 190 | |
| 191 // Call BeginTransaction() on another thread. | |
| 192 ThreadBeginningTransaction thread_beginning_transaction(&pq); | |
| 193 thread_beginning_transaction.Start(); | |
| 194 | |
| 195 // After a few milliseconds, the call to BeginTransaction() on the other | |
| 196 // thread should stil not have returned. | |
| 197 PlatformThread::Sleep(TimeDelta::FromMilliseconds(200)); | |
| 198 EXPECT_FALSE(thread_beginning_transaction.transaction_began()); | |
| 199 | |
| 200 // End the Transaction on the current thread. | |
| 201 transaction.reset(); | |
| 202 | |
| 203 // The other thread should exit after its call to BeginTransaction() returns. | |
| 204 thread_beginning_transaction.Join(); | |
| 205 EXPECT_TRUE(thread_beginning_transaction.transaction_began()); | |
| 206 } | |
| 207 | |
| 208 TEST(TaskSchedulerPriorityQueueTest, SequenceAndSortKeyIsNull) { | |
| 209 EXPECT_TRUE(PriorityQueue::SequenceAndSortKey().is_null()); | |
| 210 | |
| 211 scoped_refptr<Sequence> sequence = new Sequence; | |
| 212 sequence->PushTask(make_scoped_ptr( | |
| 213 new Task(FROM_HERE, Closure(), | |
| 214 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE)))); | |
|
gab
2016/02/24 18:07:38
Does the sequence need to have a task in it for th
fdoray
2016/02/24 18:18:42
Done.
| |
| 215 EXPECT_FALSE( | |
| 216 PriorityQueue::SequenceAndSortKey(sequence, sequence->GetSortKey()) | |
| 217 .is_null()); | |
| 218 } | |
| 219 | |
| 220 } // namespace internal | |
| 221 } // namespace base | |
| OLD | NEW |