| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "base/task_scheduler/priority_queue.h" | 5 #include "base/task_scheduler/priority_queue.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 transaction_began_.TimedWait(TimeDelta::FromMilliseconds(250))); | 45 transaction_began_.TimedWait(TimeDelta::FromMilliseconds(250))); |
| 46 } | 46 } |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 PriorityQueue* const priority_queue_; | 49 PriorityQueue* const priority_queue_; |
| 50 WaitableEvent transaction_began_; | 50 WaitableEvent transaction_began_; |
| 51 | 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(ThreadBeginningTransaction); | 52 DISALLOW_COPY_AND_ASSIGN(ThreadBeginningTransaction); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 void ExpectSequenceAndSortKeyEq( | |
| 56 const PriorityQueue::SequenceAndSortKey& expected, | |
| 57 const PriorityQueue::SequenceAndSortKey& actual) { | |
| 58 EXPECT_EQ(expected.sequence, actual.sequence); | |
| 59 EXPECT_EQ(expected.sort_key.priority, actual.sort_key.priority); | |
| 60 EXPECT_EQ(expected.sort_key.next_task_sequenced_time, | |
| 61 actual.sort_key.next_task_sequenced_time); | |
| 62 } | |
| 63 | |
| 64 #define EXPECT_SEQUENCE_AND_SORT_KEY_EQ(expected, actual) \ | |
| 65 do { \ | |
| 66 SCOPED_TRACE(""); \ | |
| 67 ExpectSequenceAndSortKeyEq(expected, actual); \ | |
| 68 } while (false) | |
| 69 | |
| 70 } // namespace | 55 } // namespace |
| 71 | 56 |
| 72 TEST(TaskSchedulerPriorityQueueTest, PushPopPeek) { | 57 TEST(TaskSchedulerPriorityQueueTest, PushPopPeek) { |
| 73 // Create test sequences. | 58 // Create test sequences. |
| 74 scoped_refptr<Sequence> sequence_a(new Sequence); | 59 scoped_refptr<Sequence> sequence_a(new Sequence); |
| 75 sequence_a->PushTask(WrapUnique(new Task( | 60 sequence_a->PushTask(WrapUnique(new Task( |
| 76 FROM_HERE, Closure(), | 61 FROM_HERE, Closure(), |
| 77 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), TimeDelta()))); | 62 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), TimeDelta()))); |
| 78 SequenceSortKey sort_key_a = sequence_a->GetSortKey(); | 63 SequenceSortKey sort_key_a = sequence_a->GetSortKey(); |
| 79 | 64 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 91 | 76 |
| 92 scoped_refptr<Sequence> sequence_d(new Sequence); | 77 scoped_refptr<Sequence> sequence_d(new Sequence); |
| 93 sequence_d->PushTask(WrapUnique(new Task( | 78 sequence_d->PushTask(WrapUnique(new Task( |
| 94 FROM_HERE, Closure(), TaskTraits().WithPriority(TaskPriority::BACKGROUND), | 79 FROM_HERE, Closure(), TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 95 TimeDelta()))); | 80 TimeDelta()))); |
| 96 SequenceSortKey sort_key_d = sequence_d->GetSortKey(); | 81 SequenceSortKey sort_key_d = sequence_d->GetSortKey(); |
| 97 | 82 |
| 98 // Create a PriorityQueue and a Transaction. | 83 // Create a PriorityQueue and a Transaction. |
| 99 PriorityQueue pq; | 84 PriorityQueue pq; |
| 100 auto transaction(pq.BeginTransaction()); | 85 auto transaction(pq.BeginTransaction()); |
| 101 EXPECT_SEQUENCE_AND_SORT_KEY_EQ(PriorityQueue::SequenceAndSortKey(), | 86 EXPECT_TRUE(transaction->IsEmpty()); |
| 102 transaction->Peek()); | |
| 103 | 87 |
| 104 // Push |sequence_a| in the PriorityQueue. It becomes the sequence with the | 88 // Push |sequence_a| in the PriorityQueue. It becomes the sequence with the |
| 105 // highest priority. | 89 // highest priority. |
| 106 transaction->Push(WrapUnique( | 90 transaction->Push(sequence_a, sort_key_a); |
| 107 new PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a))); | 91 EXPECT_EQ(sort_key_a, transaction->PeekSortKey()); |
| 108 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | |
| 109 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a), | |
| 110 transaction->Peek()); | |
| 111 | 92 |
| 112 // Push |sequence_b| in the PriorityQueue. It becomes the sequence with the | 93 // Push |sequence_b| in the PriorityQueue. It becomes the sequence with the |
| 113 // highest priority. | 94 // highest priority. |
| 114 transaction->Push(WrapUnique( | 95 transaction->Push(sequence_b, sort_key_b); |
| 115 new PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b))); | 96 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); |
| 116 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | |
| 117 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
| 118 transaction->Peek()); | |
| 119 | 97 |
| 120 // Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence | 98 // Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence |
| 121 // with the highest priority. | 99 // with the highest priority. |
| 122 transaction->Push(WrapUnique( | 100 transaction->Push(sequence_c, sort_key_c); |
| 123 new PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c))); | 101 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); |
| 124 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | |
| 125 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
| 126 transaction->Peek()); | |
| 127 | 102 |
| 128 // Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence | 103 // Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence |
| 129 // with the highest priority. | 104 // with the highest priority. |
| 130 transaction->Push(WrapUnique( | 105 transaction->Push(sequence_d, sort_key_d); |
| 131 new PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d))); | 106 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); |
| 132 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | |
| 133 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
| 134 transaction->Peek()); | |
| 135 | 107 |
| 136 // Pop |sequence_b| from the PriorityQueue. |sequence_c| becomes the sequence | 108 // Pop |sequence_b| from the PriorityQueue. |sequence_c| becomes the sequence |
| 137 // with the highest priority. | 109 // with the highest priority. |
| 138 transaction->Pop(); | 110 EXPECT_EQ(sequence_b, transaction->PopSequence()); |
| 139 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | 111 EXPECT_EQ(sort_key_c, transaction->PeekSortKey()); |
| 140 PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c), | |
| 141 transaction->Peek()); | |
| 142 | 112 |
| 143 // Pop |sequence_c| from the PriorityQueue. |sequence_a| becomes the sequence | 113 // Pop |sequence_c| from the PriorityQueue. |sequence_a| becomes the sequence |
| 144 // with the highest priority. | 114 // with the highest priority. |
| 145 transaction->Pop(); | 115 EXPECT_EQ(sequence_c, transaction->PopSequence()); |
| 146 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | 116 EXPECT_EQ(sort_key_a, transaction->PeekSortKey()); |
| 147 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a), | |
| 148 transaction->Peek()); | |
| 149 | 117 |
| 150 // Pop |sequence_a| from the PriorityQueue. |sequence_d| becomes the sequence | 118 // Pop |sequence_a| from the PriorityQueue. |sequence_d| becomes the sequence |
| 151 // with the highest priority. | 119 // with the highest priority. |
| 152 transaction->Pop(); | 120 EXPECT_EQ(sequence_a, transaction->PopSequence()); |
| 153 EXPECT_SEQUENCE_AND_SORT_KEY_EQ( | 121 EXPECT_EQ(sort_key_d, transaction->PeekSortKey()); |
| 154 PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d), | |
| 155 transaction->Peek()); | |
| 156 | 122 |
| 157 // Pop |sequence_d| from the PriorityQueue. It is now empty. | 123 // Pop |sequence_d| from the PriorityQueue. It is now empty. |
| 158 transaction->Pop(); | 124 EXPECT_EQ(sequence_d, transaction->PopSequence()); |
| 159 EXPECT_SEQUENCE_AND_SORT_KEY_EQ(PriorityQueue::SequenceAndSortKey(), | 125 EXPECT_TRUE(transaction->IsEmpty()); |
| 160 transaction->Peek()); | |
| 161 } | 126 } |
| 162 | 127 |
| 163 // Check that creating Transactions on the same thread for 2 unrelated | 128 // Check that creating Transactions on the same thread for 2 unrelated |
| 164 // PriorityQueues causes a crash. | 129 // PriorityQueues causes a crash. |
| 165 TEST(TaskSchedulerPriorityQueueTest, IllegalTwoTransactionsSameThread) { | 130 TEST(TaskSchedulerPriorityQueueTest, IllegalTwoTransactionsSameThread) { |
| 166 PriorityQueue pq_a; | 131 PriorityQueue pq_a; |
| 167 PriorityQueue pq_b; | 132 PriorityQueue pq_b; |
| 168 | 133 |
| 169 EXPECT_DCHECK_DEATH( | 134 EXPECT_DCHECK_DEATH( |
| 170 { | 135 { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // thread should not have returned. | 173 // thread should not have returned. |
| 209 thread_beginning_transaction.ExpectTransactionDoesNotBegin(); | 174 thread_beginning_transaction.ExpectTransactionDoesNotBegin(); |
| 210 | 175 |
| 211 // End the Transaction on the current thread. | 176 // End the Transaction on the current thread. |
| 212 transaction.reset(); | 177 transaction.reset(); |
| 213 | 178 |
| 214 // The other thread should exit after its call to BeginTransaction() returns. | 179 // The other thread should exit after its call to BeginTransaction() returns. |
| 215 thread_beginning_transaction.Join(); | 180 thread_beginning_transaction.Join(); |
| 216 } | 181 } |
| 217 | 182 |
| 218 TEST(TaskSchedulerPriorityQueueTest, SequenceAndSortKeyIsNull) { | |
| 219 EXPECT_TRUE(PriorityQueue::SequenceAndSortKey().is_null()); | |
| 220 | |
| 221 const PriorityQueue::SequenceAndSortKey non_null_sequence_and_sort_key( | |
| 222 make_scoped_refptr(new Sequence), | |
| 223 SequenceSortKey(TaskPriority::USER_VISIBLE, TimeTicks())); | |
| 224 EXPECT_FALSE(non_null_sequence_and_sort_key.is_null()); | |
| 225 } | |
| 226 | |
| 227 } // namespace internal | 183 } // namespace internal |
| 228 } // namespace base | 184 } // namespace base |
| OLD | NEW |