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 30 // Tests that SCOPED_TRACE() and various Google Test assertions can be 31 // used in a large number of threads concurrently. 32 33 #include <algorithm> 34 #include <memory> 35 #include <string> 36 #include <vector> 37 38 #include "gtest/gtest.h" 39 #include "src/gtest-internal-inl.h" 40 41 #ifdef GTEST_IS_THREADSAFE 42 43 namespace testing { 44 namespace { 45 46 using internal::Notification; 47 using internal::TestPropertyKeyIs; 48 using internal::ThreadWithParam; 49 50 // In order to run tests in this file, for platforms where Google Test is 51 // thread safe, implement ThreadWithParam. See the description of its API 52 // in gtest-port.h, where it is defined for already supported platforms. 53 54 // How many threads to create? 55 const int kThreadCount = 50; 56 57 std::string IdToKey(int id, const char* suffix) { 58 Message key; 59 key << "key_" << id << "_" << suffix; 60 return key.GetString(); 61 } 62 63 std::string IdToString(int id) { 64 Message id_message; 65 id_message << id; 66 return id_message.GetString(); 67 } 68 69 void ExpectKeyAndValueWereRecordedForId( 70 const std::vector<TestProperty>& properties, int id, const char* suffix) { 71 TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str()); 72 const std::vector<TestProperty>::const_iterator property = 73 std::find_if(properties.begin(), properties.end(), matches_key); 74 ASSERT_TRUE(property != properties.end()) 75 << "expecting " << suffix << " value for id " << id; 76 EXPECT_STREQ(IdToString(id).c_str(), property->value()); 77 } 78 79 // Calls a large number of Google Test assertions, where exactly one of them 80 // will fail. 81 void ManyAsserts(int id) { 82 GTEST_LOG_(INFO) << "Thread #" << id << " running..."; 83 84 SCOPED_TRACE(Message() << "Thread #" << id); 85 86 for (int i = 0; i < kThreadCount; i++) { 87 SCOPED_TRACE(Message() << "Iteration #" << i); 88 89 // A bunch of assertions that should succeed. 90 EXPECT_TRUE(true); 91 ASSERT_FALSE(false) << "This shouldn't fail."; 92 EXPECT_STREQ("a", "a"); 93 ASSERT_LE(5, 6); 94 EXPECT_EQ(i, i) << "This shouldn't fail."; 95 96 // RecordProperty() should interact safely with other threads as well. 97 // The shared_key forces property updates. 98 Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str()); 99 Test::RecordProperty(IdToKey(id, "int").c_str(), id); 100 Test::RecordProperty("shared_key", IdToString(id).c_str()); 101 102 // This assertion should fail kThreadCount times per thread. It 103 // is for testing whether Google Test can handle failed assertions in a 104 // multi-threaded context. 105 EXPECT_LT(i, 0) << "This should always fail."; 106 } 107 } 108 109 void CheckTestFailureCount(int expected_failures) { 110 const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); 111 const TestResult* const result = info->result(); 112 GTEST_CHECK_(expected_failures == result->total_part_count()) 113 << "Logged " << result->total_part_count() << " failures " 114 << " vs. " << expected_failures << " expected"; 115 } 116 117 // Tests using SCOPED_TRACE() and Google Test assertions in many threads 118 // concurrently. 119 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) { 120 { 121 std::unique_ptr<ThreadWithParam<int> > threads[kThreadCount]; 122 Notification threads_can_start; 123 for (int i = 0; i != kThreadCount; i++) 124 threads[i] = std::make_unique<ThreadWithParam<int>>(&ManyAsserts, i, 125 &threads_can_start); 126 127 threads_can_start.Notify(); 128 129 // Blocks until all the threads are done. 130 for (int i = 0; i != kThreadCount; i++) threads[i]->Join(); 131 } 132 133 // Ensures that kThreadCount*kThreadCount failures have been reported. 134 const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); 135 const TestResult* const result = info->result(); 136 137 std::vector<TestProperty> properties; 138 // We have no access to the TestResult's list of properties but we can 139 // copy them one by one. 140 for (int i = 0; i < result->test_property_count(); ++i) 141 properties.push_back(result->GetTestProperty(i)); 142 143 EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count()) 144 << "String and int values recorded on each thread, " 145 << "as well as one shared_key"; 146 for (int i = 0; i < kThreadCount; ++i) { 147 ExpectKeyAndValueWereRecordedForId(properties, i, "string"); 148 ExpectKeyAndValueWereRecordedForId(properties, i, "int"); 149 } 150 CheckTestFailureCount(kThreadCount * kThreadCount); 151 } 152 153 void FailingThread(bool is_fatal) { 154 if (is_fatal) 155 FAIL() << "Fatal failure in some other thread. " 156 << "(This failure is expected.)"; 157 else 158 ADD_FAILURE() << "Non-fatal failure in some other thread. " 159 << "(This failure is expected.)"; 160 } 161 162 void GenerateFatalFailureInAnotherThread(bool is_fatal) { 163 ThreadWithParam<bool> thread(&FailingThread, is_fatal, nullptr); 164 thread.Join(); 165 } 166 167 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) { 168 EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true)); 169 // We should only have one failure (the one from 170 // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE 171 // should succeed. 172 CheckTestFailureCount(1); 173 } 174 175 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() { 176 ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true)); 177 } 178 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) { 179 // Using a subroutine, to make sure, that the test continues. 180 AssertNoFatalFailureIgnoresFailuresInOtherThreads(); 181 // We should only have one failure (the one from 182 // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE 183 // should succeed. 184 CheckTestFailureCount(1); 185 } 186 187 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) { 188 // This statement should fail, since the current thread doesn't generate a 189 // fatal failure, only another one does. 190 EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected"); 191 CheckTestFailureCount(2); 192 } 193 194 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) { 195 // This statement should succeed, because failures in all threads are 196 // considered. 197 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(GenerateFatalFailureInAnotherThread(true), 198 "expected"); 199 CheckTestFailureCount(0); 200 // We need to add a failure, because main() checks that there are failures. 201 // But when only this test is run, we shouldn't have any failures. 202 ADD_FAILURE() << "This is an expected non-fatal failure."; 203 } 204 205 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) { 206 // This statement should fail, since the current thread doesn't generate a 207 // fatal failure, only another one does. 208 EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false), 209 "expected"); 210 CheckTestFailureCount(2); 211 } 212 213 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) { 214 // This statement should succeed, because failures in all threads are 215 // considered. 216 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( 217 GenerateFatalFailureInAnotherThread(false), "expected"); 218 CheckTestFailureCount(0); 219 // We need to add a failure, because main() checks that there are failures, 220 // But when only this test is run, we shouldn't have any failures. 221 ADD_FAILURE() << "This is an expected non-fatal failure."; 222 } 223 224 } // namespace 225 } // namespace testing 226 227 int main(int argc, char** argv) { 228 testing::InitGoogleTest(&argc, argv); 229 230 const int result = RUN_ALL_TESTS(); // Expected to fail. 231 GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected"; 232 233 printf("\nPASS\n"); 234 return 0; 235 } 236 237 #else 238 TEST(StressTest, 239 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {} 240 241 int main(int argc, char **argv) { 242 testing::InitGoogleTest(&argc, argv); 243 return RUN_ALL_TESTS(); 244 } 245 #endif // GTEST_IS_THREADSAFE 246