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/test/scoped_task_scheduler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/macros.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/sequence_checker.h" | |
12 #include "base/task_scheduler/post_task.h" | |
13 #include "base/task_scheduler/test_utils.h" | |
14 #include "base/threading/sequenced_task_runner_handle.h" | |
15 #include "base/threading/thread_checker.h" | |
16 #include "base/threading/thread_task_runner_handle.h" | |
17 #include "build/build_config.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace base { | |
21 namespace test { | |
22 | |
23 namespace { | |
24 | |
25 class ScopedTaskSchedulerTest : public testing::Test { | |
26 public: | |
27 ScopedTaskSchedulerTest() { | |
28 sequence_checker_.DetachFromSequence(); | |
29 thread_checker_.DetachFromThread(); | |
gab
2016/12/08 20:15:45
Why detach?
fdoray
2016/12/09 16:26:42
Added comment.
| |
30 } | |
31 | |
32 SequenceCheckerImpl sequence_checker_; | |
33 ThreadCheckerImpl thread_checker_; | |
gab
2016/12/08 20:15:45
If all you need is these two members and they're u
fdoray
2016/12/09 16:26:42
Done.
| |
34 | |
35 private: | |
36 DISALLOW_COPY_AND_ASSIGN(ScopedTaskSchedulerTest); | |
37 }; | |
38 | |
39 } // namespace | |
40 | |
41 TEST_F(ScopedTaskSchedulerTest, PostTask) { | |
42 ScopedTaskScheduler scoped_task_scheduler; | |
43 bool first_task_ran = false; | |
44 bool second_task_ran = false; | |
45 | |
46 PostTask(FROM_HERE, | |
47 Bind( | |
48 [](ScopedTaskSchedulerTest* test, bool* first_task_ran) { | |
49 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); | |
50 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | |
51 EXPECT_TRUE(test->sequence_checker_.CalledOnValidSequence()); | |
52 EXPECT_TRUE(test->thread_checker_.CalledOnValidThread()); | |
53 *first_task_ran = true; | |
54 }, | |
55 Unretained(this), Unretained(&first_task_ran))); | |
56 | |
57 PostTask(FROM_HERE, | |
58 Bind( | |
59 [](ScopedTaskSchedulerTest* test, bool* second_task_ran) { | |
60 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); | |
61 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | |
62 EXPECT_FALSE(test->sequence_checker_.CalledOnValidSequence()); | |
63 EXPECT_FALSE(test->thread_checker_.CalledOnValidThread()); | |
64 *second_task_ran = true; | |
65 }, | |
66 Unretained(this), Unretained(&second_task_ran))); | |
67 | |
68 RunLoop().RunUntilIdle(); | |
69 | |
70 EXPECT_TRUE(first_task_ran); | |
71 EXPECT_TRUE(second_task_ran); | |
72 } | |
73 | |
74 TEST_F(ScopedTaskSchedulerTest, CreateTaskRunnerAndPostTask) { | |
75 ScopedTaskScheduler scoped_task_scheduler; | |
76 auto task_runner = CreateTaskRunnerWithTraits(TaskTraits()); | |
77 bool first_task_ran = false; | |
78 bool second_task_ran = false; | |
79 | |
80 task_runner->PostTask( | |
81 FROM_HERE, | |
82 Bind( | |
83 [](ScopedTaskSchedulerTest* test, bool* first_task_ran) { | |
84 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); | |
85 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | |
86 EXPECT_TRUE(test->sequence_checker_.CalledOnValidSequence()); | |
87 EXPECT_TRUE(test->thread_checker_.CalledOnValidThread()); | |
88 *first_task_ran = true; | |
89 }, | |
90 Unretained(this), Unretained(&first_task_ran))); | |
91 | |
92 task_runner->PostTask( | |
93 FROM_HERE, | |
94 Bind( | |
95 [](ScopedTaskSchedulerTest* test, bool* second_task_ran) { | |
96 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); | |
97 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | |
98 EXPECT_FALSE(test->sequence_checker_.CalledOnValidSequence()); | |
99 EXPECT_FALSE(test->thread_checker_.CalledOnValidThread()); | |
100 *second_task_ran = true; | |
101 }, | |
102 Unretained(this), Unretained(&second_task_ran))); | |
103 | |
104 RunLoop().RunUntilIdle(); | |
105 | |
106 EXPECT_TRUE(first_task_ran); | |
107 EXPECT_TRUE(second_task_ran); | |
108 } | |
109 | |
110 TEST_F(ScopedTaskSchedulerTest, CreateSequencedTaskRunnerAndPostTask) { | |
111 ScopedTaskScheduler scoped_task_scheduler; | |
112 auto task_runner = CreateSequencedTaskRunnerWithTraits(TaskTraits()); | |
113 bool first_task_ran = false; | |
114 bool second_task_ran = false; | |
115 | |
116 task_runner->PostTask( | |
117 FROM_HERE, | |
118 Bind( | |
119 [](ScopedTaskSchedulerTest* test, bool* first_task_ran) { | |
120 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); | |
121 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | |
122 EXPECT_TRUE(test->sequence_checker_.CalledOnValidSequence()); | |
123 EXPECT_TRUE(test->thread_checker_.CalledOnValidThread()); | |
124 *first_task_ran = true; | |
125 }, | |
126 Unretained(this), Unretained(&first_task_ran))); | |
127 | |
128 task_runner->PostTask( | |
129 FROM_HERE, | |
130 Bind( | |
131 [](ScopedTaskSchedulerTest* test, bool* second_task_ran) { | |
132 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); | |
133 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | |
134 EXPECT_TRUE(test->sequence_checker_.CalledOnValidSequence()); | |
135 EXPECT_FALSE(test->thread_checker_.CalledOnValidThread()); | |
136 *second_task_ran = true; | |
137 }, | |
138 Unretained(this), Unretained(&second_task_ran))); | |
139 | |
140 RunLoop().RunUntilIdle(); | |
141 | |
142 EXPECT_TRUE(first_task_ran); | |
143 EXPECT_TRUE(second_task_ran); | |
144 } | |
145 | |
146 TEST_F(ScopedTaskSchedulerTest, CreateSingleThreadTaskRunnerAndPostTask) { | |
147 ScopedTaskScheduler scoped_task_scheduler; | |
148 auto task_runner = CreateSingleThreadTaskRunnerWithTraits(TaskTraits()); | |
149 bool first_task_ran = false; | |
150 bool second_task_ran = false; | |
151 | |
152 task_runner->PostTask( | |
153 FROM_HERE, | |
154 Bind( | |
155 [](ScopedTaskSchedulerTest* test, bool* first_task_ran) { | |
156 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); | |
157 EXPECT_TRUE(ThreadTaskRunnerHandle::IsSet()); | |
158 EXPECT_TRUE(test->sequence_checker_.CalledOnValidSequence()); | |
159 EXPECT_TRUE(test->thread_checker_.CalledOnValidThread()); | |
160 *first_task_ran = true; | |
161 }, | |
162 Unretained(this), Unretained(&first_task_ran))); | |
163 | |
164 task_runner->PostTask( | |
165 FROM_HERE, | |
166 Bind( | |
167 [](ScopedTaskSchedulerTest* test, bool* second_task_ran) { | |
168 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); | |
169 EXPECT_TRUE(ThreadTaskRunnerHandle::IsSet()); | |
170 EXPECT_TRUE(test->sequence_checker_.CalledOnValidSequence()); | |
171 EXPECT_TRUE(test->thread_checker_.CalledOnValidThread()); | |
172 *second_task_ran = true; | |
173 }, | |
174 Unretained(this), Unretained(&second_task_ran))); | |
175 | |
176 RunLoop().RunUntilIdle(); | |
177 | |
178 EXPECT_TRUE(first_task_ran); | |
179 EXPECT_TRUE(second_task_ran); | |
180 } | |
181 | |
182 TEST_F(ScopedTaskSchedulerTest, ShutdownBehavior) { | |
183 bool block_shutdown_task_ran = false; | |
184 { | |
185 ScopedTaskScheduler scoped_task_scheduler; | |
186 PostTaskWithTraits( | |
187 FROM_HERE, TaskTraits().WithShutdownBehavior( | |
188 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | |
189 Bind([]() { | |
190 ADD_FAILURE() << "CONTINUE_ON_SHUTDOWN task should not run"; | |
191 })); | |
192 PostTaskWithTraits(FROM_HERE, TaskTraits().WithShutdownBehavior( | |
193 TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | |
194 Bind([]() { | |
195 ADD_FAILURE() | |
196 << "SKIP_ON_SHUTDOWN task should not run"; | |
197 })); | |
198 PostTaskWithTraits(FROM_HERE, TaskTraits().WithShutdownBehavior( | |
199 TaskShutdownBehavior::BLOCK_SHUTDOWN), | |
200 Bind( | |
201 [](bool* block_shutdown_task_ran) { | |
202 *block_shutdown_task_ran = true; | |
203 }, | |
204 Unretained(&block_shutdown_task_ran))); | |
205 } | |
206 EXPECT_TRUE(block_shutdown_task_ran); | |
207 } | |
208 | |
209 } // namespace test | |
210 } // namespace base | |
OLD | NEW |