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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Tests that Google Mock constructs can be used in a large number of
33 // threads concurrently.
35 #include "gmock/gmock.h"
37 #include "gtest/gtest.h"
42 // From "gtest/internal/gtest-port.h".
43 using ::testing::internal::ThreadWithParam
;
45 // The maximum number of test threads (not including helper threads)
47 const int kMaxTestThreads
= 50;
49 // How many times to repeat a task in a test thread.
50 const int kRepeat
= 50;
54 MOCK_METHOD1(Bar
, int(int n
)); // NOLINT
55 MOCK_METHOD2(Baz
, char(const char* s1
, const std::string
& s2
)); // NOLINT
58 // Helper for waiting for the given thread to finish and then deleting it.
60 void JoinAndDelete(ThreadWithParam
<T
>* t
) {
65 using internal::linked_ptr
;
67 // Helper classes for testing using linked_ptr concurrently.
71 explicit Base(int a_x
) : x_(a_x
) {}
73 int x() const { return x_
; }
78 class Derived1
: public Base
{
80 Derived1(int a_x
, int a_y
) : Base(a_x
), y_(a_y
) {}
81 int y() const { return y_
; }
86 class Derived2
: public Base
{
88 Derived2(int a_x
, int a_z
) : Base(a_x
), z_(a_z
) {}
89 int z() const { return z_
; }
94 linked_ptr
<Derived1
> pointer1(new Derived1(1, 2));
95 linked_ptr
<Derived2
> pointer2(new Derived2(3, 4));
99 // Tests that we can copy from a linked_ptr and read it concurrently.
100 void TestConcurrentCopyAndReadLinkedPtr(Dummy
/* dummy */) {
101 // Reads pointer1 and pointer2 while they are being copied from in
103 EXPECT_EQ(1, pointer1
->x());
104 EXPECT_EQ(2, pointer1
->y());
105 EXPECT_EQ(3, pointer2
->x());
106 EXPECT_EQ(4, pointer2
->z());
108 // Copies from pointer1.
109 linked_ptr
<Derived1
> p1(pointer1
);
110 EXPECT_EQ(1, p1
->x());
111 EXPECT_EQ(2, p1
->y());
113 // Assigns from pointer2 where the LHS was empty.
116 EXPECT_EQ(1, p2
->x());
118 // Assigns from pointer2 where the LHS was not empty.
120 EXPECT_EQ(3, p2
->x());
123 const linked_ptr
<Derived1
> p0(new Derived1(1, 2));
125 // Tests that we can concurrently modify two linked_ptrs that point to
127 void TestConcurrentWriteToEqualLinkedPtr(Dummy
/* dummy */) {
128 // p1 and p2 point to the same, shared thing. One thread resets p1.
129 // Another thread assigns to p2. This will cause the same
130 // underlying "ring" to be updated concurrently.
131 linked_ptr
<Derived1
> p1(p0
);
132 linked_ptr
<Derived1
> p2(p0
);
134 EXPECT_EQ(1, p1
->x());
135 EXPECT_EQ(2, p1
->y());
137 EXPECT_EQ(1, p2
->x());
138 EXPECT_EQ(2, p2
->y());
143 EXPECT_EQ(1, p2
->x());
144 EXPECT_EQ(2, p2
->y());
147 // Tests that different mock objects can be used in their respective
148 // threads. This should generate no Google Test failure.
149 void TestConcurrentMockObjects(Dummy
/* dummy */) {
150 // Creates a mock and does some typical operations on it.
153 .WillByDefault(Return(1));
154 ON_CALL(foo
, Baz(_
, _
))
155 .WillByDefault(Return('b'));
156 ON_CALL(foo
, Baz(_
, "you"))
157 .WillByDefault(Return('a'));
159 EXPECT_CALL(foo
, Bar(0))
161 EXPECT_CALL(foo
, Baz(_
, _
));
162 EXPECT_CALL(foo
, Baz("hi", "you"))
163 .WillOnce(Return('z'))
164 .WillRepeatedly(DoDefault());
166 EXPECT_EQ(1, foo
.Bar(0));
167 EXPECT_EQ(1, foo
.Bar(0));
168 EXPECT_EQ('z', foo
.Baz("hi", "you"));
169 EXPECT_EQ('a', foo
.Baz("hi", "you"));
170 EXPECT_EQ('b', foo
.Baz("hi", "me"));
173 // Tests invoking methods of the same mock object in multiple threads.
175 struct Helper1Param
{
180 void Helper1(Helper1Param param
) {
181 for (int i
= 0; i
< kRepeat
; i
++) {
182 const char ch
= param
.mock_foo
->Baz("a", "b");
184 // It was an expected call.
187 // It was an excessive call.
191 // An unexpected call.
192 EXPECT_EQ('\0', param
.mock_foo
->Baz("x", "y")) << "Expected failure.";
194 // An uninteresting call.
195 EXPECT_EQ(1, param
.mock_foo
->Bar(5));
199 // This should generate 3*kRepeat + 1 failures in total.
200 void TestConcurrentCallsOnSameObject(Dummy
/* dummy */) {
204 .WillByDefault(Return(1));
205 EXPECT_CALL(foo
, Baz(_
, "b"))
207 .WillRepeatedly(Return('a'));
208 EXPECT_CALL(foo
, Baz(_
, "c")); // Expected to be unsatisfied.
210 // This chunk of code should generate kRepeat failures about
211 // excessive calls, and 2*kRepeat failures about unexpected calls.
213 const Helper1Param param
= { &foo
, &count1
};
214 ThreadWithParam
<Helper1Param
>* const t
=
215 new ThreadWithParam
<Helper1Param
>(Helper1
, param
, NULL
);
218 const Helper1Param param2
= { &foo
, &count2
};
222 EXPECT_EQ(kRepeat
, count1
+ count2
);
224 // foo's destructor should generate one failure about unsatisfied
228 // Tests using the same mock object in multiple threads when the
229 // expectations are partially ordered.
231 void Helper2(MockFoo
* foo
) {
232 for (int i
= 0; i
< kRepeat
; i
++) {
238 // This should generate no Google Test failures.
239 void TestPartiallyOrderedExpectationsWithThreads(Dummy
/* dummy */) {
245 EXPECT_CALL(foo
, Bar(0));
246 EXPECT_CALL(foo
, Bar(1))
250 EXPECT_CALL(foo
, Bar(2))
253 .RetiresOnSaturation();
254 EXPECT_CALL(foo
, Bar(3))
260 EXPECT_CALL(foo
, Bar(2))
262 EXPECT_CALL(foo
, Bar(4));
268 ThreadWithParam
<MockFoo
*>* const t
=
269 new ThreadWithParam
<MockFoo
*>(Helper2
, &foo
, NULL
);
277 // Tests using Google Mock constructs in many threads concurrently.
278 TEST(StressTest
, CanUseGMockWithThreads
) {
279 void (*test_routines
[])(Dummy dummy
) = {
280 &TestConcurrentCopyAndReadLinkedPtr
,
281 &TestConcurrentWriteToEqualLinkedPtr
,
282 &TestConcurrentMockObjects
,
283 &TestConcurrentCallsOnSameObject
,
284 &TestPartiallyOrderedExpectationsWithThreads
,
287 const int kRoutines
= sizeof(test_routines
)/sizeof(test_routines
[0]);
288 const int kCopiesOfEachRoutine
= kMaxTestThreads
/ kRoutines
;
289 const int kTestThreads
= kCopiesOfEachRoutine
* kRoutines
;
290 ThreadWithParam
<Dummy
>* threads
[kTestThreads
] = {};
291 for (int i
= 0; i
< kTestThreads
; i
++) {
292 // Creates a thread to run the test function.
294 new ThreadWithParam
<Dummy
>(test_routines
[i
% kRoutines
], Dummy(), NULL
);
295 GTEST_LOG_(INFO
) << "Thread #" << i
<< " running . . .";
298 // At this point, we have many threads running.
299 for (int i
= 0; i
< kTestThreads
; i
++) {
300 JoinAndDelete(threads
[i
]);
303 // Ensures that the correct number of failures have been reported.
304 const TestInfo
* const info
= UnitTest::GetInstance()->current_test_info();
305 const TestResult
& result
= *info
->result();
306 const int kExpectedFailures
= (3*kRepeat
+ 1)*kCopiesOfEachRoutine
;
307 GTEST_CHECK_(kExpectedFailures
== result
.total_part_count())
308 << "Expected " << kExpectedFailures
<< " failures, but got "
309 << result
.total_part_count();
313 } // namespace testing
315 int main(int argc
, char **argv
) {
316 testing::InitGoogleMock(&argc
, argv
);
318 const int exit_code
= RUN_ALL_TESTS(); // Expected to fail.
319 GTEST_CHECK_(exit_code
!= 0) << "RUN_ALL_TESTS() did not fail as expected";