aboutsummaryrefslogtreecommitdiffstats
path: root/googlemock/test/gmock_stress_test.cc
blob: 20725d69b7bf76fab1f8d91f59ff8d024588cae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


// Tests that Google Mock constructs can be used in a large number of
// threads concurrently.

#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace testing {
namespace {

// From gtest-port.h.
using ::testing::internal::ThreadWithParam;

// The maximum number of test threads (not including helper threads)
// to create.
const int kMaxTestThreads = 50;

// How many times to repeat a task in a test thread.
const int kRepeat = 50;

class MockFoo {
 public:
  MOCK_METHOD1(Bar, int(int n));  // NOLINT
  MOCK_METHOD2(Baz, char(const char* s1, const std::string& s2));  // NOLINT
};

// Helper for waiting for the given thread to finish and then deleting it.
template <typename T>
void JoinAndDelete(ThreadWithParam<T>* t) {
  t->Join();
  delete t;
}

struct Dummy {};


// Tests that different mock objects can be used in their respective
// threads.  This should generate no Google Test failure.
void TestConcurrentMockObjects(Dummy /* dummy */) {
  // Creates a mock and does some typical operations on it.
  MockFoo foo;
  ON_CALL(foo, Bar(_))
      .WillByDefault(Return(1));
  ON_CALL(foo, Baz(_, _))
      .WillByDefault(Return('b'));
  ON_CALL(foo, Baz(_, "you"))
      .WillByDefault(Return('a'));

  EXPECT_CALL(foo, Bar(0))
      .Times(AtMost(3));
  EXPECT_CALL(foo, Baz(_, _));
  EXPECT_CALL(foo, Baz("hi", "you"))
      .WillOnce(Return('z'))
      .WillRepeatedly(DoDefault());

  EXPECT_EQ(1, foo.Bar(0));
  EXPECT_EQ(1, foo.Bar(0));
  EXPECT_EQ('z', foo.Baz("hi", "you"));
  EXPECT_EQ('a', foo.Baz("hi", "you"));
  EXPECT_EQ('b', foo.Baz("hi", "me"));
}

// Tests invoking methods of the same mock object in multiple threads.

struct Helper1Param {
  MockFoo* mock_foo;
  int* count;
};

void Helper1(Helper1Param param) {
  for (int i = 0; i < kRepeat; i++) {
    const char ch = param.mock_foo->Baz("a", "b");
    if (ch == 'a') {
      // It was an expected call.
      (*param.count)++;
    } else {
      // It was an excessive call.
      EXPECT_EQ('\0', ch);
    }

    // An unexpected call.
    EXPECT_EQ('\0', param.mock_foo->Baz("x", "y")) << "Expected failure.";

    // An uninteresting call.
    EXPECT_EQ(1, param.mock_foo->Bar(5));
  }
}

// This should generate 3*kRepeat + 1 failures in total.
void TestConcurrentCallsOnSameObject(Dummy /* dummy */) {
  MockFoo foo;

  ON_CALL(foo, Bar(_))
      .WillByDefault(Return(1));
  EXPECT_CALL(foo, Baz(_, "b"))
      .Times(kRepeat)
      .WillRepeatedly(Return('a'));
  EXPECT_CALL(foo, Baz(_, "c"));  // Expected to be unsatisfied.

  // This chunk of code should generate kRepeat failures about
  // excessive calls, and 2*kRepeat failures about unexpected calls.
  int count1 = 0;
  const Helper1Param param = { &foo, &count1 };
  ThreadWithParam<Helper1Param>* const t =
      new ThreadWithParam<Helper1Param>(Helper1, param, nullptr);

  int count2 = 0;
  const Helper1Param param2 = { &foo, &count2 };
  Helper1(param2);
  JoinAndDelete(t);

  EXPECT_EQ(kRepeat, count1 + count2);

  // foo's destructor should generate one failure about unsatisfied
  // expectation.
}

// Tests using the same mock object in multiple threads when the
// expectations are partially ordered.

void Helper2(MockFoo* foo) {
  for (int i = 0; i < kRepeat; i++) {
    foo->Bar(2);
    foo->Bar(3);
  }
}

// This should generate no Google Test failures.
void TestPartiallyOrderedExpectationsWithThreads(Dummy /* dummy */) {
  MockFoo foo;
  Sequence s1, s2;

  {
    InSequence dummy;
    EXPECT_CALL(foo, Bar(0));
    EXPECT_CALL(foo, Bar(1))
        .InSequence(s1, s2);
  }

  EXPECT_CALL(foo, Bar(2))
      .Times(2*kRepeat)
      .InSequence(s1)
      .RetiresOnSaturation();
  EXPECT_CALL(foo, Bar(3))
      .Times(2*kRepeat)
      .InSequence(s2);

  {
    InSequence dummy;
    EXPECT_CALL(foo, Bar(2))
        .InSequence(s1, s2);
    EXPECT_CALL(foo, Bar(4));
  }

  foo.Bar(0);
  foo.Bar(1);

  ThreadWithParam<MockFoo*>* const t =
      new ThreadWithParam<MockFoo*>(Helper2, &foo, nullptr);
  Helper2(&foo);
  JoinAndDelete(t);

  foo.Bar(2);
  foo.Bar(4);
}

// Tests using Google Mock constructs in many threads concurrently.
TEST(StressTest, CanUseGMockWithThreads) {
  void (*test_routines[])(Dummy dummy) = {
    &TestConcurrentMockObjects,
    &TestConcurrentCallsOnSameObject,
    &TestPartiallyOrderedExpectationsWithThreads,
  };

  const int kRoutines = sizeof(test_routines)/sizeof(test_routines[0]);
  const int kCopiesOfEachRoutine = kMaxTestThreads / kRoutines;
  const int kTestThreads = kCopiesOfEachRoutine * kRoutines;
  ThreadWithParam<Dummy>* threads[kTestThreads] = {};
  for (int i = 0; i < kTestThreads; i++) {
    // Creates a thread to run the test function.
    threads[i] = new ThreadWithParam<Dummy>(test_routines[i % kRoutines],
                                            Dummy(), nullptr);
    GTEST_LOG_(INFO) << "Thread #" << i << " running . . .";
  }

  // At this point, we have many threads running.
  for (int i = 0; i < kTestThreads; i++) {
    JoinAndDelete(threads[i]);
  }

  // Ensures that the correct number of failures have been reported.
  const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
  const TestResult& result = *info->result();
  const int kExpectedFailures = (3*kRepeat + 1)*kCopiesOfEachRoutine;
  GTEST_CHECK_(kExpectedFailures == result.total_part_count())
      << "Expected " << kExpectedFailures << " failures, but got "
      << result.total_part_count();
}

}  // namespace
}  // namespace testing

int main(int argc, char **argv) {
  testing::InitGoogleMock(&argc, argv);

  const int exit_code = RUN_ALL_TESTS();  // Expected to fail.
  GTEST_CHECK_(exit_code != 0) << "RUN_ALL_TESTS() did not fail as expected";

  printf("\nPASS\n");
  return 0;
}