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