ThreadsTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include "Action.h"
  18. #include "Pointers.h"
  19. #include "Thread.h"
  20. #include "Threads.h"
  21. TEST(ThreadsTest, single_thread) {
  22. Pointers pointers(2);
  23. Threads threads(&pointers, 1);
  24. Thread* thread = threads.CreateThread(900);
  25. ASSERT_TRUE(thread != nullptr);
  26. ASSERT_EQ(1U, threads.num_threads());
  27. Thread* found_thread = threads.FindThread(900);
  28. ASSERT_EQ(thread, found_thread);
  29. thread->CreateAction(0x1234, "thread_done", "");
  30. thread->SetPending();
  31. threads.Finish(thread);
  32. ASSERT_EQ(0U, threads.num_threads());
  33. }
  34. TEST(ThreadsTest, multiple_threads) {
  35. Pointers pointers(4);
  36. Threads threads(&pointers, 1);
  37. Thread* thread1 = threads.CreateThread(900);
  38. ASSERT_TRUE(thread1 != nullptr);
  39. ASSERT_EQ(1U, threads.num_threads());
  40. Thread* thread2 = threads.CreateThread(901);
  41. ASSERT_TRUE(thread2 != nullptr);
  42. ASSERT_EQ(2U, threads.num_threads());
  43. Thread* thread3 = threads.CreateThread(902);
  44. ASSERT_TRUE(thread3 != nullptr);
  45. ASSERT_EQ(3U, threads.num_threads());
  46. Thread* found_thread1 = threads.FindThread(900);
  47. ASSERT_EQ(thread1, found_thread1);
  48. Thread* found_thread2 = threads.FindThread(901);
  49. ASSERT_EQ(thread2, found_thread2);
  50. Thread* found_thread3 = threads.FindThread(902);
  51. ASSERT_EQ(thread3, found_thread3);
  52. thread1->CreateAction(0x1234, "thread_done", "");
  53. thread2->CreateAction(0x1235, "thread_done", "");
  54. thread3->CreateAction(0x1236, "thread_done", "");
  55. thread1->SetPending();
  56. threads.Finish(thread1);
  57. ASSERT_EQ(2U, threads.num_threads());
  58. thread3->SetPending();
  59. threads.Finish(thread3);
  60. ASSERT_EQ(1U, threads.num_threads());
  61. thread2->SetPending();
  62. threads.Finish(thread2);
  63. ASSERT_EQ(0U, threads.num_threads());
  64. }
  65. TEST(ThreadsTest, verify_quiesce) {
  66. Pointers pointers(4);
  67. Threads threads(&pointers, 1);
  68. Thread* thread = threads.CreateThread(900);
  69. ASSERT_TRUE(thread != nullptr);
  70. ASSERT_EQ(1U, threads.num_threads());
  71. // If WaitForAllToQuiesce is not correct, then this should provoke an error
  72. // since we are overwriting the action data while it's being used.
  73. for (size_t i = 0; i < 512; i++) {
  74. thread->CreateAction(0x1234 + i, "malloc", "100");
  75. thread->SetPending();
  76. threads.WaitForAllToQuiesce();
  77. thread->CreateAction(0x1234 + i, "free", "");
  78. thread->SetPending();
  79. threads.WaitForAllToQuiesce();
  80. }
  81. thread->CreateAction(0x1236, "thread_done", "");
  82. thread->SetPending();
  83. threads.Finish(thread);
  84. ASSERT_EQ(0U, threads.num_threads());
  85. }
  86. static void TestTooManyThreads() {
  87. Pointers pointers(4);
  88. Threads threads(&pointers, 1);
  89. for (size_t i = 0; i <= threads.max_threads(); i++) {
  90. Thread* thread = threads.CreateThread(900+i);
  91. ASSERT_EQ(thread, threads.FindThread(900+i));
  92. }
  93. }
  94. TEST(ThreadsTest, too_many_threads) {
  95. ASSERT_EXIT(TestTooManyThreads(), ::testing::ExitedWithCode(1), "");
  96. }