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"
36 #include "gtest/gtest.h"
41 // From <gtest/internal/gtest-port.h>.
42 using ::testing::internal::ThreadWithParam;
44 // The maximum number of test threads (not including helper threads)
46 const int kMaxTestThreads = 50;
48 // How many times to repeat a task in a test thread.
49 const int kRepeat = 50;
53 MOCK_METHOD1(Bar, int(int n)); // NOLINT
54 MOCK_METHOD2(Baz, char(const char* s1, const std::string& s2)); // NOLINT
57 // Helper for waiting for the given thread to finish and then deleting it.
59 void JoinAndDelete(ThreadWithParam<T>* t) {
64 using internal::linked_ptr;
66 // Helper classes for testing using linked_ptr concurrently.
70 explicit Base(int a_x) : x_(a_x) {}
72 int x() const { return x_; }
77 class Derived1 : public Base {
79 Derived1(int a_x, int a_y) : Base(a_x), y_(a_y) {}
80 int y() const { return y_; }
85 class Derived2 : public Base {
87 Derived2(int a_x, int a_z) : Base(a_x), z_(a_z) {}
88 int z() const { return z_; }
93 linked_ptr<Derived1> pointer1(new Derived1(1, 2));
94 linked_ptr<Derived2> pointer2(new Derived2(3, 4));
98 // Tests that we can copy from a linked_ptr and read it concurrently.
99 void TestConcurrentCopyAndReadLinkedPtr(Dummy /* dummy */) {
100 // Reads pointer1 and pointer2 while they are being copied from in
102 EXPECT_EQ(1, pointer1->x());
103 EXPECT_EQ(2, pointer1->y());
104 EXPECT_EQ(3, pointer2->x());
105 EXPECT_EQ(4, pointer2->z());
107 // Copies from pointer1.
108 linked_ptr<Derived1> p1(pointer1);
109 EXPECT_EQ(1, p1->x());
110 EXPECT_EQ(2, p1->y());
112 // Assigns from pointer2 where the LHS was empty.
115 EXPECT_EQ(1, p2->x());
117 // Assigns from pointer2 where the LHS was not empty.
119 EXPECT_EQ(3, p2->x());
122 const linked_ptr<Derived1> p0(new Derived1(1, 2));
124 // Tests that we can concurrently modify two linked_ptrs that point to
126 void TestConcurrentWriteToEqualLinkedPtr(Dummy /* dummy */) {
127 // p1 and p2 point to the same, shared thing. One thread resets p1.
128 // Another thread assigns to p2. This will cause the same
129 // underlying "ring" to be updated concurrently.
130 linked_ptr<Derived1> p1(p0);
131 linked_ptr<Derived1> p2(p0);
133 EXPECT_EQ(1, p1->x());
134 EXPECT_EQ(2, p1->y());
136 EXPECT_EQ(1, p2->x());
137 EXPECT_EQ(2, p2->y());
142 EXPECT_EQ(1, p2->x());
143 EXPECT_EQ(2, p2->y());
146 // Tests that different mock objects can be used in their respective
147 // threads. This should generate no Google Test failure.
148 void TestConcurrentMockObjects(Dummy /* dummy */) {
149 // Creates a mock and does some typical operations on it.
152 .WillByDefault(Return(1));
153 ON_CALL(foo, Baz(_, _))
154 .WillByDefault(Return('b'));
155 ON_CALL(foo, Baz(_, "you"))
156 .WillByDefault(Return('a'));
158 EXPECT_CALL(foo, Bar(0))
160 EXPECT_CALL(foo, Baz(_, _));
161 EXPECT_CALL(foo, Baz("hi", "you"))
162 .WillOnce(Return('z'))
163 .WillRepeatedly(DoDefault());
165 EXPECT_EQ(1, foo.Bar(0));
166 EXPECT_EQ(1, foo.Bar(0));
167 EXPECT_EQ('z', foo.Baz("hi", "you"));
168 EXPECT_EQ('a', foo.Baz("hi", "you"));
169 EXPECT_EQ('b', foo.Baz("hi", "me"));
172 // Tests invoking methods of the same mock object in multiple threads.
174 struct Helper1Param {
179 void Helper1(Helper1Param param) {
180 for (int i = 0; i < kRepeat; i++) {
181 const char ch = param.mock_foo->Baz("a", "b");
183 // It was an expected call.
186 // It was an excessive call.
190 // An unexpected call.
191 EXPECT_EQ('\0', param.mock_foo->Baz("x", "y")) << "Expected failure.";
193 // An uninteresting call.
194 EXPECT_EQ(1, param.mock_foo->Bar(5));
198 // This should generate 3*kRepeat + 1 failures in total.
199 void TestConcurrentCallsOnSameObject(Dummy /* dummy */) {
203 .WillByDefault(Return(1));
204 EXPECT_CALL(foo, Baz(_, "b"))
206 .WillRepeatedly(Return('a'));
207 EXPECT_CALL(foo, Baz(_, "c")); // Expected to be unsatisfied.
209 // This chunk of code should generate kRepeat failures about
210 // excessive calls, and 2*kRepeat failures about unexpected calls.
212 const Helper1Param param = { &foo, &count1 };
213 ThreadWithParam<Helper1Param>* const t =
214 new ThreadWithParam<Helper1Param>(Helper1, param, NULL);
217 const Helper1Param param2 = { &foo, &count2 };
221 EXPECT_EQ(kRepeat, count1 + count2);
223 // foo's destructor should generate one failure about unsatisfied
227 // Tests using the same mock object in multiple threads when the
228 // expectations are partially ordered.
230 void Helper2(MockFoo* foo) {
231 for (int i = 0; i < kRepeat; i++) {
237 // This should generate no Google Test failures.
238 void TestPartiallyOrderedExpectationsWithThreads(Dummy /* dummy */) {
244 EXPECT_CALL(foo, Bar(0));
245 EXPECT_CALL(foo, Bar(1))
249 EXPECT_CALL(foo, Bar(2))
252 .RetiresOnSaturation();
253 EXPECT_CALL(foo, Bar(3))
259 EXPECT_CALL(foo, Bar(2))
261 EXPECT_CALL(foo, Bar(4));
267 ThreadWithParam<MockFoo*>* const t =
268 new ThreadWithParam<MockFoo*>(Helper2, &foo, NULL);
276 // Tests using Google Mock constructs in many threads concurrently.
277 TEST(StressTest, CanUseGMockWithThreads) {
278 void (*test_routines[])(Dummy dummy) = {
279 &TestConcurrentCopyAndReadLinkedPtr,
280 &TestConcurrentWriteToEqualLinkedPtr,
281 &TestConcurrentMockObjects,
282 &TestConcurrentCallsOnSameObject,
283 &TestPartiallyOrderedExpectationsWithThreads,
286 const int kRoutines = sizeof(test_routines)/sizeof(test_routines[0]);
287 const int kCopiesOfEachRoutine = kMaxTestThreads / kRoutines;
288 const int kTestThreads = kCopiesOfEachRoutine * kRoutines;
289 ThreadWithParam<Dummy>* threads[kTestThreads] = {};
290 for (int i = 0; i < kTestThreads; i++) {
291 // Creates a thread to run the test function.
293 new ThreadWithParam<Dummy>(test_routines[i % kRoutines], Dummy(), NULL);
294 GTEST_LOG_(INFO) << "Thread #" << i << " running . . .";
297 // At this point, we have many threads running.
298 for (int i = 0; i < kTestThreads; i++) {
299 JoinAndDelete(threads[i]);
302 // Ensures that the correct number of failures have been reported.
303 const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
304 const TestResult& result = *info->result();
305 const int kExpectedFailures = (3*kRepeat + 1)*kCopiesOfEachRoutine;
306 GTEST_CHECK_(kExpectedFailures == result.total_part_count())
307 << "Expected " << kExpectedFailures << " failures, but got "
308 << result.total_part_count();
312 } // namespace testing
314 int main(int argc, char **argv) {
315 testing::InitGoogleMock(&argc, argv);
317 const int exit_code = RUN_ALL_TESTS(); // Expected to fail.
318 GTEST_CHECK_(exit_code != 0) << "RUN_ALL_TESTS() did not fail as expected";