platform_thread_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.
  4. #include "butil/compiler_specific.h"
  5. #include "butil/threading/platform_thread.h"
  6. #include <gtest/gtest.h>
  7. namespace butil {
  8. // Trivial tests that thread runs and doesn't crash on create and join ---------
  9. class TrivialThread : public PlatformThread::Delegate {
  10. public:
  11. TrivialThread() : did_run_(false) {}
  12. virtual void ThreadMain() OVERRIDE {
  13. did_run_ = true;
  14. }
  15. bool did_run() const { return did_run_; }
  16. private:
  17. bool did_run_;
  18. DISALLOW_COPY_AND_ASSIGN(TrivialThread);
  19. };
  20. TEST(PlatformThreadTest, Trivial) {
  21. TrivialThread thread;
  22. PlatformThreadHandle handle;
  23. ASSERT_FALSE(thread.did_run());
  24. ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
  25. PlatformThread::Join(handle);
  26. ASSERT_TRUE(thread.did_run());
  27. }
  28. TEST(PlatformThreadTest, TrivialTimesTen) {
  29. TrivialThread thread[10];
  30. PlatformThreadHandle handle[arraysize(thread)];
  31. for (size_t n = 0; n < arraysize(thread); n++)
  32. ASSERT_FALSE(thread[n].did_run());
  33. for (size_t n = 0; n < arraysize(thread); n++)
  34. ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
  35. for (size_t n = 0; n < arraysize(thread); n++)
  36. PlatformThread::Join(handle[n]);
  37. for (size_t n = 0; n < arraysize(thread); n++)
  38. ASSERT_TRUE(thread[n].did_run());
  39. }
  40. // Tests of basic thread functions ---------------------------------------------
  41. class FunctionTestThread : public TrivialThread {
  42. public:
  43. FunctionTestThread() : thread_id_(0) {}
  44. virtual void ThreadMain() OVERRIDE {
  45. thread_id_ = PlatformThread::CurrentId();
  46. PlatformThread::YieldCurrentThread();
  47. PlatformThread::Sleep(TimeDelta::FromMilliseconds(50));
  48. // Make sure that the thread ID is the same across calls.
  49. EXPECT_EQ(thread_id_, PlatformThread::CurrentId());
  50. TrivialThread::ThreadMain();
  51. }
  52. PlatformThreadId thread_id() const { return thread_id_; }
  53. private:
  54. PlatformThreadId thread_id_;
  55. DISALLOW_COPY_AND_ASSIGN(FunctionTestThread);
  56. };
  57. TEST(PlatformThreadTest, Function) {
  58. PlatformThreadId main_thread_id = PlatformThread::CurrentId();
  59. FunctionTestThread thread;
  60. PlatformThreadHandle handle;
  61. ASSERT_FALSE(thread.did_run());
  62. ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
  63. PlatformThread::Join(handle);
  64. ASSERT_TRUE(thread.did_run());
  65. EXPECT_NE(thread.thread_id(), main_thread_id);
  66. // Make sure that the thread ID is the same across calls.
  67. EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
  68. }
  69. TEST(PlatformThreadTest, FunctionTimesTen) {
  70. PlatformThreadId main_thread_id = PlatformThread::CurrentId();
  71. FunctionTestThread thread[10];
  72. PlatformThreadHandle handle[arraysize(thread)];
  73. for (size_t n = 0; n < arraysize(thread); n++)
  74. ASSERT_FALSE(thread[n].did_run());
  75. for (size_t n = 0; n < arraysize(thread); n++)
  76. ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
  77. for (size_t n = 0; n < arraysize(thread); n++)
  78. PlatformThread::Join(handle[n]);
  79. for (size_t n = 0; n < arraysize(thread); n++) {
  80. ASSERT_TRUE(thread[n].did_run());
  81. EXPECT_NE(thread[n].thread_id(), main_thread_id);
  82. // Make sure no two threads get the same ID.
  83. for (size_t i = 0; i < n; ++i) {
  84. EXPECT_NE(thread[i].thread_id(), thread[n].thread_id());
  85. }
  86. }
  87. // Make sure that the thread ID is the same across calls.
  88. EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
  89. }
  90. } // namespace butil