OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
8 #include "base/debug/leak_annotations.h" | |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
10 #include "base/test/gtest_util.h" | 11 #include "base/test/gtest_util.h" |
11 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
12 #include "base/threading/simple_thread.h" | 13 #include "base/threading/simple_thread.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 | 18 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 | 140 |
140 EXPECT_TRUE(event.IsSignaled()); | 141 EXPECT_TRUE(event.IsSignaled()); |
141 thread.Join(); | 142 thread.Join(); |
142 | 143 |
143 // We keep the name and tid, even after the thread is gone. | 144 // We keep the name and tid, even after the thread is gone. |
144 EXPECT_EQ(thread.name_prefix(), "event_waiter"); | 145 EXPECT_EQ(thread.name_prefix(), "event_waiter"); |
145 EXPECT_EQ(thread.name(), | 146 EXPECT_EQ(thread.name(), |
146 std::string("event_waiter/") + IntToString(thread.tid())); | 147 std::string("event_waiter/") + IntToString(thread.tid())); |
147 } | 148 } |
148 | 149 |
149 TEST(SimpleThreadTest, JoinNonJoinableThreadDeath) { | 150 TEST(SimpleThreadTest, StartNonJoinableThreadAndDieOnJoin) { |
151 SleepRunner runner; | |
sdefresne
2016/08/05 16:48:33
I think SleepRunner also needs to be leaked as Del
gab
2016/08/05 17:28:33
Good point, for DelegateSimpleThread, the Delegate
| |
152 | |
153 SimpleThread::Options options; | |
154 options.joinable = false; | |
155 DelegateSimpleThread* thread = | |
156 new DelegateSimpleThread(&runner, "noop_runner", options); | |
157 // Non-joinable SimpleThreads have to be leaked. | |
158 ANNOTATE_LEAKING_OBJECT_PTR(thread); | |
159 | |
160 EXPECT_FALSE(thread->HasBeenStarted()); | |
161 thread->Start(); | |
162 EXPECT_TRUE(thread->HasBeenStarted()); | |
163 EXPECT_FALSE(thread->HasBeenJoined()); | |
164 | |
165 EXPECT_DCHECK_DEATH(thread->Join()); | |
166 } | |
167 | |
168 TEST(SimpleThreadTest, NonJoinableThreadDeathOnDestruction) { | |
150 SleepRunner runner; | 169 SleepRunner runner; |
151 SimpleThread::Options options; | 170 SimpleThread::Options options; |
152 options.joinable = false; | 171 options.joinable = false; |
153 DelegateSimpleThread thread(&runner, "noop_runner", options); | 172 DelegateSimpleThread* thread = |
173 new DelegateSimpleThread(&runner, "noop_runner", options); | |
174 ANNOTATE_LEAKING_OBJECT_PTR(thread); | |
154 | 175 |
155 thread.Start(); | 176 thread->Start(); |
156 EXPECT_DCHECK_DEATH(thread.Join()); | 177 EXPECT_DCHECK_DEATH({ delete thread; }); |
157 } | |
158 | |
159 TEST(SimpleThreadTest, DeleteNonJoinableWithoutJoinIsSafe) { | |
160 SleepRunner runner; | |
161 SimpleThread::Options options; | |
162 options.joinable = false; | |
163 DelegateSimpleThread thread(&runner, "noop_runner", options); | |
164 thread.Start(); | |
165 // Nothing blows up when |thread| goes out of scope. | |
166 } | 178 } |
167 | 179 |
168 TEST(SimpleThreadTest, ThreadPool) { | 180 TEST(SimpleThreadTest, ThreadPool) { |
169 AtomicSequenceNumber seq; | 181 AtomicSequenceNumber seq; |
170 SeqRunner runner(&seq); | 182 SeqRunner runner(&seq); |
171 DelegateSimpleThreadPool pool("seq_runner", 10); | 183 DelegateSimpleThreadPool pool("seq_runner", 10); |
172 | 184 |
173 // Add work before we're running. | 185 // Add work before we're running. |
174 pool.AddWork(&runner, 300); | 186 pool.AddWork(&runner, 300); |
175 | 187 |
(...skipping 16 matching lines...) Expand all Loading... | |
192 VerifyPoolRunner verifier(&seq2, 9, &event); | 204 VerifyPoolRunner verifier(&seq2, 9, &event); |
193 pool.Start(); | 205 pool.Start(); |
194 | 206 |
195 pool.AddWork(&verifier, 10); | 207 pool.AddWork(&verifier, 10); |
196 | 208 |
197 pool.JoinAll(); | 209 pool.JoinAll(); |
198 EXPECT_EQ(seq2.GetNext(), 10); | 210 EXPECT_EQ(seq2.GetNext(), 10); |
199 } | 211 } |
200 | 212 |
201 } // namespace base | 213 } // namespace base |
OLD | NEW |