34 #include "gmock/gmock.h"
35 #include "gtest/gtest.h"
41 using ::testing::internal::ThreadWithParam;
45 const int kMaxTestThreads = 50;
48 const int kRepeat = 50;
58 void JoinAndDelete(ThreadWithParam<T>* t) {
68 void TestConcurrentMockObjects(
Dummy ) {
74 .WillByDefault(
Return(
'b'));
76 .WillByDefault(
Return(
'a'));
99 void Helper1(Helper1Param param) {
100 for (
int i = 0;
i < kRepeat;
i++) {
101 const char ch = param.mock_foo->Baz(
"a",
"b");
111 EXPECT_EQ(
'\0', param.mock_foo->Baz(
"x",
"y")) <<
"Expected failure.";
119 void TestConcurrentCallsOnSameObject(
Dummy ) {
123 .WillByDefault(
Return(1));
126 .WillRepeatedly(
Return(
'a'));
132 const Helper1Param param = { &
foo, &count1 };
133 ThreadWithParam<Helper1Param>*
const t =
134 new ThreadWithParam<Helper1Param>(Helper1, param,
nullptr);
137 const Helper1Param param2 = { &
foo, &count2 };
150 void Helper2(MockFoo*
foo) {
151 for (
int i = 0;
i < kRepeat;
i++) {
158 void TestPartiallyOrderedExpectationsWithThreads(
Dummy ) {
172 .RetiresOnSaturation();
187 ThreadWithParam<MockFoo*>*
const t =
188 new ThreadWithParam<MockFoo*>(Helper2, &
foo,
nullptr);
197 TEST(StressTest, CanUseGMockWithThreads) {
199 &TestConcurrentMockObjects,
200 &TestConcurrentCallsOnSameObject,
201 &TestPartiallyOrderedExpectationsWithThreads,
204 const int kRoutines =
sizeof(test_routines)/
sizeof(test_routines[0]);
205 const int kCopiesOfEachRoutine = kMaxTestThreads / kRoutines;
206 const int kTestThreads = kCopiesOfEachRoutine * kRoutines;
207 ThreadWithParam<Dummy>*
threads[kTestThreads] = {};
208 for (
int i = 0;
i < kTestThreads;
i++) {
210 threads[
i] =
new ThreadWithParam<Dummy>(test_routines[i % kRoutines],
216 for (
int i = 0;
i < kTestThreads;
i++) {
222 const TestResult&
result = *info->result();
223 const int kExpectedFailures = (3*kRepeat + 1)*kCopiesOfEachRoutine;
225 <<
"Expected " << kExpectedFailures <<
" failures, but got "
226 <<
result.total_part_count();
232 int main(
int argc,
char **argv) {