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