gtest_stress_test.cc (googletest-release-1.11.0) | : | gtest_stress_test.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 33 | skipping to change at line 33 | |||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// Tests that SCOPED_TRACE() and various Google Test assertions can be | // Tests that SCOPED_TRACE() and various Google Test assertions can be | |||
// used in a large number of threads concurrently. | // used in a large number of threads concurrently. | |||
#include "gtest/gtest.h" | ||||
#include <vector> | #include <vector> | |||
#include "gtest/gtest.h" | ||||
#include "src/gtest-internal-inl.h" | #include "src/gtest-internal-inl.h" | |||
#if GTEST_IS_THREADSAFE | #if GTEST_IS_THREADSAFE | |||
namespace testing { | namespace testing { | |||
namespace { | namespace { | |||
using internal::Notification; | using internal::Notification; | |||
using internal::TestPropertyKeyIs; | using internal::TestPropertyKeyIs; | |||
using internal::ThreadWithParam; | using internal::ThreadWithParam; | |||
skipping to change at line 68 | skipping to change at line 67 | |||
return key.GetString(); | return key.GetString(); | |||
} | } | |||
std::string IdToString(int id) { | std::string IdToString(int id) { | |||
Message id_message; | Message id_message; | |||
id_message << id; | id_message << id; | |||
return id_message.GetString(); | return id_message.GetString(); | |||
} | } | |||
void ExpectKeyAndValueWereRecordedForId( | void ExpectKeyAndValueWereRecordedForId( | |||
const std::vector<TestProperty>& properties, | const std::vector<TestProperty>& properties, int id, const char* suffix) { | |||
int id, const char* suffix) { | ||||
TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str()); | TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str()); | |||
const std::vector<TestProperty>::const_iterator property = | const std::vector<TestProperty>::const_iterator property = | |||
std::find_if(properties.begin(), properties.end(), matches_key); | std::find_if(properties.begin(), properties.end(), matches_key); | |||
ASSERT_TRUE(property != properties.end()) | ASSERT_TRUE(property != properties.end()) | |||
<< "expecting " << suffix << " value for id " << id; | << "expecting " << suffix << " value for id " << id; | |||
EXPECT_STREQ(IdToString(id).c_str(), property->value()); | EXPECT_STREQ(IdToString(id).c_str(), property->value()); | |||
} | } | |||
// Calls a large number of Google Test assertions, where exactly one of them | // Calls a large number of Google Test assertions, where exactly one of them | |||
// will fail. | // will fail. | |||
skipping to change at line 123 | skipping to change at line 121 | |||
<< " vs. " << expected_failures << " expected"; | << " vs. " << expected_failures << " expected"; | |||
} | } | |||
// Tests using SCOPED_TRACE() and Google Test assertions in many threads | // Tests using SCOPED_TRACE() and Google Test assertions in many threads | |||
// concurrently. | // concurrently. | |||
TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) { | TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) { | |||
{ | { | |||
std::unique_ptr<ThreadWithParam<int> > threads[kThreadCount]; | std::unique_ptr<ThreadWithParam<int> > threads[kThreadCount]; | |||
Notification threads_can_start; | Notification threads_can_start; | |||
for (int i = 0; i != kThreadCount; i++) | for (int i = 0; i != kThreadCount; i++) | |||
threads[i].reset(new ThreadWithParam<int>(&ManyAsserts, | threads[i].reset( | |||
i, | new ThreadWithParam<int>(&ManyAsserts, i, &threads_can_start)); | |||
&threads_can_start)); | ||||
threads_can_start.Notify(); | threads_can_start.Notify(); | |||
// Blocks until all the threads are done. | // Blocks until all the threads are done. | |||
for (int i = 0; i != kThreadCount; i++) | for (int i = 0; i != kThreadCount; i++) threads[i]->Join(); | |||
threads[i]->Join(); | ||||
} | } | |||
// Ensures that kThreadCount*kThreadCount failures have been reported. | // Ensures that kThreadCount*kThreadCount failures have been reported. | |||
const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); | const TestInfo* const info = UnitTest::GetInstance()->current_test_info(); | |||
const TestResult* const result = info->result(); | const TestResult* const result = info->result(); | |||
std::vector<TestProperty> properties; | std::vector<TestProperty> properties; | |||
// We have no access to the TestResult's list of properties but we can | // We have no access to the TestResult's list of properties but we can | |||
// copy them one by one. | // copy them one by one. | |||
for (int i = 0; i < result->test_property_count(); ++i) | for (int i = 0; i < result->test_property_count(); ++i) | |||
properties.push_back(result->GetTestProperty(i)); | properties.push_back(result->GetTestProperty(i)); | |||
EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count()) | EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count()) | |||
<< "String and int values recorded on each thread, " | << "String and int values recorded on each thread, " | |||
<< "as well as one shared_key"; | << "as well as one shared_key"; | |||
for (int i = 0; i < kThreadCount; ++i) { | for (int i = 0; i < kThreadCount; ++i) { | |||
ExpectKeyAndValueWereRecordedForId(properties, i, "string"); | ExpectKeyAndValueWereRecordedForId(properties, i, "string"); | |||
ExpectKeyAndValueWereRecordedForId(properties, i, "int"); | ExpectKeyAndValueWereRecordedForId(properties, i, "int"); | |||
} | } | |||
CheckTestFailureCount(kThreadCount*kThreadCount); | CheckTestFailureCount(kThreadCount * kThreadCount); | |||
} | } | |||
void FailingThread(bool is_fatal) { | void FailingThread(bool is_fatal) { | |||
if (is_fatal) | if (is_fatal) | |||
FAIL() << "Fatal failure in some other thread. " | FAIL() << "Fatal failure in some other thread. " | |||
<< "(This failure is expected.)"; | << "(This failure is expected.)"; | |||
else | else | |||
ADD_FAILURE() << "Non-fatal failure in some other thread. " | ADD_FAILURE() << "Non-fatal failure in some other thread. " | |||
<< "(This failure is expected.)"; | << "(This failure is expected.)"; | |||
} | } | |||
skipping to change at line 198 | skipping to change at line 194 | |||
TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) { | TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) { | |||
// This statement should fail, since the current thread doesn't generate a | // This statement should fail, since the current thread doesn't generate a | |||
// fatal failure, only another one does. | // fatal failure, only another one does. | |||
EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected"); | EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected"); | |||
CheckTestFailureCount(2); | CheckTestFailureCount(2); | |||
} | } | |||
TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) { | TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) { | |||
// This statement should succeed, because failures in all threads are | // This statement should succeed, because failures in all threads are | |||
// considered. | // considered. | |||
EXPECT_FATAL_FAILURE_ON_ALL_THREADS( | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(GenerateFatalFailureInAnotherThread(true), | |||
GenerateFatalFailureInAnotherThread(true), "expected"); | "expected"); | |||
CheckTestFailureCount(0); | CheckTestFailureCount(0); | |||
// We need to add a failure, because main() checks that there are failures. | // We need to add a failure, because main() checks that there are failures. | |||
// But when only this test is run, we shouldn't have any failures. | // But when only this test is run, we shouldn't have any failures. | |||
ADD_FAILURE() << "This is an expected non-fatal failure."; | ADD_FAILURE() << "This is an expected non-fatal failure."; | |||
} | } | |||
TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) { | TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) { | |||
// This statement should fail, since the current thread doesn't generate a | // This statement should fail, since the current thread doesn't generate a | |||
// fatal failure, only another one does. | // fatal failure, only another one does. | |||
EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false), | EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false), | |||
skipping to change at line 228 | skipping to change at line 224 | |||
GenerateFatalFailureInAnotherThread(false), "expected"); | GenerateFatalFailureInAnotherThread(false), "expected"); | |||
CheckTestFailureCount(0); | CheckTestFailureCount(0); | |||
// We need to add a failure, because main() checks that there are failures, | // We need to add a failure, because main() checks that there are failures, | |||
// But when only this test is run, we shouldn't have any failures. | // But when only this test is run, we shouldn't have any failures. | |||
ADD_FAILURE() << "This is an expected non-fatal failure."; | ADD_FAILURE() << "This is an expected non-fatal failure."; | |||
} | } | |||
} // namespace | } // namespace | |||
} // namespace testing | } // namespace testing | |||
int main(int argc, char **argv) { | int main(int argc, char** argv) { | |||
testing::InitGoogleTest(&argc, argv); | testing::InitGoogleTest(&argc, argv); | |||
const int result = RUN_ALL_TESTS(); // Expected to fail. | const int result = RUN_ALL_TESTS(); // Expected to fail. | |||
GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected"; | GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected"; | |||
printf("\nPASS\n"); | printf("\nPASS\n"); | |||
return 0; | return 0; | |||
} | } | |||
#else | #else | |||
TEST(StressTest, | TEST(StressTest, | |||
DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) { | DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {} | |||
} | ||||
int main(int argc, char **argv) { | int main(int argc, char **argv) { | |||
testing::InitGoogleTest(&argc, argv); | testing::InitGoogleTest(&argc, argv); | |||
return RUN_ALL_TESTS(); | return RUN_ALL_TESTS(); | |||
} | } | |||
#endif // GTEST_IS_THREADSAFE | #endif // GTEST_IS_THREADSAFE | |||
End of changes. 9 change blocks. | ||||
15 lines changed or deleted | 10 lines changed or added |