41#if GTEST_IS_THREADSAFE
46using internal::Notification;
47using internal::TestPropertyKeyIs;
48using internal::ThreadWithParam;
49using internal::scoped_ptr;
56const int kThreadCount = 50;
58std::string IdToKey(
int id,
const char* suffix) {
60 key <<
"key_" <<
id <<
"_" << suffix;
61 return key.GetString();
64std::string IdToString(
int id) {
67 return id_message.GetString();
70void ExpectKeyAndValueWereRecordedForId(
71 const std::vector<TestProperty>& properties,
72 int id,
const char* suffix) {
73 TestPropertyKeyIs matches_key(IdToKey(
id, suffix).c_str());
74 const std::vector<TestProperty>::const_iterator
property =
75 std::find_if(properties.begin(), properties.end(), matches_key);
77 <<
"expecting " << suffix <<
" value for id " <<
id;
83void ManyAsserts(
int id) {
88 for (
int i = 0; i < kThreadCount; i++) {
96 EXPECT_EQ(i, i) <<
"This shouldn't fail.";
107 EXPECT_LT(i, 0) <<
"This should always fail.";
111void CheckTestFailureCount(
int expected_failures) {
113 const TestResult*
const result = info->result();
114 GTEST_CHECK_(expected_failures == result->total_part_count())
115 <<
"Logged " << result->total_part_count() <<
" failures "
116 <<
" vs. " << expected_failures <<
" expected";
121TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
123 scoped_ptr<ThreadWithParam<int> > threads[kThreadCount];
124 Notification threads_can_start;
125 for (
int i = 0; i != kThreadCount; i++)
126 threads[i].reset(
new ThreadWithParam<int>(&ManyAsserts,
128 &threads_can_start));
130 threads_can_start.Notify();
133 for (
int i = 0; i != kThreadCount; i++)
139 const TestResult*
const result = info->result();
141 std::vector<TestProperty> properties;
144 for (
int i = 0; i < result->test_property_count(); ++i)
145 properties.push_back(result->GetTestProperty(i));
147 EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
148 <<
"String and int values recorded on each thread, "
149 <<
"as well as one shared_key";
150 for (
int i = 0; i < kThreadCount; ++i) {
151 ExpectKeyAndValueWereRecordedForId(properties, i,
"string");
152 ExpectKeyAndValueWereRecordedForId(properties, i,
"int");
154 CheckTestFailureCount(kThreadCount*kThreadCount);
157void FailingThread(
bool is_fatal) {
159 FAIL() <<
"Fatal failure in some other thread. "
160 <<
"(This failure is expected.)";
162 ADD_FAILURE() <<
"Non-fatal failure in some other thread. "
163 <<
"(This failure is expected.)";
166void GenerateFatalFailureInAnotherThread(
bool is_fatal) {
167 ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL);
171TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
176 CheckTestFailureCount(1);
179void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
182TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
184 AssertNoFatalFailureIgnoresFailuresInOtherThreads();
188 CheckTestFailureCount(1);
191TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
195 CheckTestFailureCount(2);
198TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
202 GenerateFatalFailureInAnotherThread(
true),
"expected");
203 CheckTestFailureCount(0);
206 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
209TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
214 CheckTestFailureCount(2);
217TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
221 GenerateFatalFailureInAnotherThread(
false),
"expected");
222 CheckTestFailureCount(0);
225 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
235 GTEST_CHECK_(result == 1) <<
"RUN_ALL_TESTS() did not fail as expected";
243 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
static void RecordProperty(const std::string &key, const std::string &value)
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
static UnitTest * GetInstance()
#define GTEST_LOG_(severity)
#define GTEST_CHECK_(condition)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define EXPECT_NO_FATAL_FAILURE(statement)
#define EXPECT_EQ(val1, val2)
#define SCOPED_TRACE(message)
#define ASSERT_NO_FATAL_FAILURE(statement)
#define ASSERT_LE(val1, val2)
#define ASSERT_FALSE(condition)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define TEST(test_case_name, test_name)
#define ASSERT_TRUE(condition)
#define EXPECT_LT(val1, val2)
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
LOGGING_API void printf(Category category, const char *format,...)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)