1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2007, Google Inc. 2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved. 3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without 5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are 6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met: 7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions of source code must retain the above copyright 9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer. 10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions in binary form must reproduce the above 11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer 12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the 13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution. 14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Neither the name of Google Inc. nor the names of its 15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from 16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission. 17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: wan@google.com (Zhanyong Wan) 31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests that SCOPED_TRACE() and various Google Test assertions can be 33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// used in a large number of threads concurrently. 34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/gtest.h> 36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 370ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson#include <iostream> 380ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson#include <vector> 390ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// We must define this macro in order to #include 41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// gtest-internal-inl.h. This is how Google Test prevents a user from 42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// accidentally depending on its internal implementation. 43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_IMPLEMENTATION_ 1 44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include "src/gtest-internal-inl.h" 45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#undef GTEST_IMPLEMENTATION_ 46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 470ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson#if GTEST_IS_THREADSAFE 480ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace testing { 50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace { 51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 520ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonusing internal::Notification; 53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleusing internal::String; 54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleusing internal::TestPropertyKeyIs; 550ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonusing internal::ThreadWithParam; 560ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonusing internal::scoped_ptr; 570ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 580ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson// In order to run tests in this file, for platforms where Google Test is 590ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson// thread safe, implement ThreadWithParam. See the description of its API 600ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson// in gtest-port.h, where it is defined for already supported platforms. 61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// How many threads to create? 63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst int kThreadCount = 50; 64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString IdToKey(int id, const char* suffix) { 66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Message key; 67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville key << "key_" << id << "_" << suffix; 68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return key.GetString(); 69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString IdToString(int id) { 72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Message id_message; 73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville id_message << id; 74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return id_message.GetString(); 75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 770ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonvoid ExpectKeyAndValueWereRecordedForId( 780ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const std::vector<TestProperty>& properties, 790ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson int id, const char* suffix) { 80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str()); 810ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const std::vector<TestProperty>::const_iterator property = 820ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson std::find_if(properties.begin(), properties.end(), matches_key); 830ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ASSERT_TRUE(property != properties.end()) 84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville << "expecting " << suffix << " value for id " << id; 85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville EXPECT_STREQ(IdToString(id).c_str(), property->value()); 86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Calls a large number of Google Test assertions, where exactly one of them 89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// will fail. 90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillevoid ManyAsserts(int id) { 910ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson GTEST_LOG_(INFO) << "Thread #" << id << " running..."; 92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville SCOPED_TRACE(Message() << "Thread #" << id); 94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville for (int i = 0; i < kThreadCount; i++) { 96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville SCOPED_TRACE(Message() << "Iteration #" << i); 97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // A bunch of assertions that should succeed. 99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville EXPECT_TRUE(true); 100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ASSERT_FALSE(false) << "This shouldn't fail."; 101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville EXPECT_STREQ("a", "a"); 102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ASSERT_LE(5, 6); 103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville EXPECT_EQ(i, i) << "This shouldn't fail."; 104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // RecordProperty() should interact safely with other threads as well. 106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // The shared_key forces property updates. 107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str()); 108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Test::RecordProperty(IdToKey(id, "int").c_str(), id); 109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Test::RecordProperty("shared_key", IdToString(id).c_str()); 110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // This assertion should fail kThreadCount times per thread. It 112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // is for testing whether Google Test can handle failed assertions in a 113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // multi-threaded context. 114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville EXPECT_LT(i, 0) << "This should always fail."; 115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 1180ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonvoid CheckTestFailureCount(int expected_failures) { 1190ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); 1200ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const TestResult* const result = info->result(); 1210ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson GTEST_CHECK_(expected_failures == result->total_part_count()) 1220ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson << "Logged " << result->total_part_count() << " failures " 1230ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson << " vs. " << expected_failures << " expected"; 1240ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson} 1250ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests using SCOPED_TRACE() and Google Test assertions in many threads 127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// concurrently. 128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) { 1290ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson { 1300ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson scoped_ptr<ThreadWithParam<int> > threads[kThreadCount]; 1310ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson Notification threads_can_start; 1320ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson for (int i = 0; i != kThreadCount; i++) 1330ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson threads[i].reset(new ThreadWithParam<int>(&ManyAsserts, 1340ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson i, 1350ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson &threads_can_start)); 1360ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1370ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson threads_can_start.Notify(); 1380ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1390ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // Blocks until all the threads are done. 1400ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson for (int i = 0; i != kThreadCount; i++) 1410ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson threads[i]->Join(); 1420ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson } 1430ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1440ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // Ensures that kThreadCount*kThreadCount failures have been reported. 1450ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); 1460ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const TestResult* const result = info->result(); 1470ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1480ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson std::vector<TestProperty> properties; 1490ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // We have no access to the TestResult's list of properties but we can 1500ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // copy them one by one. 1510ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson for (int i = 0; i < result->test_property_count(); ++i) 1520ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson properties.push_back(result->GetTestProperty(i)); 1530ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1540ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count()) 1550ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson << "String and int values recorded on each thread, " 1560ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson << "as well as one shared_key"; 1570ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson for (int i = 0; i < kThreadCount; ++i) { 1580ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ExpectKeyAndValueWereRecordedForId(properties, i, "string"); 1590ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ExpectKeyAndValueWereRecordedForId(properties, i, "int"); 1600ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson } 1610ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(kThreadCount*kThreadCount); 1620ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson} 1630ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1640ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonvoid FailingThread(bool is_fatal) { 1650ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson if (is_fatal) 1660ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson FAIL() << "Fatal failure in some other thread. " 1670ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson << "(This failure is expected.)"; 1680ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson else 1690ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ADD_FAILURE() << "Non-fatal failure in some other thread. " 1700ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson << "(This failure is expected.)"; 1710ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson} 1720ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 1730ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonvoid GenerateFatalFailureInAnotherThread(bool is_fatal) { 1740ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL); 1750ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson thread.Join(); 176fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 177fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 178fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) { 1790ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true)); 1800ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // We should only have one failure (the one from 1810ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE 1820ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // should succeed. 1830ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(1); 184fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 185fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 1860ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonvoid AssertNoFatalFailureIgnoresFailuresInOtherThreads() { 1870ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true)); 1880ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson} 189fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) { 1900ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // Using a subroutine, to make sure, that the test continues. 1910ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson AssertNoFatalFailureIgnoresFailuresInOtherThreads(); 1920ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // We should only have one failure (the one from 1930ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE 1940ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // should succeed. 1950ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(1); 196fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 197fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 198fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) { 1990ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // This statement should fail, since the current thread doesn't generate a 2000ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // fatal failure, only another one does. 2010ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected"); 2020ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(2); 203fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 204fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 205fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) { 2060ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // This statement should succeed, because failures in all threads are 2070ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // considered. 2080ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson EXPECT_FATAL_FAILURE_ON_ALL_THREADS( 2090ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson GenerateFatalFailureInAnotherThread(true), "expected"); 2100ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(0); 2110ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // We need to add a failure, because main() checks that there are failures. 2120ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // But when only this test is run, we shouldn't have any failures. 2130ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ADD_FAILURE() << "This is an expected non-fatal failure."; 214fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 215fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 216fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) { 2170ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // This statement should fail, since the current thread doesn't generate a 2180ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // fatal failure, only another one does. 2190ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false), 2200ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson "expected"); 2210ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(2); 222fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 223fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 224fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) { 2250ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // This statement should succeed, because failures in all threads are 2260ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // considered. 2270ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( 2280ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson GenerateFatalFailureInAnotherThread(false), "expected"); 2290ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson CheckTestFailureCount(0); 2300ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // We need to add a failure, because main() checks that there are failures, 2310ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson // But when only this test is run, we shouldn't have any failures. 2320ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson ADD_FAILURE() << "This is an expected non-fatal failure."; 233fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 234fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 235fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace 236fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace testing 237fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 238fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleint main(int argc, char **argv) { 239fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville testing::InitGoogleTest(&argc, argv); 240fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 2410ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson const int result = RUN_ALL_TESTS(); // Expected to fail. 2420ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected"; 2430ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 2440ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson printf("\nPASS\n"); 2450ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson return 0; 2460ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson} 2470ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 2480ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson#else 2490ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff DavidsonTEST(StressTest, 2500ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) { 2510ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson} 2520ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson 2530ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidsonint main(int argc, char **argv) { 2540ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson testing::InitGoogleTest(&argc, argv); 255fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return RUN_ALL_TESTS(); 256fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 2570ddac1f3791efefb2cffdb425f0c600feb7a47e6Jeff Davidson#endif // GTEST_IS_THREADSAFE 258