gtest_stress_test.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Tests that SCOPED_TRACE() and various Google Test assertions can be
  30. // used in a large number of threads concurrently.
  31. #include "gtest/gtest.h"
  32. #include <vector>
  33. #include "src/gtest-internal-inl.h"
  34. #if GTEST_IS_THREADSAFE
  35. namespace testing {
  36. namespace {
  37. using internal::Notification;
  38. using internal::TestPropertyKeyIs;
  39. using internal::ThreadWithParam;
  40. using internal::scoped_ptr;
  41. // In order to run tests in this file, for platforms where Google Test is
  42. // thread safe, implement ThreadWithParam. See the description of its API
  43. // in gtest-port.h, where it is defined for already supported platforms.
  44. // How many threads to create?
  45. const int kThreadCount = 50;
  46. std::string IdToKey(int id, const char* suffix) {
  47. Message key;
  48. key << "key_" << id << "_" << suffix;
  49. return key.GetString();
  50. }
  51. std::string IdToString(int id) {
  52. Message id_message;
  53. id_message << id;
  54. return id_message.GetString();
  55. }
  56. void ExpectKeyAndValueWereRecordedForId(
  57. const std::vector<TestProperty>& properties,
  58. int id, const char* suffix) {
  59. TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
  60. const std::vector<TestProperty>::const_iterator property =
  61. std::find_if(properties.begin(), properties.end(), matches_key);
  62. ASSERT_TRUE(property != properties.end())
  63. << "expecting " << suffix << " value for id " << id;
  64. EXPECT_STREQ(IdToString(id).c_str(), property->value());
  65. }
  66. // Calls a large number of Google Test assertions, where exactly one of them
  67. // will fail.
  68. void ManyAsserts(int id) {
  69. GTEST_LOG_(INFO) << "Thread #" << id << " running...";
  70. SCOPED_TRACE(Message() << "Thread #" << id);
  71. for (int i = 0; i < kThreadCount; i++) {
  72. SCOPED_TRACE(Message() << "Iteration #" << i);
  73. // A bunch of assertions that should succeed.
  74. EXPECT_TRUE(true);
  75. ASSERT_FALSE(false) << "This shouldn't fail.";
  76. EXPECT_STREQ("a", "a");
  77. ASSERT_LE(5, 6);
  78. EXPECT_EQ(i, i) << "This shouldn't fail.";
  79. // RecordProperty() should interact safely with other threads as well.
  80. // The shared_key forces property updates.
  81. Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
  82. Test::RecordProperty(IdToKey(id, "int").c_str(), id);
  83. Test::RecordProperty("shared_key", IdToString(id).c_str());
  84. // This assertion should fail kThreadCount times per thread. It
  85. // is for testing whether Google Test can handle failed assertions in a
  86. // multi-threaded context.
  87. EXPECT_LT(i, 0) << "This should always fail.";
  88. }
  89. }
  90. void CheckTestFailureCount(int expected_failures) {
  91. const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
  92. const TestResult* const result = info->result();
  93. GTEST_CHECK_(expected_failures == result->total_part_count())
  94. << "Logged " << result->total_part_count() << " failures "
  95. << " vs. " << expected_failures << " expected";
  96. }
  97. // Tests using SCOPED_TRACE() and Google Test assertions in many threads
  98. // concurrently.
  99. TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
  100. {
  101. scoped_ptr<ThreadWithParam<int> > threads[kThreadCount];
  102. Notification threads_can_start;
  103. for (int i = 0; i != kThreadCount; i++)
  104. threads[i].reset(new ThreadWithParam<int>(&ManyAsserts,
  105. i,
  106. &threads_can_start));
  107. threads_can_start.Notify();
  108. // Blocks until all the threads are done.
  109. for (int i = 0; i != kThreadCount; i++)
  110. threads[i]->Join();
  111. }
  112. // Ensures that kThreadCount*kThreadCount failures have been reported.
  113. const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
  114. const TestResult* const result = info->result();
  115. std::vector<TestProperty> properties;
  116. // We have no access to the TestResult's list of properties but we can
  117. // copy them one by one.
  118. for (int i = 0; i < result->test_property_count(); ++i)
  119. properties.push_back(result->GetTestProperty(i));
  120. EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
  121. << "String and int values recorded on each thread, "
  122. << "as well as one shared_key";
  123. for (int i = 0; i < kThreadCount; ++i) {
  124. ExpectKeyAndValueWereRecordedForId(properties, i, "string");
  125. ExpectKeyAndValueWereRecordedForId(properties, i, "int");
  126. }
  127. CheckTestFailureCount(kThreadCount*kThreadCount);
  128. }
  129. void FailingThread(bool is_fatal) {
  130. if (is_fatal)
  131. FAIL() << "Fatal failure in some other thread. "
  132. << "(This failure is expected.)";
  133. else
  134. ADD_FAILURE() << "Non-fatal failure in some other thread. "
  135. << "(This failure is expected.)";
  136. }
  137. void GenerateFatalFailureInAnotherThread(bool is_fatal) {
  138. ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL);
  139. thread.Join();
  140. }
  141. TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
  142. EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
  143. // We should only have one failure (the one from
  144. // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
  145. // should succeed.
  146. CheckTestFailureCount(1);
  147. }
  148. void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
  149. ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
  150. }
  151. TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
  152. // Using a subroutine, to make sure, that the test continues.
  153. AssertNoFatalFailureIgnoresFailuresInOtherThreads();
  154. // We should only have one failure (the one from
  155. // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
  156. // should succeed.
  157. CheckTestFailureCount(1);
  158. }
  159. TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
  160. // This statement should fail, since the current thread doesn't generate a
  161. // fatal failure, only another one does.
  162. EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected");
  163. CheckTestFailureCount(2);
  164. }
  165. TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
  166. // This statement should succeed, because failures in all threads are
  167. // considered.
  168. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(
  169. GenerateFatalFailureInAnotherThread(true), "expected");
  170. CheckTestFailureCount(0);
  171. // We need to add a failure, because main() checks that there are failures.
  172. // But when only this test is run, we shouldn't have any failures.
  173. ADD_FAILURE() << "This is an expected non-fatal failure.";
  174. }
  175. TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
  176. // This statement should fail, since the current thread doesn't generate a
  177. // fatal failure, only another one does.
  178. EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false),
  179. "expected");
  180. CheckTestFailureCount(2);
  181. }
  182. TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
  183. // This statement should succeed, because failures in all threads are
  184. // considered.
  185. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(
  186. GenerateFatalFailureInAnotherThread(false), "expected");
  187. CheckTestFailureCount(0);
  188. // We need to add a failure, because main() checks that there are failures,
  189. // But when only this test is run, we shouldn't have any failures.
  190. ADD_FAILURE() << "This is an expected non-fatal failure.";
  191. }
  192. } // namespace
  193. } // namespace testing
  194. int main(int argc, char **argv) {
  195. testing::InitGoogleTest(&argc, argv);
  196. const int result = RUN_ALL_TESTS(); // Expected to fail.
  197. GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected";
  198. printf("\nPASS\n");
  199. return 0;
  200. }
  201. #else
  202. TEST(StressTest,
  203. DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
  204. }
  205. int main(int argc, char **argv) {
  206. testing::InitGoogleTest(&argc, argv);
  207. return RUN_ALL_TESTS();
  208. }
  209. #endif // GTEST_IS_THREADSAFE