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

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

Issue 1892033003: TaskScheduler [10] SchedulerWorkerThreadStack (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@9_single_thread
Patch Set: CR danakj #44 (Empty -> IsEmpty) Created 4 years, 8 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
« no previous file with comments | « base/task_scheduler/scheduler_worker_thread_stack.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/scheduler_worker_thread_stack.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/task_scheduler/scheduler_worker_thread.h"
10 #include "base/task_scheduler/sequence.h"
11 #include "base/task_scheduler/task_tracker.h"
12 #include "base/task_scheduler/test_utils.h"
13 #include "base/threading/platform_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace base {
17 namespace internal {
18
19 namespace {
20
21 class MockSchedulerWorkerThreadDelegate
22 : public SchedulerWorkerThread::Delegate {
23 public:
24 void OnMainEntry() override {}
25 scoped_refptr<Sequence> GetWork(
26 SchedulerWorkerThread* worker_thread) override {
27 return nullptr;
28 }
29 void EnqueueSequence(scoped_refptr<Sequence> sequence) override {
30 NOTREACHED();
31 }
32 };
33
34 class TaskSchedulerWorkerThreadStackTest : public testing::Test {
35 protected:
36 void SetUp() override {
37 thread_a_ = SchedulerWorkerThread::CreateSchedulerWorkerThread(
38 ThreadPriority::NORMAL, &delegate_, &task_tracker_);
39 ASSERT_TRUE(thread_a_);
40 thread_b_ = SchedulerWorkerThread::CreateSchedulerWorkerThread(
41 ThreadPriority::NORMAL, &delegate_, &task_tracker_);
42 ASSERT_TRUE(thread_b_);
43 thread_c_ = SchedulerWorkerThread::CreateSchedulerWorkerThread(
44 ThreadPriority::NORMAL, &delegate_, &task_tracker_);
45 ASSERT_TRUE(thread_c_);
46 }
47
48 void TearDown() override {
49 thread_a_->JoinForTesting();
50 thread_b_->JoinForTesting();
51 thread_c_->JoinForTesting();
52 }
53
54 std::unique_ptr<SchedulerWorkerThread> thread_a_;
55 std::unique_ptr<SchedulerWorkerThread> thread_b_;
56 std::unique_ptr<SchedulerWorkerThread> thread_c_;
57
58 private:
59 MockSchedulerWorkerThreadDelegate delegate_;
60 TaskTracker task_tracker_;
61 };
62
63 } // namespace
64
65 // Verify that Push() and Pop() add/remove values in FIFO order.
66 TEST_F(TaskSchedulerWorkerThreadStackTest, PushPop) {
67 SchedulerWorkerThreadStack stack;
68 EXPECT_TRUE(stack.IsEmpty());
69 EXPECT_EQ(0U, stack.Size());
70
71 stack.Push(thread_a_.get());
72 EXPECT_FALSE(stack.IsEmpty());
73 EXPECT_EQ(1U, stack.Size());
74
75 stack.Push(thread_b_.get());
76 EXPECT_FALSE(stack.IsEmpty());
77 EXPECT_EQ(2U, stack.Size());
78
79 stack.Push(thread_c_.get());
80 EXPECT_FALSE(stack.IsEmpty());
81 EXPECT_EQ(3U, stack.Size());
82
83 EXPECT_EQ(thread_c_.get(), stack.Pop());
84 EXPECT_FALSE(stack.IsEmpty());
85 EXPECT_EQ(2U, stack.Size());
86
87 stack.Push(thread_c_.get());
88 EXPECT_FALSE(stack.IsEmpty());
89 EXPECT_EQ(3U, stack.Size());
90
91 EXPECT_EQ(thread_c_.get(), stack.Pop());
92 EXPECT_FALSE(stack.IsEmpty());
93 EXPECT_EQ(2U, stack.Size());
94
95 EXPECT_EQ(thread_b_.get(), stack.Pop());
96 EXPECT_FALSE(stack.IsEmpty());
97 EXPECT_EQ(1U, stack.Size());
98
99 EXPECT_EQ(thread_a_.get(), stack.Pop());
100 EXPECT_TRUE(stack.IsEmpty());
101 EXPECT_EQ(0U, stack.Size());
102 }
103
104 // Verify that a value can be removed by Remove().
105 TEST_F(TaskSchedulerWorkerThreadStackTest, Remove) {
106 SchedulerWorkerThreadStack stack;
107 EXPECT_TRUE(stack.IsEmpty());
108 EXPECT_EQ(0U, stack.Size());
109
110 stack.Push(thread_a_.get());
111 EXPECT_FALSE(stack.IsEmpty());
112 EXPECT_EQ(1U, stack.Size());
113
114 stack.Push(thread_b_.get());
115 EXPECT_FALSE(stack.IsEmpty());
116 EXPECT_EQ(2U, stack.Size());
117
118 stack.Push(thread_c_.get());
119 EXPECT_FALSE(stack.IsEmpty());
120 EXPECT_EQ(3U, stack.Size());
121
122 stack.Remove(thread_b_.get());
123 EXPECT_FALSE(stack.IsEmpty());
124 EXPECT_EQ(2U, stack.Size());
125
126 EXPECT_EQ(thread_c_.get(), stack.Pop());
127 EXPECT_FALSE(stack.IsEmpty());
128 EXPECT_EQ(1U, stack.Size());
129
130 EXPECT_EQ(thread_a_.get(), stack.Pop());
131 EXPECT_TRUE(stack.IsEmpty());
132 EXPECT_EQ(0U, stack.Size());
133 }
134
135 // Verify that a value can be pushed again after it has been removed.
136 TEST_F(TaskSchedulerWorkerThreadStackTest, PushAfterRemove) {
137 SchedulerWorkerThreadStack stack;
138 EXPECT_EQ(0U, stack.Size());
139 EXPECT_TRUE(stack.IsEmpty());
140
141 stack.Push(thread_a_.get());
142 EXPECT_EQ(1U, stack.Size());
143 EXPECT_FALSE(stack.IsEmpty());
144
145 stack.Remove(thread_a_.get());
146 EXPECT_EQ(0U, stack.Size());
147 EXPECT_TRUE(stack.IsEmpty());
148
149 stack.Push(thread_a_.get());
150 EXPECT_EQ(1U, stack.Size());
151 EXPECT_FALSE(stack.IsEmpty());
152 }
153
154 // Verify that Push() DCHECKs when a value is inserted twice.
155 TEST_F(TaskSchedulerWorkerThreadStackTest, PushTwice) {
156 SchedulerWorkerThreadStack stack;
157 stack.Push(thread_a_.get());
158 EXPECT_DCHECK_DEATH({ stack.Push(thread_a_.get()); }, "");
159 }
160
161 } // namespace internal
162 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/scheduler_worker_thread_stack.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698