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/time/time.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace base { | |
20 namespace internal { | |
21 | |
22 namespace { | |
23 | |
24 class PriorityQueueCallbackMock { | |
25 public: | |
26 PriorityQueueCallbackMock() = default; | |
27 MOCK_METHOD0(SequenceInsertedInPriorityQueue, void()); | |
28 | |
29 private: | |
30 DISALLOW_COPY_AND_ASSIGN(PriorityQueueCallbackMock); | |
31 }; | |
32 | |
33 void ExpectSequenceAndSortKey(const PriorityQueue::SequenceAndSortKey& expected, | |
34 const PriorityQueue::SequenceAndSortKey& actual) { | |
35 EXPECT_EQ(expected.sequence, actual.sequence); | |
36 EXPECT_EQ(expected.sort_key.priority, actual.sort_key.priority); | |
37 EXPECT_EQ(expected.sort_key.next_task_sequenced_time, | |
38 actual.sort_key.next_task_sequenced_time); | |
39 EXPECT_EQ(expected.is_null(), actual.is_null()); | |
gab
2016/02/23 20:58:42
Don't think this is necessary. Since is_null() is
fdoray
2016/02/24 15:03:53
It tested the implementation of is_null(). I creat
| |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 // Check the behavior of the Push, Pop and Peek methods of | |
gab
2016/02/23 20:58:42
s/Check/Verifies/
(and overall I think the test b
fdoray
2016/02/24 15:03:53
Done. Removed the comment.
| |
45 // PriorityQueue::Transaction. | |
46 TEST(TaskSchedulerPriorityQueueTest, PushPopPeek) { | |
47 // Create test sequences. | |
48 scoped_refptr<Sequence> sequence_a = new Sequence; | |
49 sequence_a->PushTask(make_scoped_ptr( | |
50 new Task(FROM_HERE, Closure(), | |
51 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE)))); | |
52 SequenceSortKey sort_key_a = sequence_a->GetSortKey(); | |
53 | |
54 scoped_refptr<Sequence> sequence_b = new Sequence; | |
55 sequence_b->PushTask(make_scoped_ptr( | |
56 new Task(FROM_HERE, Closure(), | |
57 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING)))); | |
58 SequenceSortKey sort_key_b = sequence_b->GetSortKey(); | |
59 | |
60 scoped_refptr<Sequence> sequence_c = new Sequence; | |
61 sequence_c->PushTask(make_scoped_ptr( | |
62 new Task(FROM_HERE, Closure(), | |
63 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING)))); | |
64 SequenceSortKey sort_key_c = sequence_c->GetSortKey(); | |
65 | |
66 scoped_refptr<Sequence> sequence_d = new Sequence; | |
67 sequence_d->PushTask(make_scoped_ptr( | |
68 new Task(FROM_HERE, Closure(), | |
69 TaskTraits().WithPriority(TaskPriority::BACKGROUND)))); | |
70 SequenceSortKey sort_key_d = sequence_d->GetSortKey(); | |
71 | |
72 // Create a priority queue and a transaction. | |
73 testing::StrictMock<PriorityQueueCallbackMock> mock; | |
74 PriorityQueue pq( | |
75 Bind(&PriorityQueueCallbackMock::SequenceInsertedInPriorityQueue, | |
76 Unretained(&mock))); | |
77 scoped_ptr<PriorityQueue::Transaction> transaction(pq.BeginTransaction()); | |
78 ExpectSequenceAndSortKey(PriorityQueue::SequenceAndSortKey::Null(), | |
79 transaction->Peek()); | |
80 | |
81 // Push sequences in the priority queue. | |
82 transaction->Push(make_scoped_ptr( | |
83 new PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a))); | |
84 ExpectSequenceAndSortKey( | |
85 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a), | |
86 transaction->Peek()); | |
87 | |
88 transaction->Push(make_scoped_ptr( | |
89 new PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b))); | |
90 ExpectSequenceAndSortKey( | |
91 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
92 transaction->Peek()); | |
93 | |
94 transaction->Push(make_scoped_ptr( | |
95 new PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c))); | |
96 ExpectSequenceAndSortKey( | |
97 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
98 transaction->Peek()); | |
99 | |
100 transaction->Push(make_scoped_ptr( | |
101 new PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d))); | |
102 ExpectSequenceAndSortKey( | |
103 PriorityQueue::SequenceAndSortKey(sequence_b, sort_key_b), | |
104 transaction->Peek()); | |
105 | |
106 // Pop sequences from the priority queue. | |
107 transaction->Pop(); | |
108 ExpectSequenceAndSortKey( | |
109 PriorityQueue::SequenceAndSortKey(sequence_c, sort_key_c), | |
110 transaction->Peek()); | |
111 | |
112 transaction->Pop(); | |
113 ExpectSequenceAndSortKey( | |
114 PriorityQueue::SequenceAndSortKey(sequence_a, sort_key_a), | |
115 transaction->Peek()); | |
116 | |
117 transaction->Pop(); | |
118 ExpectSequenceAndSortKey( | |
119 PriorityQueue::SequenceAndSortKey(sequence_d, sort_key_d), | |
120 transaction->Peek()); | |
121 | |
122 transaction->Pop(); | |
123 ExpectSequenceAndSortKey(PriorityQueue::SequenceAndSortKey::Null(), | |
124 transaction->Peek()); | |
125 | |
126 // Expect 4 calls to mock.SequenceInsertedInPriorityQueue() when the | |
127 // Transaction is destroyed. | |
128 EXPECT_CALL(mock, SequenceInsertedInPriorityQueue()).Times(4); | |
129 transaction.reset(); | |
130 } | |
131 | |
132 // Check that creating Transactions on the same thread for 2 unrelated | |
robliao
2016/02/23 21:00:35
It would also be interesting to create two transac
fdoray
2016/02/24 15:03:53
Done.
| |
133 // PriorityQueues causes a crash. | |
134 TEST(TaskSchedulerPriorityQueueTest, IllegalMultipleTransactions) { | |
135 PriorityQueue pq_a(Bind(&DoNothing)); | |
gab
2016/02/23 20:58:42
s/Bind(&DoNothing)/Closure()/
here and below? I
fdoray
2016/02/24 15:03:53
Done. Added comment in PriorityQueue to say that t
| |
136 PriorityQueue pq_b(Bind(&DoNothing)); | |
137 | |
138 EXPECT_DEBUG_DEATH( | |
139 { | |
robliao
2016/02/23 21:00:35
Follow the lambda expressions style for this block
fdoray
2016/02/24 15:03:53
Done.
| |
140 scoped_ptr<PriorityQueue::Transaction> transaction_a = | |
141 pq_a.BeginTransaction(); | |
142 scoped_ptr<PriorityQueue::Transaction> transaction_b = | |
143 pq_b.BeginTransaction(); | |
144 }, | |
145 ""); | |
146 } | |
147 | |
148 // Check that there is no crash when Transactions are created on the same thread | |
149 // for 2 PriorityQueues which have a predecessor relationship. | |
150 TEST(TaskSchedulerPriorityQueueTest, LegalMultipleTransactions) { | |
151 PriorityQueue pq_a(Bind(&DoNothing)); | |
152 PriorityQueue pq_b(Bind(&DoNothing), &pq_a); | |
153 | |
154 // This shouldn't crash. | |
155 scoped_ptr<PriorityQueue::Transaction> transaction_a = | |
156 pq_a.BeginTransaction(); | |
157 scoped_ptr<PriorityQueue::Transaction> transaction_b = | |
158 pq_b.BeginTransaction(); | |
159 } | |
160 | |
161 } // namespace internal | |
162 } // namespace base | |
OLD | NEW |