40#if GTEST_IS_THREADSAFE
45using internal::Notification;
46using internal::TestPropertyKeyIs;
47using internal::ThreadWithParam;
54const int kThreadCount = 50;
56std::string IdToKey(
int id,
const char* suffix) {
58 key <<
"key_" <<
id <<
"_" << suffix;
59 return key.GetString();
62std::string IdToString(
int id) {
65 return id_message.GetString();
68void 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;
81void ManyAsserts(
int id) {
86 for (
int i = 0;
i < kThreadCount;
i++) {
105 EXPECT_LT(
i, 0) <<
"This should always fail.";
109void 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";
119TEST(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);
155void 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.)";
164void GenerateFatalFailureInAnotherThread(
bool is_fatal) {
165 ThreadWithParam<bool> thread(&FailingThread, is_fatal,
nullptr);
169TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
174 CheckTestFailureCount(1);
177void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
180TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
182 AssertNoFatalFailureIgnoresFailuresInOtherThreads();
186 CheckTestFailureCount(1);
189TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
193 CheckTestFailureCount(2);
196TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
200 GenerateFatalFailureInAnotherThread(
true),
"expected");
201 CheckTestFailureCount(0);
204 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
207TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
212 CheckTestFailureCount(2);
215TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
219 GenerateFatalFailureInAnotherThread(
false),
"expected");
220 CheckTestFailureCount(0);
223 ADD_FAILURE() <<
"This is an expected non-fatal failure.";
229int main(
int argc,
char **argv) {
233 GTEST_CHECK_(result == 1) <<
"RUN_ALL_TESTS() did not fail as expected";
241 DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
244int main(
int argc,
char **argv) {
#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 ASSERT_TRUE(condition)
#define EXPECT_LT(val1, val2)
#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 GTEST_LOG_(severity)
#define GTEST_CHECK_(condition)
int main(int argc, char **argv)
TEST(StressTest, DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe)
TEST(GTestEnvVarTest, Dummy)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
static void RecordProperty(const std::string &key, const std::string &value)
const TestResult * result() const
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
static UnitTest * GetInstance()