40 using ::testing::internal::ThreadWithParam;
44 const int kMaxTestThreads = 50;
47 const int kRepeat = 50;
52 MOCK_METHOD2(Baz,
char(
const char* s1,
const std::string& s2));
57 void JoinAndDelete(ThreadWithParam<T>* t) {
66 void TestConcurrentMockObjects(Dummy ) {
93 void Helper1(Helper1Param param) {
94 for (
int i = 0;
i < kRepeat;
i++) {
95 const char ch = param.mock_foo->Baz(
"a",
"b");
105 EXPECT_EQ(
'\0', param.mock_foo->Baz(
"x",
"y")) <<
"Expected failure.";
113 void TestConcurrentCallsOnSameObject(Dummy ) {
123 const Helper1Param param = {&
foo, &count1};
124 ThreadWithParam<Helper1Param>*
const t =
125 new ThreadWithParam<Helper1Param>(Helper1, param,
nullptr);
128 const Helper1Param param2 = {&
foo, &count2};
141 void Helper2(MockFoo* foo) {
142 for (
int i = 0;
i < kRepeat;
i++) {
149 void TestPartiallyOrderedExpectationsWithThreads(Dummy ) {
162 .RetiresOnSaturation();
163 EXPECT_CALL(foo, Bar(3)).Times(2 * kRepeat).InSequence(s2);
174 ThreadWithParam<MockFoo*>*
const t =
175 new ThreadWithParam<MockFoo*>(Helper2, &
foo,
nullptr);
184 TEST(StressTest, CanUseGMockWithThreads) {
185 void (*test_routines[])(Dummy dummy) = {
186 &TestConcurrentMockObjects,
187 &TestConcurrentCallsOnSameObject,
188 &TestPartiallyOrderedExpectationsWithThreads,
191 const int kRoutines =
sizeof(test_routines) /
sizeof(test_routines[0]);
192 const int kCopiesOfEachRoutine = kMaxTestThreads / kRoutines;
193 const int kTestThreads = kCopiesOfEachRoutine * kRoutines;
194 ThreadWithParam<Dummy>* threads[kTestThreads] = {};
195 for (
int i = 0;
i < kTestThreads;
i++) {
197 threads[
i] =
new ThreadWithParam<Dummy>(test_routines[
i % kRoutines],
199 GTEST_LOG_(INFO) <<
"Thread #" <<
i <<
" running . . .";
203 for (
int i = 0;
i < kTestThreads;
i++) {
204 JoinAndDelete(threads[
i]);
209 const TestResult& result = *info->
result();
210 const int kExpectedFailures = (3 * kRepeat + 1) * kCopiesOfEachRoutine;
211 GTEST_CHECK_(kExpectedFailures == result.total_part_count())
212 <<
"Expected " << kExpectedFailures <<
" failures, but got "
213 << result.total_part_count();
219 int main(
int argc,
char** argv) {
223 GTEST_CHECK_(exit_code != 0) <<
"RUN_ALL_TESTS() did not fail as expected";
GTEST_API_ Cardinality AtMost(int n)
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
#define GTEST_LOG_(severity)
#define ON_CALL(obj, call)
#define MOCK_METHOD2(m,...)
#define GTEST_CHECK_(condition)
TEST(GTestEnvVarTest, Dummy)
ADVar foo(double d, ADVar x, ADVar y)
#define MOCK_METHOD1(m,...)
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
internal::DoDefaultAction DoDefault()
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
#define EXPECT_EQ(val1, val2)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
const TestResult * result() const
static UnitTest * GetInstance()