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

Side by Side Diff: base/threading/simple_thread_unittest.cc

Issue 2204333003: Add joinable option to SimpleThread::Options as was just done for Thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@b1_nonjoinable_thread
Patch Set: Created 4 years, 4 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
OLDNEW
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
5 #include "base/atomic_sequence_num.h" 7 #include "base/atomic_sequence_num.h"
6 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
7 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/test/gtest_util.h"
11 #include "base/threading/platform_thread.h"
8 #include "base/threading/simple_thread.h" 12 #include "base/threading/simple_thread.h"
13 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
10 15
11 namespace base { 16 namespace base {
12 17
13 namespace { 18 namespace {
14 19
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(base::TimeDelta::FromMilliseconds(20));
Lei Zhang 2016/08/03 23:24:12 no need for base:: inside base.
gab 2016/08/04 14:35:01 Done.
25 }
26 };
27
15 class SetIntRunner : public DelegateSimpleThread::Delegate { 28 class SetIntRunner : public DelegateSimpleThread::Delegate {
16 public: 29 public:
17 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { } 30 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { }
18 ~SetIntRunner() override {} 31 ~SetIntRunner() override {}
19 32
20 void Run() override { *ptr_ = val_; } 33 void Run() override { *ptr_ = val_; }
21 34
22 private: 35 private:
23 int* ptr_; 36 int* ptr_;
24 int val_; 37 int val_;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 139
127 EXPECT_TRUE(event.IsSignaled()); 140 EXPECT_TRUE(event.IsSignaled());
128 thread.Join(); 141 thread.Join();
129 142
130 // We keep the name and tid, even after the thread is gone. 143 // We keep the name and tid, even after the thread is gone.
131 EXPECT_EQ(thread.name_prefix(), "event_waiter"); 144 EXPECT_EQ(thread.name_prefix(), "event_waiter");
132 EXPECT_EQ(thread.name(), 145 EXPECT_EQ(thread.name(),
133 std::string("event_waiter/") + IntToString(thread.tid())); 146 std::string("event_waiter/") + IntToString(thread.tid()));
134 } 147 }
135 148
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
136 TEST(SimpleThreadTest, ThreadPool) { 168 TEST(SimpleThreadTest, ThreadPool) {
137 AtomicSequenceNumber seq; 169 AtomicSequenceNumber seq;
138 SeqRunner runner(&seq); 170 SeqRunner runner(&seq);
139 DelegateSimpleThreadPool pool("seq_runner", 10); 171 DelegateSimpleThreadPool pool("seq_runner", 10);
140 172
141 // Add work before we're running. 173 // Add work before we're running.
142 pool.AddWork(&runner, 300); 174 pool.AddWork(&runner, 300);
143 175
144 EXPECT_EQ(seq.GetNext(), 0); 176 EXPECT_EQ(seq.GetNext(), 0);
145 pool.Start(); 177 pool.Start();
(...skipping 14 matching lines...) Expand all
160 VerifyPoolRunner verifier(&seq2, 9, &event); 192 VerifyPoolRunner verifier(&seq2, 9, &event);
161 pool.Start(); 193 pool.Start();
162 194
163 pool.AddWork(&verifier, 10); 195 pool.AddWork(&verifier, 10);
164 196
165 pool.JoinAll(); 197 pool.JoinAll();
166 EXPECT_EQ(seq2.GetNext(), 10); 198 EXPECT_EQ(seq2.GetNext(), 10);
167 } 199 }
168 200
169 } // namespace base 201 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698