michael@0: // Copyright 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: // Tests that SCOPED_TRACE() and various Google Test assertions can be michael@0: // used in a large number of threads concurrently. michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: // We must define this macro in order to #include michael@0: // gtest-internal-inl.h. This is how Google Test prevents a user from michael@0: // accidentally depending on its internal implementation. michael@0: #define GTEST_IMPLEMENTATION_ 1 michael@0: #include "src/gtest-internal-inl.h" michael@0: #undef GTEST_IMPLEMENTATION_ michael@0: michael@0: #if GTEST_IS_THREADSAFE michael@0: michael@0: namespace testing { michael@0: namespace { michael@0: michael@0: using internal::Notification; michael@0: using internal::String; michael@0: using internal::TestPropertyKeyIs; michael@0: using internal::ThreadWithParam; michael@0: using internal::scoped_ptr; michael@0: michael@0: // In order to run tests in this file, for platforms where Google Test is michael@0: // thread safe, implement ThreadWithParam. See the description of its API michael@0: // in gtest-port.h, where it is defined for already supported platforms. michael@0: michael@0: // How many threads to create? michael@0: const int kThreadCount = 50; michael@0: michael@0: String IdToKey(int id, const char* suffix) { michael@0: Message key; michael@0: key << "key_" << id << "_" << suffix; michael@0: return key.GetString(); michael@0: } michael@0: michael@0: String IdToString(int id) { michael@0: Message id_message; michael@0: id_message << id; michael@0: return id_message.GetString(); michael@0: } michael@0: michael@0: void ExpectKeyAndValueWereRecordedForId( michael@0: const std::vector& properties, michael@0: int id, const char* suffix) { michael@0: TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str()); michael@0: const std::vector::const_iterator property = michael@0: std::find_if(properties.begin(), properties.end(), matches_key); michael@0: ASSERT_TRUE(property != properties.end()) michael@0: << "expecting " << suffix << " value for id " << id; michael@0: EXPECT_STREQ(IdToString(id).c_str(), property->value()); michael@0: } michael@0: michael@0: // Calls a large number of Google Test assertions, where exactly one of them michael@0: // will fail. michael@0: void ManyAsserts(int id) { michael@0: GTEST_LOG_(INFO) << "Thread #" << id << " running..."; michael@0: michael@0: SCOPED_TRACE(Message() << "Thread #" << id); michael@0: michael@0: for (int i = 0; i < kThreadCount; i++) { michael@0: SCOPED_TRACE(Message() << "Iteration #" << i); michael@0: michael@0: // A bunch of assertions that should succeed. michael@0: EXPECT_TRUE(true); michael@0: ASSERT_FALSE(false) << "This shouldn't fail."; michael@0: EXPECT_STREQ("a", "a"); michael@0: ASSERT_LE(5, 6); michael@0: EXPECT_EQ(i, i) << "This shouldn't fail."; michael@0: michael@0: // RecordProperty() should interact safely with other threads as well. michael@0: // The shared_key forces property updates. michael@0: Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str()); michael@0: Test::RecordProperty(IdToKey(id, "int").c_str(), id); michael@0: Test::RecordProperty("shared_key", IdToString(id).c_str()); michael@0: michael@0: // This assertion should fail kThreadCount times per thread. It michael@0: // is for testing whether Google Test can handle failed assertions in a michael@0: // multi-threaded context. michael@0: EXPECT_LT(i, 0) << "This should always fail."; michael@0: } michael@0: } michael@0: michael@0: void CheckTestFailureCount(int expected_failures) { michael@0: const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); michael@0: const TestResult* const result = info->result(); michael@0: GTEST_CHECK_(expected_failures == result->total_part_count()) michael@0: << "Logged " << result->total_part_count() << " failures " michael@0: << " vs. " << expected_failures << " expected"; michael@0: } michael@0: michael@0: // Tests using SCOPED_TRACE() and Google Test assertions in many threads michael@0: // concurrently. michael@0: TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) { michael@0: { michael@0: scoped_ptr > threads[kThreadCount]; michael@0: Notification threads_can_start; michael@0: for (int i = 0; i != kThreadCount; i++) michael@0: threads[i].reset(new ThreadWithParam(&ManyAsserts, michael@0: i, michael@0: &threads_can_start)); michael@0: michael@0: threads_can_start.Notify(); michael@0: michael@0: // Blocks until all the threads are done. michael@0: for (int i = 0; i != kThreadCount; i++) michael@0: threads[i]->Join(); michael@0: } michael@0: michael@0: // Ensures that kThreadCount*kThreadCount failures have been reported. michael@0: const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); michael@0: const TestResult* const result = info->result(); michael@0: michael@0: std::vector properties; michael@0: // We have no access to the TestResult's list of properties but we can michael@0: // copy them one by one. michael@0: for (int i = 0; i < result->test_property_count(); ++i) michael@0: properties.push_back(result->GetTestProperty(i)); michael@0: michael@0: EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count()) michael@0: << "String and int values recorded on each thread, " michael@0: << "as well as one shared_key"; michael@0: for (int i = 0; i < kThreadCount; ++i) { michael@0: ExpectKeyAndValueWereRecordedForId(properties, i, "string"); michael@0: ExpectKeyAndValueWereRecordedForId(properties, i, "int"); michael@0: } michael@0: CheckTestFailureCount(kThreadCount*kThreadCount); michael@0: } michael@0: michael@0: void FailingThread(bool is_fatal) { michael@0: if (is_fatal) michael@0: FAIL() << "Fatal failure in some other thread. " michael@0: << "(This failure is expected.)"; michael@0: else michael@0: ADD_FAILURE() << "Non-fatal failure in some other thread. " michael@0: << "(This failure is expected.)"; michael@0: } michael@0: michael@0: void GenerateFatalFailureInAnotherThread(bool is_fatal) { michael@0: ThreadWithParam thread(&FailingThread, is_fatal, NULL); michael@0: thread.Join(); michael@0: } michael@0: michael@0: TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) { michael@0: EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true)); michael@0: // We should only have one failure (the one from michael@0: // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE michael@0: // should succeed. michael@0: CheckTestFailureCount(1); michael@0: } michael@0: michael@0: void AssertNoFatalFailureIgnoresFailuresInOtherThreads() { michael@0: ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true)); michael@0: } michael@0: TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) { michael@0: // Using a subroutine, to make sure, that the test continues. michael@0: AssertNoFatalFailureIgnoresFailuresInOtherThreads(); michael@0: // We should only have one failure (the one from michael@0: // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE michael@0: // should succeed. michael@0: CheckTestFailureCount(1); michael@0: } michael@0: michael@0: TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) { michael@0: // This statement should fail, since the current thread doesn't generate a michael@0: // fatal failure, only another one does. michael@0: EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected"); michael@0: CheckTestFailureCount(2); michael@0: } michael@0: michael@0: TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) { michael@0: // This statement should succeed, because failures in all threads are michael@0: // considered. michael@0: EXPECT_FATAL_FAILURE_ON_ALL_THREADS( michael@0: GenerateFatalFailureInAnotherThread(true), "expected"); michael@0: CheckTestFailureCount(0); michael@0: // We need to add a failure, because main() checks that there are failures. michael@0: // But when only this test is run, we shouldn't have any failures. michael@0: ADD_FAILURE() << "This is an expected non-fatal failure."; michael@0: } michael@0: michael@0: TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) { michael@0: // This statement should fail, since the current thread doesn't generate a michael@0: // fatal failure, only another one does. michael@0: EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false), michael@0: "expected"); michael@0: CheckTestFailureCount(2); michael@0: } michael@0: michael@0: TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) { michael@0: // This statement should succeed, because failures in all threads are michael@0: // considered. michael@0: EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( michael@0: GenerateFatalFailureInAnotherThread(false), "expected"); michael@0: CheckTestFailureCount(0); michael@0: // We need to add a failure, because main() checks that there are failures, michael@0: // But when only this test is run, we shouldn't have any failures. michael@0: ADD_FAILURE() << "This is an expected non-fatal failure."; michael@0: } michael@0: michael@0: } // namespace michael@0: } // namespace testing michael@0: michael@0: int main(int argc, char **argv) { michael@0: testing::InitGoogleTest(&argc, argv); michael@0: michael@0: const int result = RUN_ALL_TESTS(); // Expected to fail. michael@0: GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected"; michael@0: michael@0: printf("\nPASS\n"); michael@0: return 0; michael@0: } michael@0: michael@0: #else michael@0: TEST(StressTest, michael@0: DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) { michael@0: } michael@0: michael@0: int main(int argc, char **argv) { michael@0: testing::InitGoogleTest(&argc, argv); michael@0: return RUN_ALL_TESTS(); michael@0: } michael@0: #endif // GTEST_IS_THREADSAFE