41 using ::testing::internal::ThreadWithParam;
45 const int kMaxTestThreads = 50;
48 const int kRepeat = 50;
53 MOCK_METHOD2(Baz,
char(
const char* s1,
const std::string& s2));
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],
212 GTEST_LOG_(INFO) <<
"Thread #" <<
i <<
" running . . .";
216 for (
int i = 0;
i < kTestThreads;
i++) {
217 JoinAndDelete(threads[
i]);
222 const TestResult& result = *info->
result();
223 const int kExpectedFailures = (3*kRepeat + 1)*kCopiesOfEachRoutine;
224 GTEST_CHECK_(kExpectedFailures == result.total_part_count())
225 <<
"Expected " << kExpectedFailures <<
" failures, but got "
226 << result.total_part_count();
232 int main(
int argc,
char **argv) {
236 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()