Update broken references to image assets
[chromium-blink-merge.git] / base / threading / simple_thread_unittest.cc
blob7229d362f142ec082a76645992f5c1ae1385c116
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"
11 namespace base {
13 namespace {
15 class SetIntRunner : public DelegateSimpleThread::Delegate {
16 public:
17 SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { }
18 ~SetIntRunner() override {}
20 void Run() override { *ptr_ = val_; }
22 private:
23 int* ptr_;
24 int val_;
27 class WaitEventRunner : public DelegateSimpleThread::Delegate {
28 public:
29 explicit WaitEventRunner(WaitableEvent* event) : event_(event) { }
30 ~WaitEventRunner() override {}
32 void Run() override {
33 EXPECT_FALSE(event_->IsSignaled());
34 event_->Signal();
35 EXPECT_TRUE(event_->IsSignaled());
37 private:
38 WaitableEvent* event_;
41 class SeqRunner : public DelegateSimpleThread::Delegate {
42 public:
43 explicit SeqRunner(AtomicSequenceNumber* seq) : seq_(seq) { }
44 void Run() override { seq_->GetNext(); }
46 private:
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 {
54 public:
55 VerifyPoolRunner(AtomicSequenceNumber* seq,
56 int total, WaitableEvent* event)
57 : seq_(seq), total_(total), event_(event) { }
59 void Run() override {
60 if (seq_->GetNext() == total_) {
61 event_->Signal();
62 } else {
63 event_->Wait();
67 private:
68 AtomicSequenceNumber* seq_;
69 int total_;
70 WaitableEvent* event_;
73 } // namespace
75 TEST(SimpleThreadTest, CreateAndJoin) {
76 int stack_int = 0;
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);
86 thread.Start();
87 EXPECT_TRUE(thread.HasBeenStarted());
88 EXPECT_FALSE(thread.HasBeenJoined());
90 thread.Join();
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());
104 thread.Start();
105 event.Wait();
106 EXPECT_TRUE(event.IsSignaled());
107 thread.Join();
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());
119 thread.Start();
120 EXPECT_EQ(thread.name_prefix(), "event_waiter");
121 EXPECT_EQ(thread.name(),
122 std::string("event_waiter/") + IntToString(thread.tid()));
123 event.Wait();
125 EXPECT_TRUE(event.IsSignaled());
126 thread.Join();
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);
143 pool.Start();
145 // Add work while we're running.
146 pool.AddWork(&runner, 300);
148 pool.JoinAll();
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);
158 pool.Start();
160 pool.AddWork(&verifier, 10);
162 pool.JoinAll();
163 EXPECT_EQ(seq2.GetNext(), 10);
166 } // namespace base