media/webrtc/trunk/testing/gtest/test/gtest_stress_test.cc

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial