Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest_stress_test.cc
Go to the documentation of this file.
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 
31 // Tests that SCOPED_TRACE() and various Google Test assertions can be
32 // used in a large number of threads concurrently.
33 
34 #include "gtest/gtest.h"
35 
36 #include <vector>
37 
38 #include "src/gtest-internal-inl.h"
39 
40 #if GTEST_IS_THREADSAFE
41 
42 namespace testing {
43 namespace {
44 
45 using internal::Notification;
46 using internal::TestPropertyKeyIs;
47 using internal::ThreadWithParam;
48 
49 // In order to run tests in this file, for platforms where Google Test is
50 // thread safe, implement ThreadWithParam. See the description of its API
51 // in gtest-port.h, where it is defined for already supported platforms.
52 
53 // How many threads to create?
54 const int kThreadCount = 50;
55 
56 std::string IdToKey(int id, const char* suffix) {
57  Message key;
58  key << "key_" << id << "_" << suffix;
59  return key.GetString();
60 }
61 
62 std::string IdToString(int id) {
63  Message id_message;
64  id_message << id;
65  return id_message.GetString();
66 }
67 
68 void ExpectKeyAndValueWereRecordedForId(
69  const std::vector<TestProperty>& properties,
70  int id, const char* suffix) {
71  TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
72  const std::vector<TestProperty>::const_iterator property =
73  std::find_if(properties.begin(), properties.end(), matches_key);
74  ASSERT_TRUE(property != properties.end())
75  << "expecting " << suffix << " value for id " << id;
76  EXPECT_STREQ(IdToString(id).c_str(), property->value());
77 }
78 
79 // Calls a large number of Google Test assertions, where exactly one of them
80 // will fail.
81 void ManyAsserts(int id) {
82  GTEST_LOG_(INFO) << "Thread #" << id << " running...";
83 
84  SCOPED_TRACE(Message() << "Thread #" << id);
85 
86  for (int i = 0; i < kThreadCount; i++) {
87  SCOPED_TRACE(Message() << "Iteration #" << i);
88 
89  // A bunch of assertions that should succeed.
90  EXPECT_TRUE(true);
91  ASSERT_FALSE(false) << "This shouldn't fail.";
92  EXPECT_STREQ("a", "a");
93  ASSERT_LE(5, 6);
94  EXPECT_EQ(i, i) << "This shouldn't fail.";
95 
96  // RecordProperty() should interact safely with other threads as well.
97  // The shared_key forces property updates.
98  Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
99  Test::RecordProperty(IdToKey(id, "int").c_str(), id);
100  Test::RecordProperty("shared_key", IdToString(id).c_str());
101 
102  // This assertion should fail kThreadCount times per thread. It
103  // is for testing whether Google Test can handle failed assertions in a
104  // multi-threaded context.
105  EXPECT_LT(i, 0) << "This should always fail.";
106  }
107 }
108 
109 void CheckTestFailureCount(int expected_failures) {
110  const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
111  const TestResult* const result = info->result();
112  GTEST_CHECK_(expected_failures == result->total_part_count())
113  << "Logged " << result->total_part_count() << " failures "
114  << " vs. " << expected_failures << " expected";
115 }
116 
117 // Tests using SCOPED_TRACE() and Google Test assertions in many threads
118 // concurrently.
119 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
120  {
121  std::unique_ptr<ThreadWithParam<int> > threads[kThreadCount];
122  Notification threads_can_start;
123  for (int i = 0; i != kThreadCount; i++)
124  threads[i].reset(new ThreadWithParam<int>(&ManyAsserts,
125  i,
126  &threads_can_start));
127 
128  threads_can_start.Notify();
129 
130  // Blocks until all the threads are done.
131  for (int i = 0; i != kThreadCount; i++)
132  threads[i]->Join();
133  }
134 
135  // Ensures that kThreadCount*kThreadCount failures have been reported.
136  const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
137  const TestResult* const result = info->result();
138 
139  std::vector<TestProperty> properties;
140  // We have no access to the TestResult's list of properties but we can
141  // copy them one by one.
142  for (int i = 0; i < result->test_property_count(); ++i)
143  properties.push_back(result->GetTestProperty(i));
144 
145  EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
146  << "String and int values recorded on each thread, "
147  << "as well as one shared_key";
148  for (int i = 0; i < kThreadCount; ++i) {
149  ExpectKeyAndValueWereRecordedForId(properties, i, "string");
150  ExpectKeyAndValueWereRecordedForId(properties, i, "int");
151  }
152  CheckTestFailureCount(kThreadCount*kThreadCount);
153 }
154 
155 void FailingThread(bool is_fatal) {
156  if (is_fatal)
157  FAIL() << "Fatal failure in some other thread. "
158  << "(This failure is expected.)";
159  else
160  ADD_FAILURE() << "Non-fatal failure in some other thread. "
161  << "(This failure is expected.)";
162 }
163 
164 void GenerateFatalFailureInAnotherThread(bool is_fatal) {
165  ThreadWithParam<bool> thread(&FailingThread, is_fatal, nullptr);
166  thread.Join();
167 }
168 
169 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
170  EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
171  // We should only have one failure (the one from
172  // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
173  // should succeed.
174  CheckTestFailureCount(1);
175 }
176 
177 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
178  ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
179 }
180 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
181  // Using a subroutine, to make sure, that the test continues.
182  AssertNoFatalFailureIgnoresFailuresInOtherThreads();
183  // We should only have one failure (the one from
184  // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
185  // should succeed.
186  CheckTestFailureCount(1);
187 }
188 
189 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
190  // This statement should fail, since the current thread doesn't generate a
191  // fatal failure, only another one does.
192  EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected");
193  CheckTestFailureCount(2);
194 }
195 
196 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
197  // This statement should succeed, because failures in all threads are
198  // considered.
200  GenerateFatalFailureInAnotherThread(true), "expected");
201  CheckTestFailureCount(0);
202  // We need to add a failure, because main() checks that there are failures.
203  // But when only this test is run, we shouldn't have any failures.
204  ADD_FAILURE() << "This is an expected non-fatal failure.";
205 }
206 
207 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
208  // This statement should fail, since the current thread doesn't generate a
209  // fatal failure, only another one does.
210  EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false),
211  "expected");
212  CheckTestFailureCount(2);
213 }
214 
215 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
216  // This statement should succeed, because failures in all threads are
217  // considered.
219  GenerateFatalFailureInAnotherThread(false), "expected");
220  CheckTestFailureCount(0);
221  // We need to add a failure, because main() checks that there are failures,
222  // But when only this test is run, we shouldn't have any failures.
223  ADD_FAILURE() << "This is an expected non-fatal failure.";
224 }
225 
226 } // namespace
227 } // namespace testing
228 
229 int main(int argc, char **argv) {
230  testing::InitGoogleTest(&argc, argv);
231 
232  const int result = RUN_ALL_TESTS(); // Expected to fail.
233  GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected";
234 
235  printf("\nPASS\n");
236  return 0;
237 }
238 
239 #else
240 TEST(StressTest,
241  DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
242 }
243 
244 int main(int argc, char **argv) {
245  testing::InitGoogleTest(&argc, argv);
246  return RUN_ALL_TESTS();
247 }
248 #endif // GTEST_IS_THREADSAFE
#define ASSERT_LE(val1, val2)
Definition: gtest.h:2076
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define GTEST_LOG_(severity)
Definition: gtest-port.h:980
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2348
#define EXPECT_LT(val1, val2)
Definition: gtest.h:2044
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2213
#define ASSERT_TRUE(condition)
Definition: gtest.h:1985
#define GTEST_CHECK_(condition)
Definition: gtest-port.h:1004
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2211
int main()
Definition: ad_example.cpp:171
TEST(GTestEnvVarTest, Dummy)
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2107
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
Definition: gtest.cc:5344
#define SCOPED_TRACE(message)
Definition: gtest.h:2276
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2038
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2484
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6527
const TestResult * result() const
Definition: gtest.h:769
#define EXPECT_TRUE(condition)
Definition: gtest.h:1979
static UnitTest * GetInstance()
Definition: gtest.cc:4998
#define ADD_FAILURE()
Definition: gtest.h:1923
static void RecordProperty(const std::string &key, const std::string &value)
Definition: gtest.cc:2449
#define FAIL()
Definition: gtest.h:1942
#define ASSERT_FALSE(condition)
Definition: gtest.h:1988