blob: 2717246dbe70bb3cf12d229c8e445a23282a6fcd [file] [log] [blame]
initial.commit3f4a7322008-07-27 06:49:38 +09001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "base/lock.h"
31#include "base/message_loop.h"
32#include "base/string_util.h"
33#include "base/thread.h"
34#include "testing/gtest/include/gtest/gtest.h"
35
36namespace {
initial.commit3f4a7322008-07-27 06:49:38 +090037
38class ToggleValue : public Task {
39 public:
40 explicit ToggleValue(bool* value) : value_(value) {
41 }
42 virtual void Run() {
43 *value_ = !*value_;
44 }
45 private:
46 bool* value_;
47};
48
49class SleepSome : public Task {
50 public:
maruel@google.comf6e3db82008-08-13 03:37:35 +090051 explicit SleepSome(int msec) : msec_(msec) {
initial.commit3f4a7322008-07-27 06:49:38 +090052 }
53 virtual void Run() {
darin@google.comc18d7ae2008-08-21 18:46:32 +090054 PlatformThread::Sleep(msec_);
initial.commit3f4a7322008-07-27 06:49:38 +090055 }
56 private:
maruel@google.comf6e3db82008-08-13 03:37:35 +090057 int msec_;
initial.commit3f4a7322008-07-27 06:49:38 +090058};
59
maruel@google.comf6e3db82008-08-13 03:37:35 +090060} // namespace
61
62TEST(ThreadTest, Restart) {
63 Thread a("Restart");
64 a.Stop();
65 EXPECT_FALSE(a.message_loop());
66 EXPECT_TRUE(a.Start());
initial.commit3f4a7322008-07-27 06:49:38 +090067 EXPECT_TRUE(a.message_loop());
68 a.Stop();
69 EXPECT_FALSE(a.message_loop());
maruel@google.comf6e3db82008-08-13 03:37:35 +090070 EXPECT_TRUE(a.Start());
initial.commit3f4a7322008-07-27 06:49:38 +090071 EXPECT_TRUE(a.message_loop());
72 a.Stop();
73 EXPECT_FALSE(a.message_loop());
maruel@google.comf6e3db82008-08-13 03:37:35 +090074 a.Stop();
75 EXPECT_FALSE(a.message_loop());
initial.commit3f4a7322008-07-27 06:49:38 +090076}
77
maruel@google.comf6e3db82008-08-13 03:37:35 +090078TEST(ThreadTest, StartWithStackSize) {
79 Thread a("StartWithStackSize");
80 // Ensure that the thread can work with only 12 kb and still process a
81 // message.
82 EXPECT_TRUE(a.StartWithStackSize(12*1024));
initial.commit3f4a7322008-07-27 06:49:38 +090083 EXPECT_TRUE(a.message_loop());
84
85 bool was_invoked = false;
86 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
87
88 // wait for the task to run (we could use a kernel event here
89 // instead to avoid busy waiting, but this is sufficient for
90 // testing purposes).
maruel@google.comf6e3db82008-08-13 03:37:35 +090091 for (int i = 100; i >= 0 && !was_invoked; --i) {
darin@google.comc18d7ae2008-08-21 18:46:32 +090092 PlatformThread::Sleep(10);
initial.commit3f4a7322008-07-27 06:49:38 +090093 }
94 EXPECT_TRUE(was_invoked);
95}
96
maruel@google.comf6e3db82008-08-13 03:37:35 +090097TEST(ThreadTest, TwoTasks) {
initial.commit3f4a7322008-07-27 06:49:38 +090098 bool was_invoked = false;
99 {
maruel@google.comf6e3db82008-08-13 03:37:35 +0900100 Thread a("TwoTasks");
101 EXPECT_TRUE(a.Start());
initial.commit3f4a7322008-07-27 06:49:38 +0900102 EXPECT_TRUE(a.message_loop());
103
104 // Test that all events are dispatched before the Thread object is
105 // destroyed. We do this by dispatching a sleep event before the
106 // event that will toggle our sentinel value.
maruel@google.comf6e3db82008-08-13 03:37:35 +0900107 a.message_loop()->PostTask(FROM_HERE, new SleepSome(20));
initial.commit3f4a7322008-07-27 06:49:38 +0900108 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
109 }
110 EXPECT_TRUE(was_invoked);
111}
112
maruel@google.comf6e3db82008-08-13 03:37:35 +0900113TEST(ThreadTest, StopSoon) {
114 Thread a("StopSoon");
115 EXPECT_TRUE(a.Start());
116 EXPECT_TRUE(a.message_loop());
117 a.StopSoon();
118 EXPECT_FALSE(a.message_loop());
119 a.StopSoon();
120 EXPECT_FALSE(a.message_loop());
initial.commit3f4a7322008-07-27 06:49:38 +0900121}
122
maruel@google.comf6e3db82008-08-13 03:37:35 +0900123TEST(ThreadTest, ThreadName) {
124 Thread a("ThreadName");
125 EXPECT_TRUE(a.Start());
126 EXPECT_EQ("ThreadName", a.thread_name());
initial.commit3f4a7322008-07-27 06:49:38 +0900127}