license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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" |
| 10 | |
| 11 | namespace { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 12 | |
| 13 | class 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 | |
| 24 | class SleepSome : public Task { |
| 25 | public: |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 26 | explicit SleepSome(int msec) : msec_(msec) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 27 | } |
| 28 | virtual void Run() { |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 29 | PlatformThread::Sleep(msec_); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 30 | } |
| 31 | private: |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 32 | int msec_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 33 | }; |
| 34 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 35 | } // namespace |
| 36 | |
| 37 | TEST(ThreadTest, Restart) { |
| 38 | Thread a("Restart"); |
| 39 | a.Stop(); |
| 40 | EXPECT_FALSE(a.message_loop()); |
| 41 | EXPECT_TRUE(a.Start()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 42 | EXPECT_TRUE(a.message_loop()); |
| 43 | a.Stop(); |
| 44 | EXPECT_FALSE(a.message_loop()); |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 45 | EXPECT_TRUE(a.Start()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 46 | EXPECT_TRUE(a.message_loop()); |
| 47 | a.Stop(); |
| 48 | EXPECT_FALSE(a.message_loop()); |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 49 | a.Stop(); |
| 50 | EXPECT_FALSE(a.message_loop()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 51 | } |
| 52 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 53 | TEST(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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 58 | 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.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 66 | for (int i = 100; i >= 0 && !was_invoked; --i) { |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 67 | PlatformThread::Sleep(10); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 68 | } |
| 69 | EXPECT_TRUE(was_invoked); |
| 70 | } |
| 71 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 72 | TEST(ThreadTest, TwoTasks) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 73 | bool was_invoked = false; |
| 74 | { |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 75 | Thread a("TwoTasks"); |
| 76 | EXPECT_TRUE(a.Start()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 77 | 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.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 82 | a.message_loop()->PostTask(FROM_HERE, new SleepSome(20)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 83 | a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); |
| 84 | } |
| 85 | EXPECT_TRUE(was_invoked); |
| 86 | } |
| 87 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 88 | TEST(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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 96 | } |
| 97 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 98 | TEST(ThreadTest, ThreadName) { |
| 99 | Thread a("ThreadName"); |
| 100 | EXPECT_TRUE(a.Start()); |
| 101 | EXPECT_EQ("ThreadName", a.thread_name()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 102 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 103 | |