41 #ifdef GTEST_IS_THREADSAFE
46 using internal::Notification;
47 using internal::TestPropertyKeyIs;
48 using internal::ThreadWithParam;
55 const int kThreadCount = 50;
57 std::string IdToKey(
int id,
const char* suffix) {
59 key <<
"key_" <<
id <<
"_" << suffix;
60 return key.GetString();
63 std::string IdToString(
int id) {
66 return id_message.GetString();
69 void ExpectKeyAndValueWereRecordedForId(
70 const std::vector<TestProperty>& properties,
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) {
82 GTEST_LOG_(INFO) <<
"Thread #" <<
id <<
" running...";
86 for (
int i = 0;
i < kThreadCount;
i++) {
105 EXPECT_LT(
i, 0) <<
"This should always fail.";
109 void CheckTestFailureCount(
int expected_failures) {
111 const TestResult*
const result = info->
result();
112 GTEST_CHECK_(expected_failures == result->total_part_count())
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] = std::make_unique<ThreadWithParam<int>>(&ManyAsserts,
i,
127 threads_can_start.Notify();
130 for (
int i = 0;
i != kThreadCount;
i++) threads[
i]->Join();
135 const TestResult*
const result = info->
result();
137 std::vector<TestProperty> properties;
140 for (
int i = 0;
i < result->test_property_count(); ++
i)
141 properties.push_back(result->GetTestProperty(
i));
143 EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
144 <<
"String and int values recorded on each thread, "
145 <<
"as well as one shared_key";
146 for (
int i = 0;
i < kThreadCount; ++
i) {
147 ExpectKeyAndValueWereRecordedForId(properties,
i,
"string");
148 ExpectKeyAndValueWereRecordedForId(properties,
i,
"int");
150 CheckTestFailureCount(kThreadCount * kThreadCount);
153 void FailingThread(
bool is_fatal) {
155 FAIL() <<
"Fatal failure in some other thread. "
156 <<
"(This failure is expected.)";
158 ADD_FAILURE() <<
"Non-fatal failure in some other thread. "
159 <<
"(This failure is expected.)";
162 void GenerateFatalFailureInAnotherThread(
bool is_fatal) {
163 ThreadWithParam<bool> thread(&FailingThread, is_fatal,
nullptr);
167 TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
172 CheckTestFailureCount(1);
175 void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
178 TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
180 AssertNoFatalFailureIgnoresFailuresInOtherThreads();
184 CheckTestFailureCount(1);
187 TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
191 CheckTestFailureCount(2);
194 TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
199 CheckTestFailureCount(0);
202 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
205 TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
210 CheckTestFailureCount(2);
213 TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
217 GenerateFatalFailureInAnotherThread(
false),
"expected");
218 CheckTestFailureCount(0);
221 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
227 int main(
int argc,
char** argv) {
231 GTEST_CHECK_(result == 1) <<
"RUN_ALL_TESTS() did not fail as expected";
239 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {}
241 int main(
int argc,
char **argv) {
245 #endif // GTEST_IS_THREADSAFE
#define ASSERT_LE(val1, val2)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define GTEST_LOG_(severity)
#define TEST(test_suite_name, test_name)
#define EXPECT_LT(val1, val2)
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_NO_FATAL_FAILURE(statement)
#define ASSERT_TRUE(condition)
#define GTEST_CHECK_(condition)
#define ASSERT_NO_FATAL_FAILURE(statement)
TEST(GTestEnvVarTest, Dummy)
#define EXPECT_STREQ(s1, s2)
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
#define SCOPED_TRACE(message)
#define EXPECT_EQ(val1, val2)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
const TestResult * result() const
#define EXPECT_TRUE(condition)
static UnitTest * GetInstance()
static void RecordProperty(const std::string &key, const std::string &value)
#define ASSERT_FALSE(condition)