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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 scoped_refptr<Sequence> sequence_d(new Sequence); | 80 scoped_refptr<Sequence> sequence_d(new Sequence); |
81 sequence_d->PushTask(MakeUnique<Task>( | 81 sequence_d->PushTask(MakeUnique<Task>( |
82 FROM_HERE, Bind(&DoNothing), | 82 FROM_HERE, Bind(&DoNothing), |
83 TaskTraits().WithPriority(TaskPriority::BACKGROUND), TimeDelta())); | 83 TaskTraits().WithPriority(TaskPriority::BACKGROUND), TimeDelta())); |
84 SequenceSortKey sort_key_d = sequence_d->GetSortKey(); | 84 SequenceSortKey sort_key_d = sequence_d->GetSortKey(); |
85 | 85 |
86 // Create a PriorityQueue and a Transaction. | 86 // Create a PriorityQueue and a Transaction. |
87 PriorityQueue pq; | 87 PriorityQueue pq; |
88 auto transaction(pq.BeginTransaction()); | 88 auto transaction(pq.BeginTransaction()); |
89 EXPECT_TRUE(transaction->IsEmpty()); | 89 EXPECT_TRUE(transaction->IsEmpty()); |
| 90 EXPECT_EQ(0U, transaction->Size()); |
90 | 91 |
91 // Push |sequence_a| in the PriorityQueue. It becomes the sequence with the | 92 // Push |sequence_a| in the PriorityQueue. It becomes the sequence with the |
92 // highest priority. | 93 // highest priority. |
93 transaction->Push(sequence_a, sort_key_a); | 94 transaction->Push(sequence_a, sort_key_a); |
94 EXPECT_EQ(sort_key_a, transaction->PeekSortKey()); | 95 EXPECT_EQ(sort_key_a, transaction->PeekSortKey()); |
| 96 EXPECT_EQ(1U, transaction->Size()); |
95 | 97 |
96 // Push |sequence_b| in the PriorityQueue. It becomes the sequence with the | 98 // Push |sequence_b| in the PriorityQueue. It becomes the sequence with the |
97 // highest priority. | 99 // highest priority. |
98 transaction->Push(sequence_b, sort_key_b); | 100 transaction->Push(sequence_b, sort_key_b); |
99 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); | 101 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); |
| 102 EXPECT_EQ(2U, transaction->Size()); |
100 | 103 |
101 // Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence | 104 // Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence |
102 // with the highest priority. | 105 // with the highest priority. |
103 transaction->Push(sequence_c, sort_key_c); | 106 transaction->Push(sequence_c, sort_key_c); |
104 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); | 107 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); |
| 108 EXPECT_EQ(3U, transaction->Size()); |
105 | 109 |
106 // Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence | 110 // Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence |
107 // with the highest priority. | 111 // with the highest priority. |
108 transaction->Push(sequence_d, sort_key_d); | 112 transaction->Push(sequence_d, sort_key_d); |
109 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); | 113 EXPECT_EQ(sort_key_b, transaction->PeekSortKey()); |
| 114 EXPECT_EQ(4U, transaction->Size()); |
110 | 115 |
111 // Pop |sequence_b| from the PriorityQueue. |sequence_c| becomes the sequence | 116 // Pop |sequence_b| from the PriorityQueue. |sequence_c| becomes the sequence |
112 // with the highest priority. | 117 // with the highest priority. |
113 EXPECT_EQ(sequence_b, transaction->PopSequence()); | 118 EXPECT_EQ(sequence_b, transaction->PopSequence()); |
114 EXPECT_EQ(sort_key_c, transaction->PeekSortKey()); | 119 EXPECT_EQ(sort_key_c, transaction->PeekSortKey()); |
| 120 EXPECT_EQ(3U, transaction->Size()); |
115 | 121 |
116 // Pop |sequence_c| from the PriorityQueue. |sequence_a| becomes the sequence | 122 // Pop |sequence_c| from the PriorityQueue. |sequence_a| becomes the sequence |
117 // with the highest priority. | 123 // with the highest priority. |
118 EXPECT_EQ(sequence_c, transaction->PopSequence()); | 124 EXPECT_EQ(sequence_c, transaction->PopSequence()); |
119 EXPECT_EQ(sort_key_a, transaction->PeekSortKey()); | 125 EXPECT_EQ(sort_key_a, transaction->PeekSortKey()); |
| 126 EXPECT_EQ(2U, transaction->Size()); |
120 | 127 |
121 // Pop |sequence_a| from the PriorityQueue. |sequence_d| becomes the sequence | 128 // Pop |sequence_a| from the PriorityQueue. |sequence_d| becomes the sequence |
122 // with the highest priority. | 129 // with the highest priority. |
123 EXPECT_EQ(sequence_a, transaction->PopSequence()); | 130 EXPECT_EQ(sequence_a, transaction->PopSequence()); |
124 EXPECT_EQ(sort_key_d, transaction->PeekSortKey()); | 131 EXPECT_EQ(sort_key_d, transaction->PeekSortKey()); |
| 132 EXPECT_EQ(1U, transaction->Size()); |
125 | 133 |
126 // Pop |sequence_d| from the PriorityQueue. It is now empty. | 134 // Pop |sequence_d| from the PriorityQueue. It is now empty. |
127 EXPECT_EQ(sequence_d, transaction->PopSequence()); | 135 EXPECT_EQ(sequence_d, transaction->PopSequence()); |
128 EXPECT_TRUE(transaction->IsEmpty()); | 136 EXPECT_TRUE(transaction->IsEmpty()); |
| 137 EXPECT_EQ(0U, transaction->Size()); |
129 } | 138 } |
130 | 139 |
131 // Check that creating Transactions on the same thread for 2 unrelated | 140 // Check that creating Transactions on the same thread for 2 unrelated |
132 // PriorityQueues causes a crash. | 141 // PriorityQueues causes a crash. |
133 TEST(TaskSchedulerPriorityQueueTest, IllegalTwoTransactionsSameThread) { | 142 TEST(TaskSchedulerPriorityQueueTest, IllegalTwoTransactionsSameThread) { |
134 PriorityQueue pq_a; | 143 PriorityQueue pq_a; |
135 PriorityQueue pq_b; | 144 PriorityQueue pq_b; |
136 | 145 |
137 EXPECT_DCHECK_DEATH( | 146 EXPECT_DCHECK_DEATH( |
138 { | 147 { |
(...skipping 25 matching lines...) Expand all Loading... |
164 | 173 |
165 // End the Transaction on the current thread. | 174 // End the Transaction on the current thread. |
166 transaction.reset(); | 175 transaction.reset(); |
167 | 176 |
168 // The other thread should exit after its call to BeginTransaction() returns. | 177 // The other thread should exit after its call to BeginTransaction() returns. |
169 thread_beginning_transaction.Join(); | 178 thread_beginning_transaction.Join(); |
170 } | 179 } |
171 | 180 |
172 } // namespace internal | 181 } // namespace internal |
173 } // namespace base | 182 } // namespace base |
OLD | NEW |