diff options
author | deanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-29 07:54:54 +0000 |
---|---|---|
committer | deanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-29 07:54:54 +0000 |
commit | 8a2cebb5dc1393c74c1921852369cc45e291914e (patch) | |
tree | fd6bf76b0f7d6b3469873adb2983bb9fdb80451c /base/simple_thread_unittest.cc | |
parent | 7a8475c6da19434a3bae6783279fd9e9ad79fc47 (diff) | |
download | chromium_src-8a2cebb5dc1393c74c1921852369cc45e291914e.zip chromium_src-8a2cebb5dc1393c74c1921852369cc45e291914e.tar.gz chromium_src-8a2cebb5dc1393c74c1921852369cc45e291914e.tar.bz2 |
Bring back SimpleThread, but with a Delegate interface.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1519 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/simple_thread_unittest.cc')
-rw-r--r-- | base/simple_thread_unittest.cc | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/base/simple_thread_unittest.cc b/base/simple_thread_unittest.cc new file mode 100644 index 0000000..3e55858 --- /dev/null +++ b/base/simple_thread_unittest.cc @@ -0,0 +1,99 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/simple_thread.h" +#include "base/string_util.h" +#include "base/waitable_event.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class SetIntRunner : public base::DelegateSimpleThread::Delegate { + public: + SetIntRunner(int* ptr, int val) : ptr_(ptr), val_(val) { } + ~SetIntRunner() { } + + virtual void Run() { + *ptr_ = val_; + } + + private: + int* ptr_; + int val_; +}; + +class WaitEventRunner : public base::DelegateSimpleThread::Delegate { + public: + WaitEventRunner(base::WaitableEvent* event) : event_(event) { } + ~WaitEventRunner() { } + + virtual void Run() { + EXPECT_FALSE(event_->IsSignaled()); + event_->Signal(); + EXPECT_TRUE(event_->IsSignaled()); + } + private: + base::WaitableEvent* event_; +}; + +} // namespace + +TEST(SimpleThreadTest, CreateAndJoin) { + int stack_int = 0; + + SetIntRunner runner(&stack_int, 7); + EXPECT_EQ(0, stack_int); + + base::DelegateSimpleThread thread(&runner, "int_setter"); + EXPECT_FALSE(thread.HasBeenStarted()); + EXPECT_FALSE(thread.HasBeenJoined()); + EXPECT_EQ(0, stack_int); + + thread.Start(); + EXPECT_TRUE(thread.HasBeenStarted()); + EXPECT_FALSE(thread.HasBeenJoined()); + + thread.Join(); + EXPECT_TRUE(thread.HasBeenStarted()); + EXPECT_TRUE(thread.HasBeenJoined()); + EXPECT_EQ(7, stack_int); +} + +TEST(SimpleThreadTest, WaitForEvent) { + // Create a thread, and wait for it to signal us. + base::WaitableEvent event(true, false); + + WaitEventRunner runner(&event); + base::DelegateSimpleThread thread(&runner, "event_waiter"); + + EXPECT_FALSE(event.IsSignaled()); + thread.Start(); + event.Wait(); + EXPECT_TRUE(event.IsSignaled()); + thread.Join(); +} + +TEST(SimpleThreadTest, NamedWithOptions) { + base::WaitableEvent event(true, false); + + WaitEventRunner runner(&event); + base::SimpleThread::Options options; + base::DelegateSimpleThread thread(&runner, "event_waiter", options); + EXPECT_EQ(thread.name_prefix(), "event_waiter"); + EXPECT_FALSE(event.IsSignaled()); + + thread.Start(); + EXPECT_EQ(thread.name_prefix(), "event_waiter"); + EXPECT_EQ(thread.name(), std::string("event_waiter/") + + IntToString(thread.tid())); + event.Wait(); + + EXPECT_TRUE(event.IsSignaled()); + thread.Join(); + + // We keep the name and tid, even after the thread is gone. + EXPECT_EQ(thread.name_prefix(), "event_waiter"); + EXPECT_EQ(thread.name(), std::string("event_waiter/") + + IntToString(thread.tid())); +} |