40 #if GTEST_IS_THREADSAFE
45 using internal::Notification;
46 using internal::TestPropertyKeyIs;
47 using internal::ThreadWithParam;
54 const int kThreadCount = 50;
56 std::string IdToKey(
int id,
const char* suffix) {
58 key <<
"key_" <<
id <<
"_" << suffix;
59 return key.GetString();
62 std::string IdToString(
int id) {
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) {
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].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));
145 EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
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) {
233 GTEST_CHECK_(result == 1) <<
"RUN_ALL_TESTS() did not fail as expected";
241 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
244 int main(
int argc,
char **argv) {
248 #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)