1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Tests that SCOPED_TRACE() and various Google Test assertions can be
32 // used in a large number of threads concurrently.
34 #include "gtest/gtest.h"
38 #include "src/gtest-internal-inl.h"
40 #if GTEST_IS_THREADSAFE
45 using internal::Notification;
46 using internal::TestPropertyKeyIs;
47 using internal::ThreadWithParam;
48 using internal::scoped_ptr;
50 // In order to run tests in this file, for platforms where Google Test is
51 // thread safe, implement ThreadWithParam. See the description of its API
52 // in gtest-port.h, where it is defined for already supported platforms.
54 // How many threads to create?
55 const int kThreadCount = 50;
57 std::string IdToKey(int id, const char* suffix) {
59 key << "key_" << id << "_" << suffix;
60 return key.GetString();
63 std::string IdToString(int id) {
66 return id_message.GetString();
69 void ExpectKeyAndValueWereRecordedForId(
70 const std::vector<TestProperty>& properties,
71 int id, const char* suffix) {
72 TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
73 const std::vector<TestProperty>::const_iterator property =
74 std::find_if(properties.begin(), properties.end(), matches_key);
75 ASSERT_TRUE(property != properties.end())
76 << "expecting " << suffix << " value for id " << id;
77 EXPECT_STREQ(IdToString(id).c_str(), property->value());
80 // Calls a large number of Google Test assertions, where exactly one of them
82 void ManyAsserts(int id) {
83 GTEST_LOG_(INFO) << "Thread #" << id << " running...";
85 SCOPED_TRACE(Message() << "Thread #" << id);
87 for (int i = 0; i < kThreadCount; i++) {
88 SCOPED_TRACE(Message() << "Iteration #" << i);
90 // A bunch of assertions that should succeed.
92 ASSERT_FALSE(false) << "This shouldn't fail.";
93 EXPECT_STREQ("a", "a");
95 EXPECT_EQ(i, i) << "This shouldn't fail.";
97 // RecordProperty() should interact safely with other threads as well.
98 // The shared_key forces property updates.
99 Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
100 Test::RecordProperty(IdToKey(id, "int").c_str(), id);
101 Test::RecordProperty("shared_key", IdToString(id).c_str());
103 // This assertion should fail kThreadCount times per thread. It
104 // is for testing whether Google Test can handle failed assertions in a
105 // multi-threaded context.
106 EXPECT_LT(i, 0) << "This should always fail.";
110 void CheckTestFailureCount(int expected_failures) {
111 const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
112 const TestResult* const result = info->result();
113 GTEST_CHECK_(expected_failures == result->total_part_count())
114 << "Logged " << result->total_part_count() << " failures "
115 << " vs. " << expected_failures << " expected";
118 // Tests using SCOPED_TRACE() and Google Test assertions in many threads
120 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
122 scoped_ptr<ThreadWithParam<int> > threads[kThreadCount];
123 Notification threads_can_start;
124 for (int i = 0; i != kThreadCount; i++)
125 threads[i].reset(new ThreadWithParam<int>(&ManyAsserts,
127 &threads_can_start));
129 threads_can_start.Notify();
131 // Blocks until all the threads are done.
132 for (int i = 0; i != kThreadCount; i++)
136 // Ensures that kThreadCount*kThreadCount failures have been reported.
137 const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
138 const TestResult* const result = info->result();
140 std::vector<TestProperty> properties;
141 // We have no access to the TestResult's list of properties but we can
142 // copy them one by one.
143 for (int i = 0; i < result->test_property_count(); ++i)
144 properties.push_back(result->GetTestProperty(i));
146 EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
147 << "String and int values recorded on each thread, "
148 << "as well as one shared_key";
149 for (int i = 0; i < kThreadCount; ++i) {
150 ExpectKeyAndValueWereRecordedForId(properties, i, "string");
151 ExpectKeyAndValueWereRecordedForId(properties, i, "int");
153 CheckTestFailureCount(kThreadCount*kThreadCount);
156 void FailingThread(bool is_fatal) {
158 FAIL() << "Fatal failure in some other thread. "
159 << "(This failure is expected.)";
161 ADD_FAILURE() << "Non-fatal failure in some other thread. "
162 << "(This failure is expected.)";
165 void GenerateFatalFailureInAnotherThread(bool is_fatal) {
166 ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL);
170 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
171 EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
172 // We should only have one failure (the one from
173 // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
175 CheckTestFailureCount(1);
178 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
179 ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
181 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
182 // Using a subroutine, to make sure, that the test continues.
183 AssertNoFatalFailureIgnoresFailuresInOtherThreads();
184 // We should only have one failure (the one from
185 // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
187 CheckTestFailureCount(1);
190 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
191 // This statement should fail, since the current thread doesn't generate a
192 // fatal failure, only another one does.
193 EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected");
194 CheckTestFailureCount(2);
197 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
198 // This statement should succeed, because failures in all threads are
200 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(
201 GenerateFatalFailureInAnotherThread(true), "expected");
202 CheckTestFailureCount(0);
203 // We need to add a failure, because main() checks that there are failures.
204 // But when only this test is run, we shouldn't have any failures.
205 ADD_FAILURE() << "This is an expected non-fatal failure.";
208 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
209 // This statement should fail, since the current thread doesn't generate a
210 // fatal failure, only another one does.
211 EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false),
213 CheckTestFailureCount(2);
216 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
217 // This statement should succeed, because failures in all threads are
219 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(
220 GenerateFatalFailureInAnotherThread(false), "expected");
221 CheckTestFailureCount(0);
222 // We need to add a failure, because main() checks that there are failures,
223 // But when only this test is run, we shouldn't have any failures.
224 ADD_FAILURE() << "This is an expected non-fatal failure.";
228 } // namespace testing
230 int main(int argc, char **argv) {
231 testing::InitGoogleTest(&argc, argv);
233 const int result = RUN_ALL_TESTS(); // Expected to fail.
234 GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected";
242 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
245 int main(int argc, char **argv) {
246 testing::InitGoogleTest(&argc, argv);
247 return RUN_ALL_TESTS();
249 #endif // GTEST_IS_THREADSAFE