1 | // Copyright 2007, Google Inc. |
2 | // All rights reserved. |
3 | // |
4 | // Redistribution and use in source and binary forms, with or without |
5 | // modification, are permitted provided that the following conditions are |
6 | // met: |
7 | // |
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 |
13 | // distribution. |
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. |
17 | // |
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. |
29 | |
30 | |
31 | // Tests that Google Mock constructs can be used in a large number of |
32 | // threads concurrently. |
33 | |
34 | #include "gmock/gmock.h" |
35 | #include "gtest/gtest.h" |
36 | |
37 | namespace testing { |
38 | namespace { |
39 | |
40 | // From gtest-port.h. |
41 | using ::testing::internal::ThreadWithParam; |
42 | |
43 | // The maximum number of test threads (not including helper threads) |
44 | // to create. |
45 | const int kMaxTestThreads = 50; |
46 | |
47 | // How many times to repeat a task in a test thread. |
48 | const int kRepeat = 50; |
49 | |
50 | class MockFoo { |
51 | public: |
52 | MOCK_METHOD1(Bar, int(int n)); // NOLINT |
53 | MOCK_METHOD2(Baz, char(const char* s1, const std::string& s2)); // NOLINT |
54 | }; |
55 | |
56 | // Helper for waiting for the given thread to finish and then deleting it. |
57 | template <typename T> |
58 | void JoinAndDelete(ThreadWithParam<T>* t) { |
59 | t->Join(); |
60 | delete t; |
61 | } |
62 | |
63 | struct Dummy {}; |
64 | |
65 | |
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. |
70 | MockFoo foo; |
71 | ON_CALL(foo, Bar(_)) |
72 | .WillByDefault(Return(1)); |
73 | ON_CALL(foo, Baz(_, _)) |
74 | .WillByDefault(Return('b')); |
75 | ON_CALL(foo, Baz(_, "you" )) |
76 | .WillByDefault(Return('a')); |
77 | |
78 | EXPECT_CALL(foo, Bar(0)) |
79 | .Times(AtMost(3)); |
80 | EXPECT_CALL(foo, Baz(_, _)); |
81 | EXPECT_CALL(foo, Baz("hi" , "you" )) |
82 | .WillOnce(Return('z')) |
83 | .WillRepeatedly(DoDefault()); |
84 | |
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" )); |
90 | } |
91 | |
92 | // Tests invoking methods of the same mock object in multiple threads. |
93 | |
94 | struct Helper1Param { |
95 | MockFoo* mock_foo; |
96 | int* count; |
97 | }; |
98 | |
99 | void Helper1(Helper1Param param) { |
100 | for (int i = 0; i < kRepeat; i++) { |
101 | const char ch = param.mock_foo->Baz("a" , "b" ); |
102 | if (ch == 'a') { |
103 | // It was an expected call. |
104 | (*param.count)++; |
105 | } else { |
106 | // It was an excessive call. |
107 | EXPECT_EQ('\0', ch); |
108 | } |
109 | |
110 | // An unexpected call. |
111 | EXPECT_EQ('\0', param.mock_foo->Baz("x" , "y" )) << "Expected failure." ; |
112 | |
113 | // An uninteresting call. |
114 | EXPECT_EQ(1, param.mock_foo->Bar(5)); |
115 | } |
116 | } |
117 | |
118 | // This should generate 3*kRepeat + 1 failures in total. |
119 | void TestConcurrentCallsOnSameObject(Dummy /* dummy */) { |
120 | MockFoo foo; |
121 | |
122 | ON_CALL(foo, Bar(_)) |
123 | .WillByDefault(Return(1)); |
124 | EXPECT_CALL(foo, Baz(_, "b" )) |
125 | .Times(kRepeat) |
126 | .WillRepeatedly(Return('a')); |
127 | EXPECT_CALL(foo, Baz(_, "c" )); // Expected to be unsatisfied. |
128 | |
129 | // This chunk of code should generate kRepeat failures about |
130 | // excessive calls, and 2*kRepeat failures about unexpected calls. |
131 | int count1 = 0; |
132 | const Helper1Param param = { &foo, &count1 }; |
133 | ThreadWithParam<Helper1Param>* const t = |
134 | new ThreadWithParam<Helper1Param>(Helper1, param, nullptr); |
135 | |
136 | int count2 = 0; |
137 | const Helper1Param param2 = { &foo, &count2 }; |
138 | Helper1(param2); |
139 | JoinAndDelete(t); |
140 | |
141 | EXPECT_EQ(kRepeat, count1 + count2); |
142 | |
143 | // foo's destructor should generate one failure about unsatisfied |
144 | // expectation. |
145 | } |
146 | |
147 | // Tests using the same mock object in multiple threads when the |
148 | // expectations are partially ordered. |
149 | |
150 | void Helper2(MockFoo* foo) { |
151 | for (int i = 0; i < kRepeat; i++) { |
152 | foo->Bar(2); |
153 | foo->Bar(3); |
154 | } |
155 | } |
156 | |
157 | // This should generate no Google Test failures. |
158 | void TestPartiallyOrderedExpectationsWithThreads(Dummy /* dummy */) { |
159 | MockFoo foo; |
160 | Sequence s1, s2; |
161 | |
162 | { |
163 | InSequence dummy; |
164 | EXPECT_CALL(foo, Bar(0)); |
165 | EXPECT_CALL(foo, Bar(1)) |
166 | .InSequence(s1, s2); |
167 | } |
168 | |
169 | EXPECT_CALL(foo, Bar(2)) |
170 | .Times(2*kRepeat) |
171 | .InSequence(s1) |
172 | .RetiresOnSaturation(); |
173 | EXPECT_CALL(foo, Bar(3)) |
174 | .Times(2*kRepeat) |
175 | .InSequence(s2); |
176 | |
177 | { |
178 | InSequence dummy; |
179 | EXPECT_CALL(foo, Bar(2)) |
180 | .InSequence(s1, s2); |
181 | EXPECT_CALL(foo, Bar(4)); |
182 | } |
183 | |
184 | foo.Bar(0); |
185 | foo.Bar(1); |
186 | |
187 | ThreadWithParam<MockFoo*>* const t = |
188 | new ThreadWithParam<MockFoo*>(Helper2, &foo, nullptr); |
189 | Helper2(&foo); |
190 | JoinAndDelete(t); |
191 | |
192 | foo.Bar(2); |
193 | foo.Bar(4); |
194 | } |
195 | |
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, |
202 | }; |
203 | |
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], |
211 | Dummy(), nullptr); |
212 | GTEST_LOG_(INFO) << "Thread #" << i << " running . . ." ; |
213 | } |
214 | |
215 | // At this point, we have many threads running. |
216 | for (int i = 0; i < kTestThreads; i++) { |
217 | JoinAndDelete(threads[i]); |
218 | } |
219 | |
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(); |
227 | } |
228 | |
229 | } // namespace |
230 | } // namespace testing |
231 | |
232 | int main(int argc, char **argv) { |
233 | testing::InitGoogleMock(&argc, argv); |
234 | |
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" ; |
237 | |
238 | printf("\nPASS\n" ); |
239 | return 0; |
240 | } |
241 | |