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;
54 const int kThreadCount = 50;
59 return key.GetString();
65 return id_message.GetString();
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);
75 <<
"expecting " <<
suffix <<
" value for id " <<
id;
81 void ManyAsserts(
int id) {
86 for (
int i = 0;
i < kThreadCount;
i++) {
94 EXPECT_EQ(i, i) <<
"This shouldn't fail.";
105 EXPECT_LT(i, 0) <<
"This should always fail.";
109 void CheckTestFailureCount(
int expected_failures) {
111 const TestResult*
const result = info->result();
113 <<
"Logged " <<
result->total_part_count() <<
" failures "
114 <<
" vs. " << expected_failures <<
" expected";
119 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
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,
126 &threads_can_start));
128 threads_can_start.Notify();
131 for (
int i = 0;
i != kThreadCount;
i++)
137 const TestResult*
const result = info->result();
139 std::vector<TestProperty> properties;
142 for (
int i = 0;
i <
result->test_property_count(); ++
i)
143 properties.push_back(
result->GetTestProperty(i));
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");
152 CheckTestFailureCount(kThreadCount*kThreadCount);
155 void FailingThread(
bool is_fatal) {
157 FAIL() <<
"Fatal failure in some other thread. "
158 <<
"(This failure is expected.)";
160 ADD_FAILURE() <<
"Non-fatal failure in some other thread. "
161 <<
"(This failure is expected.)";
164 void GenerateFatalFailureInAnotherThread(
bool is_fatal) {
165 ThreadWithParam<bool>
thread(&FailingThread, is_fatal,
nullptr);
169 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
174 CheckTestFailureCount(1);
177 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
180 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
182 AssertNoFatalFailureIgnoresFailuresInOtherThreads();
186 CheckTestFailureCount(1);
189 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
193 CheckTestFailureCount(2);
196 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
200 GenerateFatalFailureInAnotherThread(
true),
"expected");
201 CheckTestFailureCount(0);
204 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
207 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
212 CheckTestFailureCount(2);
215 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
219 GenerateFatalFailureInAnotherThread(
false),
"expected");
220 CheckTestFailureCount(0);
223 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
229 int main(
int argc,
char **argv) {
241 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
244 int main(
int argc,
char **argv) {
248 #endif // GTEST_IS_THREADSAFE