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
3 // found in the LICENSE file.
5 #include "base/atomic_sequence_num.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/threading/simple_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 class SetIntRunner
: public DelegateSimpleThread::Delegate
{
17 SetIntRunner(int* ptr
, int val
) : ptr_(ptr
), val_(val
) { }
18 ~SetIntRunner() override
{}
20 void Run() override
{ *ptr_
= val_
; }
27 class WaitEventRunner
: public DelegateSimpleThread::Delegate
{
29 explicit WaitEventRunner(WaitableEvent
* event
) : event_(event
) { }
30 ~WaitEventRunner() override
{}
33 EXPECT_FALSE(event_
->IsSignaled());
35 EXPECT_TRUE(event_
->IsSignaled());
38 WaitableEvent
* event_
;
41 class SeqRunner
: public DelegateSimpleThread::Delegate
{
43 explicit SeqRunner(AtomicSequenceNumber
* seq
) : seq_(seq
) { }
44 void Run() override
{ seq_
->GetNext(); }
47 AtomicSequenceNumber
* seq_
;
50 // We count up on a sequence number, firing on the event when we've hit our
51 // expected amount, otherwise we wait on the event. This will ensure that we
52 // have all threads outstanding until we hit our expected thread pool size.
53 class VerifyPoolRunner
: public DelegateSimpleThread::Delegate
{
55 VerifyPoolRunner(AtomicSequenceNumber
* seq
,
56 int total
, WaitableEvent
* event
)
57 : seq_(seq
), total_(total
), event_(event
) { }
60 if (seq_
->GetNext() == total_
) {
68 AtomicSequenceNumber
* seq_
;
70 WaitableEvent
* event_
;
75 TEST(SimpleThreadTest
, CreateAndJoin
) {
78 SetIntRunner
runner(&stack_int
, 7);
79 EXPECT_EQ(0, stack_int
);
81 DelegateSimpleThread
thread(&runner
, "int_setter");
82 EXPECT_FALSE(thread
.HasBeenStarted());
83 EXPECT_FALSE(thread
.HasBeenJoined());
84 EXPECT_EQ(0, stack_int
);
87 EXPECT_TRUE(thread
.HasBeenStarted());
88 EXPECT_FALSE(thread
.HasBeenJoined());
91 EXPECT_TRUE(thread
.HasBeenStarted());
92 EXPECT_TRUE(thread
.HasBeenJoined());
93 EXPECT_EQ(7, stack_int
);
96 TEST(SimpleThreadTest
, WaitForEvent
) {
97 // Create a thread, and wait for it to signal us.
98 WaitableEvent
event(true, false);
100 WaitEventRunner
runner(&event
);
101 DelegateSimpleThread
thread(&runner
, "event_waiter");
103 EXPECT_FALSE(event
.IsSignaled());
106 EXPECT_TRUE(event
.IsSignaled());
110 TEST(SimpleThreadTest
, NamedWithOptions
) {
111 WaitableEvent
event(true, false);
113 WaitEventRunner
runner(&event
);
114 SimpleThread::Options options
;
115 DelegateSimpleThread
thread(&runner
, "event_waiter", options
);
116 EXPECT_EQ(thread
.name_prefix(), "event_waiter");
117 EXPECT_FALSE(event
.IsSignaled());
120 EXPECT_EQ(thread
.name_prefix(), "event_waiter");
121 EXPECT_EQ(thread
.name(),
122 std::string("event_waiter/") + IntToString(thread
.tid()));
125 EXPECT_TRUE(event
.IsSignaled());
128 // We keep the name and tid, even after the thread is gone.
129 EXPECT_EQ(thread
.name_prefix(), "event_waiter");
130 EXPECT_EQ(thread
.name(),
131 std::string("event_waiter/") + IntToString(thread
.tid()));
134 TEST(SimpleThreadTest
, ThreadPool
) {
135 AtomicSequenceNumber seq
;
136 SeqRunner
runner(&seq
);
137 DelegateSimpleThreadPool
pool("seq_runner", 10);
139 // Add work before we're running.
140 pool
.AddWork(&runner
, 300);
142 EXPECT_EQ(seq
.GetNext(), 0);
145 // Add work while we're running.
146 pool
.AddWork(&runner
, 300);
150 EXPECT_EQ(seq
.GetNext(), 601);
152 // We can reuse our pool. Verify that all 10 threads can actually run in
153 // parallel, so this test will only pass if there are actually 10 threads.
154 AtomicSequenceNumber seq2
;
155 WaitableEvent
event(true, false);
156 // Changing 9 to 10, for example, would cause us JoinAll() to never return.
157 VerifyPoolRunner
verifier(&seq2
, 9, &event
);
160 pool
.AddWork(&verifier
, 10);
163 EXPECT_EQ(seq2
.GetNext(), 10);