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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 134 |
135 EXPECT_DCHECK_DEATH( | 135 EXPECT_DCHECK_DEATH( |
136 { | 136 { |
137 std::unique_ptr<PriorityQueue::Transaction> transaction_a = | 137 std::unique_ptr<PriorityQueue::Transaction> transaction_a = |
138 pq_a.BeginTransaction(); | 138 pq_a.BeginTransaction(); |
139 std::unique_ptr<PriorityQueue::Transaction> transaction_b = | 139 std::unique_ptr<PriorityQueue::Transaction> transaction_b = |
140 pq_b.BeginTransaction(); | 140 pq_b.BeginTransaction(); |
141 }); | 141 }); |
142 } | 142 } |
143 | 143 |
144 // Check that there is no crash when Transactions are created on the same thread | |
145 // for 2 PriorityQueues which have a predecessor relationship. | |
146 TEST(TaskSchedulerPriorityQueueTest, LegalTwoTransactionsSameThread) { | |
147 PriorityQueue pq_a; | |
148 PriorityQueue pq_b(&pq_a); | |
149 | |
150 // This shouldn't crash. | |
151 std::unique_ptr<PriorityQueue::Transaction> transaction_a = | |
152 pq_a.BeginTransaction(); | |
153 std::unique_ptr<PriorityQueue::Transaction> transaction_b = | |
154 pq_b.BeginTransaction(); | |
155 } | |
156 | |
157 // Check that it is possible to begin multiple Transactions for the same | 144 // Check that it is possible to begin multiple Transactions for the same |
158 // PriorityQueue on different threads. The call to BeginTransaction() on the | 145 // PriorityQueue on different threads. The call to BeginTransaction() on the |
159 // second thread should block until the Transaction has ended on the first | 146 // second thread should block until the Transaction has ended on the first |
160 // thread. | 147 // thread. |
161 TEST(TaskSchedulerPriorityQueueTest, TwoTransactionsTwoThreads) { | 148 TEST(TaskSchedulerPriorityQueueTest, TwoTransactionsTwoThreads) { |
162 PriorityQueue pq; | 149 PriorityQueue pq; |
163 | 150 |
164 // Call BeginTransaction() on this thread and keep the Transaction alive. | 151 // Call BeginTransaction() on this thread and keep the Transaction alive. |
165 std::unique_ptr<PriorityQueue::Transaction> transaction = | 152 std::unique_ptr<PriorityQueue::Transaction> transaction = |
166 pq.BeginTransaction(); | 153 pq.BeginTransaction(); |
167 | 154 |
168 // Call BeginTransaction() on another thread. | 155 // Call BeginTransaction() on another thread. |
169 ThreadBeginningTransaction thread_beginning_transaction(&pq); | 156 ThreadBeginningTransaction thread_beginning_transaction(&pq); |
170 thread_beginning_transaction.Start(); | 157 thread_beginning_transaction.Start(); |
171 | 158 |
172 // After a few milliseconds, the call to BeginTransaction() on the other | 159 // After a few milliseconds, the call to BeginTransaction() on the other |
173 // thread should not have returned. | 160 // thread should not have returned. |
174 thread_beginning_transaction.ExpectTransactionDoesNotBegin(); | 161 thread_beginning_transaction.ExpectTransactionDoesNotBegin(); |
175 | 162 |
176 // End the Transaction on the current thread. | 163 // End the Transaction on the current thread. |
177 transaction.reset(); | 164 transaction.reset(); |
178 | 165 |
179 // The other thread should exit after its call to BeginTransaction() returns. | 166 // The other thread should exit after its call to BeginTransaction() returns. |
180 thread_beginning_transaction.Join(); | 167 thread_beginning_transaction.Join(); |
181 } | 168 } |
182 | 169 |
183 } // namespace internal | 170 } // namespace internal |
184 } // namespace base | 171 } // namespace base |
OLD | NEW |