Home | History | Annotate | Download | only in test
      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 (at) google.com (Zhanyong Wan)
     31 
     32 // Tests that SCOPED_TRACE() and various Google Test assertions can be
     33 // used in a large number of threads concurrently.
     34 
     35 #include <iostream>
     36 #include <gtest/gtest.h>
     37 
     38 // We must define this macro in order to #include
     39 // gtest-internal-inl.h.  This is how Google Test prevents a user from
     40 // accidentally depending on its internal implementation.
     41 #define GTEST_IMPLEMENTATION_ 1
     42 #include "src/gtest-internal-inl.h"
     43 #undef GTEST_IMPLEMENTATION_
     44 
     45 namespace testing {
     46 namespace {
     47 
     48 using internal::String;
     49 using internal::TestPropertyKeyIs;
     50 using internal::Vector;
     51 
     52 // How many threads to create?
     53 const int kThreadCount = 50;
     54 
     55 String IdToKey(int id, const char* suffix) {
     56   Message key;
     57   key << "key_" << id << "_" << suffix;
     58   return key.GetString();
     59 }
     60 
     61 String IdToString(int id) {
     62   Message id_message;
     63   id_message << id;
     64   return id_message.GetString();
     65 }
     66 
     67 void ExpectKeyAndValueWereRecordedForId(const Vector<TestProperty>& properties,
     68                                         int id,
     69                                         const char* suffix) {
     70   TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
     71   const TestProperty* property = properties.FindIf(matches_key);
     72   ASSERT_TRUE(property != NULL)
     73       << "expecting " << suffix << " value for id " << id;
     74   EXPECT_STREQ(IdToString(id).c_str(), property->value());
     75 }
     76 
     77 // Calls a large number of Google Test assertions, where exactly one of them
     78 // will fail.
     79 void ManyAsserts(int id) {
     80   ::std::cout << "Thread #" << id << " running...\n";
     81 
     82   SCOPED_TRACE(Message() << "Thread #" << id);
     83 
     84   for (int i = 0; i < kThreadCount; i++) {
     85     SCOPED_TRACE(Message() << "Iteration #" << i);
     86 
     87     // A bunch of assertions that should succeed.
     88     EXPECT_TRUE(true);
     89     ASSERT_FALSE(false) << "This shouldn't fail.";
     90     EXPECT_STREQ("a", "a");
     91     ASSERT_LE(5, 6);
     92     EXPECT_EQ(i, i) << "This shouldn't fail.";
     93 
     94     // RecordProperty() should interact safely with other threads as well.
     95     // The shared_key forces property updates.
     96     Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
     97     Test::RecordProperty(IdToKey(id, "int").c_str(), id);
     98     Test::RecordProperty("shared_key", IdToString(id).c_str());
     99 
    100     // This assertion should fail kThreadCount times per thread.  It
    101     // is for testing whether Google Test can handle failed assertions in a
    102     // multi-threaded context.
    103     EXPECT_LT(i, 0) << "This should always fail.";
    104   }
    105 }
    106 
    107 // Tests using SCOPED_TRACE() and Google Test assertions in many threads
    108 // concurrently.
    109 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
    110   // TODO(wan): when Google Test is made thread-safe, run
    111   // ManyAsserts() in many threads here.
    112 }
    113 
    114 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
    115   // TODO(mheule (at) google.com): Test this works correctly when Google
    116   // Test is made thread-safe.
    117 }
    118 
    119 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
    120   // TODO(mheule (at) google.com): Test this works correctly when Google
    121   // Test is made thread-safe.
    122 }
    123 
    124 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
    125   // TODO(mheule (at) google.com): Test this works correctly when Google
    126   // Test is made thread-safe.
    127 }
    128 
    129 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
    130   // TODO(wan (at) google.com): Test this works correctly when Google Test
    131   // is made thread-safe.
    132 }
    133 
    134 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
    135   // TODO(mheule (at) google.com): Test this works correctly when Google
    136   // Test is made thread-safe.
    137 }
    138 
    139 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
    140   // TODO(wan (at) google.com): Test this works correctly when Google Test
    141   // is made thread-safe.
    142 }
    143 
    144 }  // namespace
    145 }  // namespace testing
    146 
    147 int main(int argc, char **argv) {
    148   testing::InitGoogleTest(&argc, argv);
    149 
    150   return RUN_ALL_TESTS();
    151 }
    152