initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1 | // 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 | |
| 36 | namespace { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 37 | |
| 38 | class 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 | |
| 49 | class SleepSome : public Task { |
| 50 | public: |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 51 | explicit SleepSome(int msec) : msec_(msec) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 52 | } |
| 53 | virtual void Run() { |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame^] | 54 | PlatformThread::Sleep(msec_); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | } |
| 56 | private: |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 57 | int msec_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 58 | }; |
| 59 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 60 | } // namespace |
| 61 | |
| 62 | TEST(ThreadTest, Restart) { |
| 63 | Thread a("Restart"); |
| 64 | a.Stop(); |
| 65 | EXPECT_FALSE(a.message_loop()); |
| 66 | EXPECT_TRUE(a.Start()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 67 | EXPECT_TRUE(a.message_loop()); |
| 68 | a.Stop(); |
| 69 | EXPECT_FALSE(a.message_loop()); |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 70 | EXPECT_TRUE(a.Start()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 71 | EXPECT_TRUE(a.message_loop()); |
| 72 | a.Stop(); |
| 73 | EXPECT_FALSE(a.message_loop()); |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 74 | a.Stop(); |
| 75 | EXPECT_FALSE(a.message_loop()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 76 | } |
| 77 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 78 | TEST(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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 83 | 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.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 91 | for (int i = 100; i >= 0 && !was_invoked; --i) { |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame^] | 92 | PlatformThread::Sleep(10); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 93 | } |
| 94 | EXPECT_TRUE(was_invoked); |
| 95 | } |
| 96 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 97 | TEST(ThreadTest, TwoTasks) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 98 | bool was_invoked = false; |
| 99 | { |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 100 | Thread a("TwoTasks"); |
| 101 | EXPECT_TRUE(a.Start()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 102 | 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.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 107 | a.message_loop()->PostTask(FROM_HERE, new SleepSome(20)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); |
| 109 | } |
| 110 | EXPECT_TRUE(was_invoked); |
| 111 | } |
| 112 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 113 | TEST(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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 121 | } |
| 122 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 123 | TEST(ThreadTest, ThreadName) { |
| 124 | Thread a("ThreadName"); |
| 125 | EXPECT_TRUE(a.Start()); |
| 126 | EXPECT_EQ("ThreadName", a.thread_name()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 127 | } |