Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: base/task_scheduler/priority_queue_unittest.cc

Issue 1709713002: TaskScheduler [4/9] Priority Queue (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_2_sequence_and_task
Patch Set: self review Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
41 // SimpleThread:
42 void Run() override {
43 scoped_ptr<PriorityQueue::Transaction> transaction =
44 priority_queue_->BeginTransaction();
45 transaction_began_ = true;
46 }
47
48 bool transaction_began() const { return transaction_began_; }
49
50 private:
51 PriorityQueue* const priority_queue_;
52 bool transaction_began_ = false;
53
54 DISALLOW_COPY_AND_ASSIGN(ThreadBeginningTransaction);
55 };
56
57 void ExpectSequenceAndSortKey(const PriorityQueue::SequenceAndSortKey& expected,
58 const PriorityQueue::SequenceAndSortKey& actual) {
59 EXPECT_EQ(expected.sequence, actual.sequence);
60 EXPECT_EQ(expected.sort_key.priority, actual.sort_key.priority);
61 EXPECT_EQ(expected.sort_key.next_task_sequenced_time,
62 actual.sort_key.next_task_sequenced_time);
63 }
64
65 } // namespace
66
67 TEST(TaskSchedulerPriorityQueueTest, PushPopPeek) {
68 // Create test sequences.
69 scoped_refptr<Sequence> sequence_a = new Sequence;
danakj 2016/03/17 21:32:15 use the constructor rather than assigning a raw po
fdoray 2016/03/18 16:06:56 Done.
70 sequence_a->PushTask(make_scoped_ptr(
71 new Task(FROM_HERE, Closure(),
72 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE))));
73 SequenceSortKey sort_key_a = sequence_a->GetSortKey();
74
75 scoped_refptr<Sequence> sequence_b = new Sequence;
76 sequence_b->PushTask(make_scoped_ptr(
77 new Task(FROM_HERE, Closure(),
78 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING))));
79 SequenceSortKey sort_key_b = sequence_b->GetSortKey();
80
81 scoped_refptr<Sequence> sequence_c = new Sequence;
82 sequence_c->PushTask(make_scoped_ptr(
83 new Task(FROM_HERE, Closure(),
84 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING))));
85 SequenceSortKey sort_key_c = sequence_c->GetSortKey();
86
87 scoped_refptr<Sequence> sequence_d = new Sequence;
88 sequence_d->PushTask(make_scoped_ptr(
89 new Task(FROM_HERE, Closure(),
90 TaskTraits().WithPriority(TaskPriority::BACKGROUND))));
91 SequenceSortKey sort_key_d = sequence_d->GetSortKey();
92
93 // Create a PriorityQueue and a Transaction.
94 testing::StrictMock<PriorityQueueCallbackMock> mock;
95 PriorityQueue pq(
96 Bind(&PriorityQueueCallbackMock::SequenceInsertedInPriorityQueue,
97 Unretained(&mock)));
98 scoped_ptr<PriorityQueue::Transaction> transaction(pq.BeginTransaction());
99 ExpectSequenceAndSortKey(PriorityQueue::SequenceAndSortKey(),
danakj 2016/03/17 21:32:15 You can use a SCOPED_TRACE() to make errors show w
fdoray 2016/03/18 16:06:56 Done.
100 transaction->Peek());
101
102 // Push |sequence_a| in the PriorityQueue. It becomes the sequence with the
103 // highest priority.
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 // Push |sequence_b| in the PriorityQueue. It becomes the sequence with the
111 // highest priority.
112 transaction->Push(make_scoped_ptr(
113 new PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b)));
114 ExpectSequenceAndSortKey(
115 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b),
116 transaction->Peek());
117
118 // Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence
119 // with the highest priority.
120 transaction->Push(make_scoped_ptr(
121 new PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c)));
122 ExpectSequenceAndSortKey(
123 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b),
124 transaction->Peek());
125
126 // Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence
127 // with the highest priority.
128 transaction->Push(make_scoped_ptr(
129 new PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d)));
130 ExpectSequenceAndSortKey(
131 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b),
132 transaction->Peek());
133
134 // Pop |sequence_b| from the PriorityQueue. |sequence_c| becomes the sequence
135 // with the highest priority.
136 transaction->Pop();
137 ExpectSequenceAndSortKey(
138 PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c),
139 transaction->Peek());
140
141 // Pop |sequence_c| from the PriorityQueue. |sequence_a| becomes the sequence
142 // with the highest priority.
143 transaction->Pop();
144 ExpectSequenceAndSortKey(
145 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a),
146 transaction->Peek());
147
148 // Pop |sequence_a| from the PriorityQueue. |sequence_d| becomes the sequence
149 // with the highest priority.
150 transaction->Pop();
151 ExpectSequenceAndSortKey(
152 PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d),
153 transaction->Peek());
154
155 // Pop |sequence_d| from the PriorityQueue. It is now empty.
156 transaction->Pop();
157 ExpectSequenceAndSortKey(PriorityQueue::SequenceAndSortKey(),
158 transaction->Peek());
159
160 // Expect 4 calls to mock.SequenceInsertedInPriorityQueue() when the
161 // Transaction is destroyed.
danakj 2016/03/17 21:32:15 Can you VerifyAndClearExpectations(); before the E
fdoray 2016/03/18 16:06:55 This is already covered by using a StrictMock: the
162 EXPECT_CALL(mock, SequenceInsertedInPriorityQueue()).Times(4);
163 transaction.reset();
164 }
165
166 // Check that creating Transactions on the same thread for 2 unrelated
167 // PriorityQueues causes a crash.
168 TEST(TaskSchedulerPriorityQueueTest, IllegalTwoTransactionsSameThread) {
169 PriorityQueue pq_a(Bind(&DoNothing));
170 PriorityQueue pq_b(Bind(&DoNothing));
171
172 EXPECT_DEBUG_DEATH({
robliao 2016/03/17 17:48:10 We'll want to start using EXPECT_DCHECK_DEATH here
fdoray 2016/03/17 19:12:02 Done.
173 scoped_ptr<PriorityQueue::Transaction> transaction_a =
174 pq_a.BeginTransaction();
175 scoped_ptr<PriorityQueue::Transaction> transaction_b =
176 pq_b.BeginTransaction();
177 }, "");
178 }
179
180 // Check that there is no crash when Transactions are created on the same thread
181 // for 2 PriorityQueues which have a predecessor relationship.
182 TEST(TaskSchedulerPriorityQueueTest, LegalTwoTransactionsSameThread) {
183 PriorityQueue pq_a(Bind(&DoNothing));
184 PriorityQueue pq_b(Bind(&DoNothing), &pq_a);
185
186 // This shouldn't crash.
187 scoped_ptr<PriorityQueue::Transaction> transaction_a =
188 pq_a.BeginTransaction();
189 scoped_ptr<PriorityQueue::Transaction> transaction_b =
190 pq_b.BeginTransaction();
191 }
192
193 // Check that it is possible to begin multiple Transactions for the same
194 // PriorityQueue on different threads. The call to BeginTransaction() on the
195 // second thread should block until the Transaction has ended on the first
196 // thread.
197 TEST(TaskSchedulerPriorityQueueTest, TwoTransactionsTwoThreads) {
198 PriorityQueue pq(Bind(&DoNothing));
199
200 // Call BeginTransaction() on this thread and keep the Transaction alive.
201 scoped_ptr<PriorityQueue::Transaction> transaction = pq.BeginTransaction();
202
203 // Call BeginTransaction() on another thread.
204 ThreadBeginningTransaction thread_beginning_transaction(&pq);
205 thread_beginning_transaction.Start();
206
207 // After a few milliseconds, the call to BeginTransaction() on the other
208 // thread should stil not have returned.
danakj 2016/03/17 21:32:15 "still", though the word is superfluous here.
fdoray 2016/03/18 16:06:56 Done.
209 PlatformThread::Sleep(TimeDelta::FromMilliseconds(200));
210 EXPECT_FALSE(thread_beginning_transaction.transaction_began());
danakj 2016/03/17 21:32:15 This is a data race. You need a lock to access thi
fdoray 2016/03/18 16:06:55 I used a WaitableEvent to fix the data race becaus
211
212 // End the Transaction on the current thread.
213 transaction.reset();
214
215 // The other thread should exit after its call to BeginTransaction() returns.
216 thread_beginning_transaction.Join();
217 EXPECT_TRUE(thread_beginning_transaction.transaction_began());
danakj 2016/03/17 21:32:15 ditto
fdoray 2016/03/18 16:06:55 Done.
218 }
219
220 TEST(TaskSchedulerPriorityQueueTest, SequenceAndSortKeyIsNull) {
221 EXPECT_TRUE(PriorityQueue::SequenceAndSortKey().is_null());
222 EXPECT_FALSE(PriorityQueue::SequenceAndSortKey(
223 new Sequence,
danakj 2016/03/17 21:32:15 make_scoped_refptr
fdoray 2016/03/18 16:06:56 Done.
224 SequenceSortKey(TaskPriority::USER_VISIBLE, TimeTicks()))
225 .is_null());
danakj 2016/03/17 21:32:15 consider making the key outside of the EXPECT stat
fdoray 2016/03/18 16:06:55 Done.
226 }
227
228 } // namespace internal
229 } // namespace base
OLDNEW
« base/task_scheduler/priority_queue.cc ('K') | « base/task_scheduler/priority_queue.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698