1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Tests that Google Mock constructs can be used in a large number of
32 // threads concurrently.
34 #include "gmock/gmock.h"
35 #include "gtest/gtest.h"
41 using ::testing::internal::ThreadWithParam;
43 // The maximum number of test threads (not including helper threads)
45 const int kMaxTestThreads = 50;
47 // How many times to repeat a task in a test thread.
48 const int kRepeat = 50;
52 MOCK_METHOD1(Bar, int(int n)); // NOLINT
53 MOCK_METHOD2(Baz, char(const char* s1, const std::string& s2)); // NOLINT
56 // Helper for waiting for the given thread to finish and then deleting it.
58 void JoinAndDelete(ThreadWithParam<T>* t) {
66 // Tests that different mock objects can be used in their respective
67 // threads. This should generate no Google Test failure.
68 void TestConcurrentMockObjects(Dummy /* dummy */) {
69 // Creates a mock and does some typical operations on it.
72 .WillByDefault(Return(1));
73 ON_CALL(foo, Baz(_, _))
74 .WillByDefault(Return('b'));
75 ON_CALL(foo, Baz(_, "you"))
76 .WillByDefault(Return('a'));
78 EXPECT_CALL(foo, Bar(0))
80 EXPECT_CALL(foo, Baz(_, _));
81 EXPECT_CALL(foo, Baz("hi", "you"))
82 .WillOnce(Return('z'))
83 .WillRepeatedly(DoDefault());
85 EXPECT_EQ(1, foo.Bar(0));
86 EXPECT_EQ(1, foo.Bar(0));
87 EXPECT_EQ('z', foo.Baz("hi", "you"));
88 EXPECT_EQ('a', foo.Baz("hi", "you"));
89 EXPECT_EQ('b', foo.Baz("hi", "me"));
92 // Tests invoking methods of the same mock object in multiple threads.
99 void Helper1(Helper1Param param) {
100 for (int i = 0; i < kRepeat; i++) {
101 const char ch = param.mock_foo->Baz("a", "b");
103 // It was an expected call.
106 // It was an excessive call.
110 // An unexpected call.
111 EXPECT_EQ('\0', param.mock_foo->Baz("x", "y")) << "Expected failure.";
113 // An uninteresting call.
114 EXPECT_EQ(1, param.mock_foo->Bar(5));
118 // This should generate 3*kRepeat + 1 failures in total.
119 void TestConcurrentCallsOnSameObject(Dummy /* dummy */) {
123 .WillByDefault(Return(1));
124 EXPECT_CALL(foo, Baz(_, "b"))
126 .WillRepeatedly(Return('a'));
127 EXPECT_CALL(foo, Baz(_, "c")); // Expected to be unsatisfied.
129 // This chunk of code should generate kRepeat failures about
130 // excessive calls, and 2*kRepeat failures about unexpected calls.
132 const Helper1Param param = { &foo, &count1 };
133 ThreadWithParam<Helper1Param>* const t =
134 new ThreadWithParam<Helper1Param>(Helper1, param, nullptr);
137 const Helper1Param param2 = { &foo, &count2 };
141 EXPECT_EQ(kRepeat, count1 + count2);
143 // foo's destructor should generate one failure about unsatisfied
147 // Tests using the same mock object in multiple threads when the
148 // expectations are partially ordered.
150 void Helper2(MockFoo* foo) {
151 for (int i = 0; i < kRepeat; i++) {
157 // This should generate no Google Test failures.
158 void TestPartiallyOrderedExpectationsWithThreads(Dummy /* dummy */) {
164 EXPECT_CALL(foo, Bar(0));
165 EXPECT_CALL(foo, Bar(1))
169 EXPECT_CALL(foo, Bar(2))
172 .RetiresOnSaturation();
173 EXPECT_CALL(foo, Bar(3))
179 EXPECT_CALL(foo, Bar(2))
181 EXPECT_CALL(foo, Bar(4));
187 ThreadWithParam<MockFoo*>* const t =
188 new ThreadWithParam<MockFoo*>(Helper2, &foo, nullptr);
196 // Tests using Google Mock constructs in many threads concurrently.
197 TEST(StressTest, CanUseGMockWithThreads) {
198 void (*test_routines[])(Dummy dummy) = {
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++) {
209 // Creates a thread to run the test function.
210 threads[i] = new ThreadWithParam<Dummy>(test_routines[i % kRoutines],
212 GTEST_LOG_(INFO) << "Thread #" << i << " running . . .";
215 // At this point, we have many threads running.
216 for (int i = 0; i < kTestThreads; i++) {
217 JoinAndDelete(threads[i]);
220 // Ensures that the correct number of failures have been reported.
221 const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
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();
230 } // namespace testing
232 int main(int argc, char **argv) {
233 testing::InitGoogleMock(&argc, argv);
235 const int exit_code = RUN_ALL_TESTS(); // Expected to fail.
236 GTEST_CHECK_(exit_code != 0) << "RUN_ALL_TESTS() did not fail as expected";