gtest_stress_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Tests that SCOPED_TRACE() and various Google Test assertions can be
00033 // used in a large number of threads concurrently.
00034 
00035 #include "gtest/gtest.h"
00036 
00037 #include <iostream>
00038 #include <vector>
00039 
00040 // We must define this macro in order to #include
00041 // gtest-internal-inl.h.  This is how Google Test prevents a user from
00042 // accidentally depending on its internal implementation.
00043 #define GTEST_IMPLEMENTATION_ 1
00044 #include "src/gtest-internal-inl.h"
00045 #undef GTEST_IMPLEMENTATION_
00046 
00047 #if GTEST_IS_THREADSAFE
00048 
00049 namespace testing {
00050 namespace {
00051 
00052 using internal::Notification;
00053 using internal::TestPropertyKeyIs;
00054 using internal::ThreadWithParam;
00055 using internal::scoped_ptr;
00056 
00057 // In order to run tests in this file, for platforms where Google Test is
00058 // thread safe, implement ThreadWithParam. See the description of its API
00059 // in gtest-port.h, where it is defined for already supported platforms.
00060 
00061 // How many threads to create?
00062 const int kThreadCount = 50;
00063 
00064 std::string IdToKey(int id, const char* suffix) {
00065   Message key;
00066   key << "key_" << id << "_" << suffix;
00067   return key.GetString();
00068 }
00069 
00070 std::string IdToString(int id) {
00071   Message id_message;
00072   id_message << id;
00073   return id_message.GetString();
00074 }
00075 
00076 void ExpectKeyAndValueWereRecordedForId(
00077     const std::vector<TestProperty>& properties,
00078     int id, const char* suffix) {
00079   TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
00080   const std::vector<TestProperty>::const_iterator property =
00081       std::find_if(properties.begin(), properties.end(), matches_key);
00082   ASSERT_TRUE(property != properties.end())
00083       << "expecting " << suffix << " value for id " << id;
00084   EXPECT_STREQ(IdToString(id).c_str(), property->value());
00085 }
00086 
00087 // Calls a large number of Google Test assertions, where exactly one of them
00088 // will fail.
00089 void ManyAsserts(int id) {
00090   GTEST_LOG_(INFO) << "Thread #" << id << " running...";
00091 
00092   SCOPED_TRACE(Message() << "Thread #" << id);
00093 
00094   for (int i = 0; i < kThreadCount; i++) {
00095     SCOPED_TRACE(Message() << "Iteration #" << i);
00096 
00097     // A bunch of assertions that should succeed.
00098     EXPECT_TRUE(true);
00099     ASSERT_FALSE(false) << "This shouldn't fail.";
00100     EXPECT_STREQ("a", "a");
00101     ASSERT_LE(5, 6);
00102     EXPECT_EQ(i, i) << "This shouldn't fail.";
00103 
00104     // RecordProperty() should interact safely with other threads as well.
00105     // The shared_key forces property updates.
00106     Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
00107     Test::RecordProperty(IdToKey(id, "int").c_str(), id);
00108     Test::RecordProperty("shared_key", IdToString(id).c_str());
00109 
00110     // This assertion should fail kThreadCount times per thread.  It
00111     // is for testing whether Google Test can handle failed assertions in a
00112     // multi-threaded context.
00113     EXPECT_LT(i, 0) << "This should always fail.";
00114   }
00115 }
00116 
00117 void CheckTestFailureCount(int expected_failures) {
00118   const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
00119   const TestResult* const result = info->result();
00120   GTEST_CHECK_(expected_failures == result->total_part_count())
00121       << "Logged " << result->total_part_count() << " failures "
00122       << " vs. " << expected_failures << " expected";
00123 }
00124 
00125 // Tests using SCOPED_TRACE() and Google Test assertions in many threads
00126 // concurrently.
00127 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
00128   {
00129     scoped_ptr<ThreadWithParam<int> > threads[kThreadCount];
00130     Notification threads_can_start;
00131     for (int i = 0; i != kThreadCount; i++)
00132       threads[i].reset(new ThreadWithParam<int>(&ManyAsserts,
00133                                                 i,
00134                                                 &threads_can_start));
00135 
00136     threads_can_start.Notify();
00137 
00138     // Blocks until all the threads are done.
00139     for (int i = 0; i != kThreadCount; i++)
00140       threads[i]->Join();
00141   }
00142 
00143   // Ensures that kThreadCount*kThreadCount failures have been reported.
00144   const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
00145   const TestResult* const result = info->result();
00146 
00147   std::vector<TestProperty> properties;
00148   // We have no access to the TestResult's list of properties but we can
00149   // copy them one by one.
00150   for (int i = 0; i < result->test_property_count(); ++i)
00151     properties.push_back(result->GetTestProperty(i));
00152 
00153   EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
00154       << "String and int values recorded on each thread, "
00155       << "as well as one shared_key";
00156   for (int i = 0; i < kThreadCount; ++i) {
00157     ExpectKeyAndValueWereRecordedForId(properties, i, "string");
00158     ExpectKeyAndValueWereRecordedForId(properties, i, "int");
00159   }
00160   CheckTestFailureCount(kThreadCount*kThreadCount);
00161 }
00162 
00163 void FailingThread(bool is_fatal) {
00164   if (is_fatal)
00165     FAIL() << "Fatal failure in some other thread. "
00166            << "(This failure is expected.)";
00167   else
00168     ADD_FAILURE() << "Non-fatal failure in some other thread. "
00169                   << "(This failure is expected.)";
00170 }
00171 
00172 void GenerateFatalFailureInAnotherThread(bool is_fatal) {
00173   ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL);
00174   thread.Join();
00175 }
00176 
00177 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
00178   EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
00179   // We should only have one failure (the one from
00180   // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
00181   // should succeed.
00182   CheckTestFailureCount(1);
00183 }
00184 
00185 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
00186   ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
00187 }
00188 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
00189   // Using a subroutine, to make sure, that the test continues.
00190   AssertNoFatalFailureIgnoresFailuresInOtherThreads();
00191   // We should only have one failure (the one from
00192   // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
00193   // should succeed.
00194   CheckTestFailureCount(1);
00195 }
00196 
00197 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
00198   // This statement should fail, since the current thread doesn't generate a
00199   // fatal failure, only another one does.
00200   EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected");
00201   CheckTestFailureCount(2);
00202 }
00203 
00204 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
00205   // This statement should succeed, because failures in all threads are
00206   // considered.
00207   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(
00208       GenerateFatalFailureInAnotherThread(true), "expected");
00209   CheckTestFailureCount(0);
00210   // We need to add a failure, because main() checks that there are failures.
00211   // But when only this test is run, we shouldn't have any failures.
00212   ADD_FAILURE() << "This is an expected non-fatal failure.";
00213 }
00214 
00215 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
00216   // This statement should fail, since the current thread doesn't generate a
00217   // fatal failure, only another one does.
00218   EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false),
00219                           "expected");
00220   CheckTestFailureCount(2);
00221 }
00222 
00223 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
00224   // This statement should succeed, because failures in all threads are
00225   // considered.
00226   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(
00227       GenerateFatalFailureInAnotherThread(false), "expected");
00228   CheckTestFailureCount(0);
00229   // We need to add a failure, because main() checks that there are failures,
00230   // But when only this test is run, we shouldn't have any failures.
00231   ADD_FAILURE() << "This is an expected non-fatal failure.";
00232 }
00233 
00234 }  // namespace
00235 }  // namespace testing
00236 
00237 int main(int argc, char **argv) {
00238   testing::InitGoogleTest(&argc, argv);
00239 
00240   const int result = RUN_ALL_TESTS();  // Expected to fail.
00241   GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected";
00242 
00243   printf("\nPASS\n");
00244   return 0;
00245 }
00246 
00247 #else
00248 TEST(StressTest,
00249      DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
00250 }
00251 
00252 int main(int argc, char **argv) {
00253   testing::InitGoogleTest(&argc, argv);
00254   return RUN_ALL_TESTS();
00255 }
00256 #endif  // GTEST_IS_THREADSAFE


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04