43 #define GTEST_IMPLEMENTATION_ 1
45 #undef GTEST_IMPLEMENTATION_
47 #if GTEST_IS_THREADSAFE
52 using internal::Notification;
53 using internal::TestPropertyKeyIs;
54 using internal::ThreadWithParam;
55 using internal::scoped_ptr;
62 const int kThreadCount = 50;
66 key <<
"key_" <<
id <<
"_" << suffix;
67 return key.GetString();
73 return id_message.GetString();
76 void ExpectKeyAndValueWereRecordedForId(
77 const std::vector<TestProperty>& properties,
78 int id,
const char* suffix) {
79 TestPropertyKeyIs matches_key(IdToKey(
id, suffix).c_str());
80 const std::vector<TestProperty>::const_iterator
property =
81 std::find_if(properties.begin(), properties.end(), matches_key);
83 <<
"expecting " << suffix <<
" value for id " <<
id;
89 void ManyAsserts(
int id) {
94 for (
int i = 0;
i < kThreadCount;
i++) {
113 EXPECT_LT(
i, 0) <<
"This should always fail.";
117 void CheckTestFailureCount(
int expected_failures) {
119 const TestResult*
const result = info->
result();
120 GTEST_CHECK_(expected_failures == result->total_part_count())
121 <<
"Logged " << result->total_part_count() <<
" failures "
122 <<
" vs. " << expected_failures <<
" expected";
127 TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
129 scoped_ptr<ThreadWithParam<int> > threads[kThreadCount];
130 Notification threads_can_start;
131 for (
int i = 0;
i != kThreadCount;
i++)
132 threads[
i].
reset(
new ThreadWithParam<int>(&ManyAsserts,
134 &threads_can_start));
136 threads_can_start.Notify();
139 for (
int i = 0;
i != kThreadCount;
i++)
145 const TestResult*
const result = info->
result();
147 std::vector<TestProperty> properties;
150 for (
int i = 0;
i < result->test_property_count(); ++
i)
151 properties.push_back(result->GetTestProperty(
i));
153 EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
154 <<
"String and int values recorded on each thread, "
155 <<
"as well as one shared_key";
156 for (
int i = 0;
i < kThreadCount; ++
i) {
157 ExpectKeyAndValueWereRecordedForId(properties,
i,
"string");
158 ExpectKeyAndValueWereRecordedForId(properties,
i,
"int");
160 CheckTestFailureCount(kThreadCount*kThreadCount);
163 void FailingThread(
bool is_fatal) {
165 FAIL() <<
"Fatal failure in some other thread. "
166 <<
"(This failure is expected.)";
168 ADD_FAILURE() <<
"Non-fatal failure in some other thread. "
169 <<
"(This failure is expected.)";
172 void GenerateFatalFailureInAnotherThread(
bool is_fatal) {
173 ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL);
177 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
182 CheckTestFailureCount(1);
185 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
188 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
190 AssertNoFatalFailureIgnoresFailuresInOtherThreads();
194 CheckTestFailureCount(1);
197 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
201 CheckTestFailureCount(2);
204 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
208 GenerateFatalFailureInAnotherThread(
true),
"expected");
209 CheckTestFailureCount(0);
212 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
215 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
220 CheckTestFailureCount(2);
223 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
227 GenerateFatalFailureInAnotherThread(
false),
"expected");
228 CheckTestFailureCount(0);
231 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
237 int main(
int argc,
char **argv) {
241 GTEST_CHECK_(result == 1) <<
"RUN_ALL_TESTS() did not fail as expected";
249 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
252 int main(
int argc,
char **argv) {
256 #endif // GTEST_IS_THREADSAFE