35 #include "gtest/gtest.h" 43 #define GTEST_IMPLEMENTATION_ 1 44 #include "src/gtest-internal-inl.h" 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) {
90 GTEST_LOG_(INFO) <<
"Thread #" <<
id <<
" running...";
94 for (
int i = 0; i < kThreadCount; i++) {
102 EXPECT_EQ(i, i) <<
"This shouldn't fail.";
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 int main(int argc, char **argv)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define ASSERT_NO_FATAL_FAILURE(statement)
#define EXPECT_NO_FATAL_FAILURE(statement)
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define EXPECT_LT(val1, val2)
#define ASSERT_LE(val1, val2)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(expected, actual)
TEST(GTestEnvVarTest, Dummy)
#define ASSERT_TRUE(condition)
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
void InitGoogleTest(int *argc, char **argv)
const TestResult * result() const
static UnitTest * GetInstance()
#define SCOPED_TRACE(message)
static void RecordProperty(const std::string &key, const std::string &value)
#define GTEST_CHECK_(condition)
#define EXPECT_EQ(expected, actual)
#define GTEST_LOG_(severity)
#define ASSERT_FALSE(condition)
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)