blob: c570e918ed1a248e4b4a9d85813dccfb9b63e837 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "base/lock.h"
6#include "base/message_loop.h"
7#include "base/string_util.h"
8#include "base/thread.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
initial.commit3f4a7322008-07-27 06:49:38 +090012
13class ToggleValue : public Task {
14 public:
15 explicit ToggleValue(bool* value) : value_(value) {
16 }
17 virtual void Run() {
18 *value_ = !*value_;
19 }
20 private:
21 bool* value_;
22};
23
24class SleepSome : public Task {
25 public:
maruel@google.comf6e3db82008-08-13 03:37:35 +090026 explicit SleepSome(int msec) : msec_(msec) {
initial.commit3f4a7322008-07-27 06:49:38 +090027 }
28 virtual void Run() {
darin@google.comc18d7ae2008-08-21 18:46:32 +090029 PlatformThread::Sleep(msec_);
initial.commit3f4a7322008-07-27 06:49:38 +090030 }
31 private:
maruel@google.comf6e3db82008-08-13 03:37:35 +090032 int msec_;
initial.commit3f4a7322008-07-27 06:49:38 +090033};
34
maruel@google.comf6e3db82008-08-13 03:37:35 +090035} // namespace
36
37TEST(ThreadTest, Restart) {
38 Thread a("Restart");
39 a.Stop();
40 EXPECT_FALSE(a.message_loop());
41 EXPECT_TRUE(a.Start());
initial.commit3f4a7322008-07-27 06:49:38 +090042 EXPECT_TRUE(a.message_loop());
43 a.Stop();
44 EXPECT_FALSE(a.message_loop());
maruel@google.comf6e3db82008-08-13 03:37:35 +090045 EXPECT_TRUE(a.Start());
initial.commit3f4a7322008-07-27 06:49:38 +090046 EXPECT_TRUE(a.message_loop());
47 a.Stop();
48 EXPECT_FALSE(a.message_loop());
maruel@google.comf6e3db82008-08-13 03:37:35 +090049 a.Stop();
50 EXPECT_FALSE(a.message_loop());
initial.commit3f4a7322008-07-27 06:49:38 +090051}
52
maruel@google.comf6e3db82008-08-13 03:37:35 +090053TEST(ThreadTest, StartWithStackSize) {
54 Thread a("StartWithStackSize");
55 // Ensure that the thread can work with only 12 kb and still process a
56 // message.
57 EXPECT_TRUE(a.StartWithStackSize(12*1024));
initial.commit3f4a7322008-07-27 06:49:38 +090058 EXPECT_TRUE(a.message_loop());
59
60 bool was_invoked = false;
61 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
62
63 // wait for the task to run (we could use a kernel event here
64 // instead to avoid busy waiting, but this is sufficient for
65 // testing purposes).
maruel@google.comf6e3db82008-08-13 03:37:35 +090066 for (int i = 100; i >= 0 && !was_invoked; --i) {
darin@google.comc18d7ae2008-08-21 18:46:32 +090067 PlatformThread::Sleep(10);
initial.commit3f4a7322008-07-27 06:49:38 +090068 }
69 EXPECT_TRUE(was_invoked);
70}
71
maruel@google.comf6e3db82008-08-13 03:37:35 +090072TEST(ThreadTest, TwoTasks) {
initial.commit3f4a7322008-07-27 06:49:38 +090073 bool was_invoked = false;
74 {
maruel@google.comf6e3db82008-08-13 03:37:35 +090075 Thread a("TwoTasks");
76 EXPECT_TRUE(a.Start());
initial.commit3f4a7322008-07-27 06:49:38 +090077 EXPECT_TRUE(a.message_loop());
78
79 // Test that all events are dispatched before the Thread object is
80 // destroyed. We do this by dispatching a sleep event before the
81 // event that will toggle our sentinel value.
maruel@google.comf6e3db82008-08-13 03:37:35 +090082 a.message_loop()->PostTask(FROM_HERE, new SleepSome(20));
initial.commit3f4a7322008-07-27 06:49:38 +090083 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
84 }
85 EXPECT_TRUE(was_invoked);
86}
87
maruel@google.comf6e3db82008-08-13 03:37:35 +090088TEST(ThreadTest, StopSoon) {
89 Thread a("StopSoon");
90 EXPECT_TRUE(a.Start());
91 EXPECT_TRUE(a.message_loop());
92 a.StopSoon();
93 EXPECT_FALSE(a.message_loop());
94 a.StopSoon();
95 EXPECT_FALSE(a.message_loop());
initial.commit3f4a7322008-07-27 06:49:38 +090096}
97
maruel@google.comf6e3db82008-08-13 03:37:35 +090098TEST(ThreadTest, ThreadName) {
99 Thread a("ThreadName");
100 EXPECT_TRUE(a.Start());
101 EXPECT_EQ("ThreadName", a.thread_name());
initial.commit3f4a7322008-07-27 06:49:38 +0900102}
license.botf003cfe2008-08-24 09:55:55 +0900103