license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 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" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 10 | #include "testing/platform_test.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 12 | using base::Thread; |
| 13 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 14 | typedef PlatformTest ThreadTest; |
| 15 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | |
| 18 | class ToggleValue : public Task { |
| 19 | public: |
| 20 | explicit ToggleValue(bool* value) : value_(value) { |
| 21 | } |
| 22 | virtual void Run() { |
| 23 | *value_ = !*value_; |
| 24 | } |
| 25 | private: |
| 26 | bool* value_; |
| 27 | }; |
| 28 | |
| 29 | class SleepSome : public Task { |
| 30 | public: |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 31 | explicit SleepSome(int msec) : msec_(msec) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | } |
| 33 | virtual void Run() { |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 34 | PlatformThread::Sleep(msec_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | } |
| 36 | private: |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 37 | int msec_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | }; |
| 39 | |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 40 | class SleepInsideInitThread : public Thread { |
| 41 | public: |
| 42 | SleepInsideInitThread() : Thread("none") { init_called_ = false; } |
| 43 | virtual ~SleepInsideInitThread() { } |
| 44 | |
| 45 | virtual void Init() { |
| 46 | PlatformThread::Sleep(500); |
| 47 | init_called_ = true; |
| 48 | } |
| 49 | bool InitCalled() { return init_called_; } |
| 50 | private: |
| 51 | bool init_called_; |
| 52 | }; |
| 53 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 54 | } // namespace |
| 55 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 56 | TEST_F(ThreadTest, Restart) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 57 | Thread a("Restart"); |
| 58 | a.Stop(); |
| 59 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 60 | EXPECT_FALSE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 61 | EXPECT_TRUE(a.Start()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 63 | EXPECT_TRUE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | a.Stop(); |
| 65 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 66 | EXPECT_FALSE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 67 | EXPECT_TRUE(a.Start()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 69 | EXPECT_TRUE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 70 | a.Stop(); |
| 71 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 72 | EXPECT_FALSE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 73 | a.Stop(); |
| 74 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 75 | EXPECT_FALSE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 78 | TEST_F(ThreadTest, StartWithOptions_StackSize) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 79 | Thread a("StartWithStackSize"); |
| 80 | // Ensure that the thread can work with only 12 kb and still process a |
| 81 | // message. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 82 | Thread::Options options; |
| 83 | options.stack_size = 12*1024; |
| 84 | EXPECT_TRUE(a.StartWithOptions(options)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 86 | EXPECT_TRUE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | |
| 88 | bool was_invoked = false; |
| 89 | a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); |
| 90 | |
| 91 | // wait for the task to run (we could use a kernel event here |
| 92 | // instead to avoid busy waiting, but this is sufficient for |
| 93 | // testing purposes). |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 94 | for (int i = 100; i >= 0 && !was_invoked; --i) { |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 95 | PlatformThread::Sleep(10); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | } |
| 97 | EXPECT_TRUE(was_invoked); |
| 98 | } |
| 99 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 100 | TEST_F(ThreadTest, TwoTasks) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | bool was_invoked = false; |
| 102 | { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 103 | Thread a("TwoTasks"); |
| 104 | EXPECT_TRUE(a.Start()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | EXPECT_TRUE(a.message_loop()); |
| 106 | |
| 107 | // Test that all events are dispatched before the Thread object is |
| 108 | // destroyed. We do this by dispatching a sleep event before the |
| 109 | // event that will toggle our sentinel value. |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 110 | a.message_loop()->PostTask(FROM_HERE, new SleepSome(20)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); |
| 112 | } |
| 113 | EXPECT_TRUE(was_invoked); |
| 114 | } |
| 115 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 116 | TEST_F(ThreadTest, StopSoon) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 117 | Thread a("StopSoon"); |
| 118 | EXPECT_TRUE(a.Start()); |
| 119 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 120 | EXPECT_TRUE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 121 | a.StopSoon(); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 122 | a.StopSoon(); |
[email protected] | 98c74f8 | 2008-12-01 14:34:42 | [diff] [blame] | 123 | a.Stop(); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 124 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 125 | EXPECT_FALSE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 128 | TEST_F(ThreadTest, ThreadName) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 129 | Thread a("ThreadName"); |
| 130 | EXPECT_TRUE(a.Start()); |
| 131 | EXPECT_EQ("ThreadName", a.thread_name()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 132 | } |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 133 | |
| 134 | // Make sure we can't use a thread between Start() and Init(). |
| 135 | TEST_F(ThreadTest, SleepInsideInit) { |
| 136 | SleepInsideInitThread t; |
| 137 | EXPECT_FALSE(t.InitCalled()); |
| 138 | t.Start(); |
| 139 | EXPECT_TRUE(t.InitCalled()); |
| 140 | } |