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

Side by Side Diff: base/deferred_sequenced_task_runner_unittest.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 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/deferred_sequenced_task_runner.cc ('k') | base/environment.h » ('j') | 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 (c) 2013 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/deferred_sequenced_task_runner.h"
6
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/threading/thread.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace {
19
20 class DeferredSequencedTaskRunnerTest : public testing::Test,
21 public base::NonThreadSafe {
22 public:
23 class ExecuteTaskOnDestructor :
24 public base::RefCounted<ExecuteTaskOnDestructor> {
25 public:
26 ExecuteTaskOnDestructor(
27 DeferredSequencedTaskRunnerTest* executor,
28 int task_id)
29 : executor_(executor),
30 task_id_(task_id) {
31 }
32 private:
33 friend class base::RefCounted<ExecuteTaskOnDestructor>;
34 virtual ~ExecuteTaskOnDestructor() {
35 executor_->ExecuteTask(task_id_);
36 }
37 DeferredSequencedTaskRunnerTest* executor_;
38 int task_id_;
39 };
40
41 void ExecuteTask(int task_id) {
42 base::AutoLock lock(lock_);
43 executed_task_ids_.push_back(task_id);
44 }
45
46 void PostExecuteTask(int task_id) {
47 runner_->PostTask(FROM_HERE,
48 base::Bind(&DeferredSequencedTaskRunnerTest::ExecuteTask,
49 base::Unretained(this),
50 task_id));
51 }
52
53 void StartRunner() {
54 runner_->Start();
55 }
56
57 void DoNothing(ExecuteTaskOnDestructor* object) {
58 }
59
60 protected:
61 DeferredSequencedTaskRunnerTest()
62 : loop_(),
63 runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {}
64
65 base::MessageLoop loop_;
66 scoped_refptr<base::DeferredSequencedTaskRunner> runner_;
67 mutable base::Lock lock_;
68 std::vector<int> executed_task_ids_;
69 };
70
71 TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
72 PostExecuteTask(1);
73 loop_.RunUntilIdle();
74 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
75 }
76
77 TEST_F(DeferredSequencedTaskRunnerTest, Start) {
78 StartRunner();
79 PostExecuteTask(1);
80 loop_.RunUntilIdle();
81 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
82 }
83
84 TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
85 StartRunner();
86 for (int i = 1; i < 5; ++i)
87 PostExecuteTask(i);
88
89 loop_.RunUntilIdle();
90 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
91 }
92
93 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
94 PostExecuteTask(1);
95 loop_.RunUntilIdle();
96 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
97
98 StartRunner();
99 loop_.RunUntilIdle();
100 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
101
102 PostExecuteTask(2);
103 loop_.RunUntilIdle();
104 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
105 }
106
107 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
108 for (int i = 1; i < 5; ++i)
109 PostExecuteTask(i);
110 loop_.RunUntilIdle();
111 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
112
113 StartRunner();
114 for (int i = 5; i < 9; ++i)
115 PostExecuteTask(i);
116 loop_.RunUntilIdle();
117 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
118 }
119
120 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
121 {
122 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
123 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
124 thread1.Start();
125 thread2.Start();
126 for (int i = 0; i < 5; ++i) {
127 thread1.task_runner()->PostTask(
128 FROM_HERE,
129 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
130 base::Unretained(this), 2 * i));
131 thread2.task_runner()->PostTask(
132 FROM_HERE,
133 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
134 base::Unretained(this), 2 * i + 1));
135 if (i == 2) {
136 thread1.task_runner()->PostTask(
137 FROM_HERE, base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner,
138 base::Unretained(this)));
139 }
140 }
141 }
142
143 loop_.RunUntilIdle();
144 EXPECT_THAT(executed_task_ids_,
145 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
146 }
147
148 TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
149 {
150 base::Thread thread("DeferredSequencedTaskRunnerTestThread");
151 thread.Start();
152 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
153 for (int i = 0; i < 5; ++i) {
154 {
155 // Use a block to ensure that no reference to |short_lived_object|
156 // is kept on the main thread after it is posted to |runner_|.
157 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
158 new ExecuteTaskOnDestructor(this, 2 * i);
159 runner_->PostTask(
160 FROM_HERE,
161 base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing,
162 base::Unretained(this),
163 short_lived_object));
164 }
165 // |short_lived_object| with id |2 * i| should be destroyed before the
166 // task |2 * i + 1| is executed.
167 PostExecuteTask(2 * i + 1);
168 }
169 StartRunner();
170 }
171
172 // All |short_lived_object| with id |2 * i| are destroyed before the task
173 // |2 * i + 1| is executed.
174 EXPECT_THAT(executed_task_ids_,
175 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
176 }
177
178 } // namespace
OLDNEW
« no previous file with comments | « base/deferred_sequenced_task_runner.cc ('k') | base/environment.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698