OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/synchronization/waitable_event.h" | |
13 #include "base/task_scheduler/post_task.h" | |
14 #include "base/test/scoped_task_environment.h" | |
13 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 void QuitWhenIdleTask(RunLoop* run_loop, int* counter) { | 23 void QuitWhenIdleTask(RunLoop* run_loop, int* counter) { |
22 run_loop->QuitWhenIdle(); | 24 run_loop->QuitWhenIdle(); |
(...skipping 22 matching lines...) Expand all Loading... | |
45 MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current()); | 47 MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current()); |
46 nested_run_loop.Run(); | 48 nested_run_loop.Run(); |
47 | 49 |
48 ++(*counter); | 50 ++(*counter); |
49 } | 51 } |
50 | 52 |
51 class RunLoopTest : public testing::Test { | 53 class RunLoopTest : public testing::Test { |
52 protected: | 54 protected: |
53 RunLoopTest() = default; | 55 RunLoopTest() = default; |
54 | 56 |
55 MessageLoop message_loop_; | 57 test::ScopedTaskEnvironment task_environment_; |
56 RunLoop run_loop_; | 58 RunLoop run_loop_; |
57 int counter_ = 0; | 59 int counter_ = 0; |
58 | 60 |
59 private: | 61 private: |
60 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); | 62 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); |
61 }; | 63 }; |
62 | 64 |
63 } // namespace | 65 } // namespace |
64 | 66 |
65 TEST_F(RunLoopTest, QuitWhenIdle) { | 67 TEST_F(RunLoopTest, QuitWhenIdle) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) { | 109 TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) { |
108 Closure quit_when_idle_closure; | 110 Closure quit_when_idle_closure; |
109 { | 111 { |
110 RunLoop run_loop; | 112 RunLoop run_loop; |
111 quit_when_idle_closure = run_loop.QuitWhenIdleClosure(); | 113 quit_when_idle_closure = run_loop.QuitWhenIdleClosure(); |
112 run_loop.RunUntilIdle(); | 114 run_loop.RunUntilIdle(); |
113 } | 115 } |
114 quit_when_idle_closure.Run(); | 116 quit_when_idle_closure.Run(); |
115 } | 117 } |
116 | 118 |
119 // Verify that QuitClosure can be executed from another sequence. | |
120 TEST_F(RunLoopTest, QuitFromOtherSequence) { | |
121 scoped_refptr<SequencedTaskRunner> other_sequence = | |
122 CreateSequencedTaskRunnerWithTraits({}); | |
123 | |
124 // Always expected to run before asynchronous Quit() kicks in. | |
125 ThreadTaskRunnerHandle::Get()->PostTask( | |
126 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_))); | |
127 | |
128 WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL, | |
129 WaitableEvent::InitialState::NOT_SIGNALED); | |
130 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure()); | |
danakj
2017/05/19 18:53:34
These tests all verify QuitClosure() but not Quit(
gab
2017/05/19 19:38:16
Done.
| |
131 other_sequence->PostTask( | |
132 FROM_HERE, | |
133 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit))); | |
134 | |
135 // Anything that's posted after the Quit closure was posted back to this | |
136 // sequence shouldn't get a chance to run. | |
137 loop_was_quit.Wait(); | |
138 ThreadTaskRunnerHandle::Get()->PostTask( | |
139 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_))); | |
140 | |
141 run_loop_.Run(); | |
142 | |
143 EXPECT_EQ(1, counter_); | |
144 } | |
145 | |
146 // Verify that QuitClosure can be executed from another sequence even when the | |
147 // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above. | |
148 TEST_F(RunLoopTest, QuitFromOtherSequenceRacy) { | |
149 scoped_refptr<SequencedTaskRunner> other_sequence = | |
150 CreateSequencedTaskRunnerWithTraits({}); | |
151 | |
152 // Always expected to run before asynchronous Quit() kicks in. | |
153 ThreadTaskRunnerHandle::Get()->PostTask( | |
154 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_))); | |
155 | |
156 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure()); | |
157 | |
158 run_loop_.Run(); | |
159 | |
160 EXPECT_EQ(1, counter_); | |
161 } | |
162 | |
163 // Verify that QuitWhenIdleClosure can be executed from another sequence. | |
164 TEST_F(RunLoopTest, QuitWhenIdleFromOtherSequence) { | |
165 scoped_refptr<SequencedTaskRunner> other_sequence = | |
166 CreateSequencedTaskRunnerWithTraits({}); | |
167 | |
168 ThreadTaskRunnerHandle::Get()->PostTask( | |
169 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_))); | |
170 | |
171 other_sequence->PostTask(FROM_HERE, run_loop_.QuitWhenIdleClosure()); | |
172 | |
173 ThreadTaskRunnerHandle::Get()->PostTask( | |
174 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_))); | |
175 | |
176 run_loop_.Run(); | |
177 | |
178 // Regardless of the outcome of the race this thread shouldn't have been idle | |
179 // until the counter was ticked twice. | |
180 EXPECT_EQ(2, counter_); | |
181 } | |
182 | |
117 TEST_F(RunLoopTest, IsRunningOnCurrentThread) { | 183 TEST_F(RunLoopTest, IsRunningOnCurrentThread) { |
118 EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread()); | 184 EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread()); |
119 ThreadTaskRunnerHandle::Get()->PostTask( | 185 ThreadTaskRunnerHandle::Get()->PostTask( |
120 FROM_HERE, | 186 FROM_HERE, |
121 Bind([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); })); | 187 Bind([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); })); |
122 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure()); | 188 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
123 run_loop_.Run(); | 189 run_loop_.Run(); |
124 } | 190 } |
125 | 191 |
126 TEST_F(RunLoopTest, IsNestedOnCurrentThread) { | 192 TEST_F(RunLoopTest, IsNestedOnCurrentThread) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 | 265 |
200 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind([]() { | 266 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind([]() { |
201 RunLoop nested_run_loop; | 267 RunLoop nested_run_loop; |
202 nested_run_loop.RunUntilIdle(); | 268 nested_run_loop.RunUntilIdle(); |
203 })); | 269 })); |
204 EXPECT_DEATH({ run_loop_.RunUntilIdle(); }, ""); | 270 EXPECT_DEATH({ run_loop_.RunUntilIdle(); }, ""); |
205 } | 271 } |
206 #endif // defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) | 272 #endif // defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) |
207 | 273 |
208 } // namespace base | 274 } // namespace base |
OLD | NEW |