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

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial