Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / threading / platform_thread_unittest.cc
blob59f29dadc3facc57c0a4385df31e1912f3eb9b83
1 // Copyright (c) 2012 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.
5 #include "base/compiler_specific.h"
6 #include "base/threading/platform_thread.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace base {
12 // Trivial tests that thread runs and doesn't crash on create and join ---------
14 class TrivialThread : public PlatformThread::Delegate {
15 public:
16 TrivialThread() : did_run_(false) {}
18 virtual void ThreadMain() OVERRIDE {
19 did_run_ = true;
22 bool did_run() const { return did_run_; }
24 private:
25 bool did_run_;
27 DISALLOW_COPY_AND_ASSIGN(TrivialThread);
30 TEST(PlatformThreadTest, Trivial) {
31 TrivialThread thread;
32 PlatformThreadHandle handle;
34 ASSERT_FALSE(thread.did_run());
35 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
36 PlatformThread::Join(handle);
37 ASSERT_TRUE(thread.did_run());
40 TEST(PlatformThreadTest, TrivialTimesTen) {
41 TrivialThread thread[10];
42 PlatformThreadHandle handle[arraysize(thread)];
44 for (size_t n = 0; n < arraysize(thread); n++)
45 ASSERT_FALSE(thread[n].did_run());
46 for (size_t n = 0; n < arraysize(thread); n++)
47 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
48 for (size_t n = 0; n < arraysize(thread); n++)
49 PlatformThread::Join(handle[n]);
50 for (size_t n = 0; n < arraysize(thread); n++)
51 ASSERT_TRUE(thread[n].did_run());
54 // Tests of basic thread functions ---------------------------------------------
56 class FunctionTestThread : public TrivialThread {
57 public:
58 FunctionTestThread() : thread_id_(0) {}
60 virtual void ThreadMain() OVERRIDE {
61 thread_id_ = PlatformThread::CurrentId();
62 PlatformThread::YieldCurrentThread();
63 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50));
65 // Make sure that the thread ID is the same across calls.
66 EXPECT_EQ(thread_id_, PlatformThread::CurrentId());
68 TrivialThread::ThreadMain();
71 PlatformThreadId thread_id() const { return thread_id_; }
73 private:
74 PlatformThreadId thread_id_;
76 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread);
79 TEST(PlatformThreadTest, Function) {
80 PlatformThreadId main_thread_id = PlatformThread::CurrentId();
82 FunctionTestThread thread;
83 PlatformThreadHandle handle;
85 ASSERT_FALSE(thread.did_run());
86 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
87 PlatformThread::Join(handle);
88 ASSERT_TRUE(thread.did_run());
89 EXPECT_NE(thread.thread_id(), main_thread_id);
91 // Make sure that the thread ID is the same across calls.
92 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
95 TEST(PlatformThreadTest, FunctionTimesTen) {
96 PlatformThreadId main_thread_id = PlatformThread::CurrentId();
98 FunctionTestThread thread[10];
99 PlatformThreadHandle handle[arraysize(thread)];
101 for (size_t n = 0; n < arraysize(thread); n++)
102 ASSERT_FALSE(thread[n].did_run());
103 for (size_t n = 0; n < arraysize(thread); n++)
104 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
105 for (size_t n = 0; n < arraysize(thread); n++)
106 PlatformThread::Join(handle[n]);
107 for (size_t n = 0; n < arraysize(thread); n++) {
108 ASSERT_TRUE(thread[n].did_run());
109 EXPECT_NE(thread[n].thread_id(), main_thread_id);
111 // Make sure no two threads get the same ID.
112 for (size_t i = 0; i < n; ++i) {
113 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id());
117 // Make sure that the thread ID is the same across calls.
118 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
121 } // namespace base