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
45 namespace testing {
46 namespace {
47
48 using internal::List;
49 using internal::ListNode;
50 using internal::String;
51 using internal::TestProperty;
52 using internal::TestPropertyKeyIs;
53
54 // How many threads to create?
55 const int kThreadCount = 50;
56
IdToKey(int id,const char * suffix)57 String IdToKey(int id, const char* suffix) {
58 Message key;
59 key << "key_" << id << "_" << suffix;
60 return key.GetString();
61 }
62
IdToString(int id)63 String IdToString(int id) {
64 Message id_message;
65 id_message << id;
66 return id_message.GetString();
67 }
68
ExpectKeyAndValueWereRecordedForId(const List<TestProperty> & properties,int id,const char * suffix)69 void ExpectKeyAndValueWereRecordedForId(const List<TestProperty>& properties,
70 int id,
71 const char* suffix) {
72 TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
73 const ListNode<TestProperty>* node = properties.FindIf(matches_key);
74 EXPECT_TRUE(node != NULL) << "expecting " << suffix << " node for id " << id;
75 EXPECT_STREQ(IdToString(id).c_str(), node->element().value());
76 }
77
78 // Calls a large number of Google Test assertions, where exactly one of them
79 // will fail.
ManyAsserts(int id)80 void 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.
TEST(StressTest,CanUseScopedTraceAndAssertionsInManyThreads)110 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
111 // TODO(wan): when Google Test is made thread-safe, run
112 // ManyAsserts() in many threads here.
113 }
114
TEST(NoFatalFailureTest,ExpectNoFatalFailureIgnoresFailuresInOtherThreads)115 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
116 // TODO(mheule@google.com): Test this works correctly when Google
117 // Test is made thread-safe.
118 }
119
TEST(NoFatalFailureTest,AssertNoFatalFailureIgnoresFailuresInOtherThreads)120 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
121 // TODO(mheule@google.com): Test this works correctly when Google
122 // Test is made thread-safe.
123 }
124
TEST(FatalFailureTest,ExpectFatalFailureIgnoresFailuresInOtherThreads)125 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
126 // TODO(mheule@google.com): Test this works correctly when Google
127 // Test is made thread-safe.
128 }
129
TEST(FatalFailureOnAllThreadsTest,ExpectFatalFailureOnAllThreads)130 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
131 // TODO(wan@google.com): Test this works correctly when Google Test
132 // is made thread-safe.
133 }
134
TEST(NonFatalFailureTest,ExpectNonFatalFailureIgnoresFailuresInOtherThreads)135 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
136 // TODO(mheule@google.com): Test this works correctly when Google
137 // Test is made thread-safe.
138 }
139
TEST(NonFatalFailureOnAllThreadsTest,ExpectNonFatalFailureOnAllThreads)140 TEST(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
main(int argc,char ** argv)148 int main(int argc, char **argv) {
149 testing::InitGoogleTest(&argc, argv);
150
151 return RUN_ALL_TESTS();
152 }
153