| 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> | |
| 6 | |
| 7 #include "base/atomic_sequence_num.h" | 5 #include "base/atomic_sequence_num.h" |
| 8 #include "base/strings/string_number_conversions.h" | 6 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/test/gtest_util.h" | |
| 11 #include "base/threading/platform_thread.h" | |
| 12 #include "base/threading/simple_thread.h" | 8 #include "base/threading/simple_thread.h" |
| 13 #include "base/time/time.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 10 |
| 16 namespace base { | 11 namespace base { |
| 17 | 12 |
| 18 namespace { | 13 namespace { |
| 19 | 14 |
| 20 // Sleeps a tiny amount to augment chances of tests using it running in races | |
| 21 // should there be any while the tested thread is alive. | |
| 22 class SleepRunner : public DelegateSimpleThread::Delegate { | |
| 23 void Run() override { | |
| 24 PlatformThread::Sleep(TimeDelta::FromMilliseconds(20)); | |
| 25 } | |
| 26 }; | |
| 27 | |
| 28 class SetIntRunner : public DelegateSimpleThread::Delegate { | 15 class SetIntRunner : public DelegateSimpleThread::Delegate { |
| 29 public: | 16 public: |
| 30 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { } | 17 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { } |
| 31 ~SetIntRunner() override {} | 18 ~SetIntRunner() override {} |
| 32 | 19 |
| 33 void Run() override { *ptr_ = val_; } | 20 void Run() override { *ptr_ = val_; } |
| 34 | 21 |
| 35 private: | 22 private: |
| 36 int* ptr_; | 23 int* ptr_; |
| 37 int val_; | 24 int val_; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 126 |
| 140 EXPECT_TRUE(event.IsSignaled()); | 127 EXPECT_TRUE(event.IsSignaled()); |
| 141 thread.Join(); | 128 thread.Join(); |
| 142 | 129 |
| 143 // We keep the name and tid, even after the thread is gone. | 130 // We keep the name and tid, even after the thread is gone. |
| 144 EXPECT_EQ(thread.name_prefix(), "event_waiter"); | 131 EXPECT_EQ(thread.name_prefix(), "event_waiter"); |
| 145 EXPECT_EQ(thread.name(), | 132 EXPECT_EQ(thread.name(), |
| 146 std::string("event_waiter/") + IntToString(thread.tid())); | 133 std::string("event_waiter/") + IntToString(thread.tid())); |
| 147 } | 134 } |
| 148 | 135 |
| 149 TEST(SimpleThreadTest, JoinNonJoinableThreadDeath) { | |
| 150 SleepRunner runner; | |
| 151 SimpleThread::Options options; | |
| 152 options.joinable = false; | |
| 153 DelegateSimpleThread thread(&runner, "noop_runner", options); | |
| 154 | |
| 155 thread.Start(); | |
| 156 EXPECT_DCHECK_DEATH(thread.Join(), ""); | |
| 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 } | |
| 167 | |
| 168 TEST(SimpleThreadTest, ThreadPool) { | 136 TEST(SimpleThreadTest, ThreadPool) { |
| 169 AtomicSequenceNumber seq; | 137 AtomicSequenceNumber seq; |
| 170 SeqRunner runner(&seq); | 138 SeqRunner runner(&seq); |
| 171 DelegateSimpleThreadPool pool("seq_runner", 10); | 139 DelegateSimpleThreadPool pool("seq_runner", 10); |
| 172 | 140 |
| 173 // Add work before we're running. | 141 // Add work before we're running. |
| 174 pool.AddWork(&runner, 300); | 142 pool.AddWork(&runner, 300); |
| 175 | 143 |
| 176 EXPECT_EQ(seq.GetNext(), 0); | 144 EXPECT_EQ(seq.GetNext(), 0); |
| 177 pool.Start(); | 145 pool.Start(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 192 VerifyPoolRunner verifier(&seq2, 9, &event); | 160 VerifyPoolRunner verifier(&seq2, 9, &event); |
| 193 pool.Start(); | 161 pool.Start(); |
| 194 | 162 |
| 195 pool.AddWork(&verifier, 10); | 163 pool.AddWork(&verifier, 10); |
| 196 | 164 |
| 197 pool.JoinAll(); | 165 pool.JoinAll(); |
| 198 EXPECT_EQ(seq2.GetNext(), 10); | 166 EXPECT_EQ(seq2.GetNext(), 10); |
| 199 } | 167 } |
| 200 | 168 |
| 201 } // namespace base | 169 } // namespace base |
| OLD | NEW |