]> git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/thirdparty/gtest/googlemock/test/gmock_stress_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googlemock / test / gmock_stress_test.cc
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 // Author: wan@google.com (Zhanyong Wan)
31
32 // Tests that Google Mock constructs can be used in a large number of
33 // threads concurrently.
34
35 #include "gmock/gmock.h"
36
37 #include "gtest/gtest.h"
38
39 namespace testing {
40 namespace {
41
42 // From "gtest/internal/gtest-port.h".
43 using ::testing::internal::ThreadWithParam;
44
45 // The maximum number of test threads (not including helper threads)
46 // to create.
47 const int kMaxTestThreads = 50;
48
49 // How many times to repeat a task in a test thread.
50 const int kRepeat = 50;
51
52 class MockFoo {
53 public:
54 MOCK_METHOD1(Bar, int(int n)); // NOLINT
55 MOCK_METHOD2(Baz, char(const char* s1, const std::string& s2)); // NOLINT
56 };
57
58 // Helper for waiting for the given thread to finish and then deleting it.
59 template <typename T>
60 void JoinAndDelete(ThreadWithParam<T>* t) {
61 t->Join();
62 delete t;
63 }
64
65 using internal::linked_ptr;
66
67 // Helper classes for testing using linked_ptr concurrently.
68
69 class Base {
70 public:
71 explicit Base(int a_x) : x_(a_x) {}
72 virtual ~Base() {}
73 int x() const { return x_; }
74 private:
75 int x_;
76 };
77
78 class Derived1 : public Base {
79 public:
80 Derived1(int a_x, int a_y) : Base(a_x), y_(a_y) {}
81 int y() const { return y_; }
82 private:
83 int y_;
84 };
85
86 class Derived2 : public Base {
87 public:
88 Derived2(int a_x, int a_z) : Base(a_x), z_(a_z) {}
89 int z() const { return z_; }
90 private:
91 int z_;
92 };
93
94 linked_ptr<Derived1> pointer1(new Derived1(1, 2));
95 linked_ptr<Derived2> pointer2(new Derived2(3, 4));
96
97 struct Dummy {};
98
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
102 // another thread.
103 EXPECT_EQ(1, pointer1->x());
104 EXPECT_EQ(2, pointer1->y());
105 EXPECT_EQ(3, pointer2->x());
106 EXPECT_EQ(4, pointer2->z());
107
108 // Copies from pointer1.
109 linked_ptr<Derived1> p1(pointer1);
110 EXPECT_EQ(1, p1->x());
111 EXPECT_EQ(2, p1->y());
112
113 // Assigns from pointer2 where the LHS was empty.
114 linked_ptr<Base> p2;
115 p2 = pointer1;
116 EXPECT_EQ(1, p2->x());
117
118 // Assigns from pointer2 where the LHS was not empty.
119 p2 = pointer2;
120 EXPECT_EQ(3, p2->x());
121 }
122
123 const linked_ptr<Derived1> p0(new Derived1(1, 2));
124
125 // Tests that we can concurrently modify two linked_ptrs that point to
126 // the same object.
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);
133
134 EXPECT_EQ(1, p1->x());
135 EXPECT_EQ(2, p1->y());
136
137 EXPECT_EQ(1, p2->x());
138 EXPECT_EQ(2, p2->y());
139
140 p1.reset();
141 p2 = p0;
142
143 EXPECT_EQ(1, p2->x());
144 EXPECT_EQ(2, p2->y());
145 }
146
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.
151 MockFoo foo;
152 ON_CALL(foo, Bar(_))
153 .WillByDefault(Return(1));
154 ON_CALL(foo, Baz(_, _))
155 .WillByDefault(Return('b'));
156 ON_CALL(foo, Baz(_, "you"))
157 .WillByDefault(Return('a'));
158
159 EXPECT_CALL(foo, Bar(0))
160 .Times(AtMost(3));
161 EXPECT_CALL(foo, Baz(_, _));
162 EXPECT_CALL(foo, Baz("hi", "you"))
163 .WillOnce(Return('z'))
164 .WillRepeatedly(DoDefault());
165
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"));
171 }
172
173 // Tests invoking methods of the same mock object in multiple threads.
174
175 struct Helper1Param {
176 MockFoo* mock_foo;
177 int* count;
178 };
179
180 void Helper1(Helper1Param param) {
181 for (int i = 0; i < kRepeat; i++) {
182 const char ch = param.mock_foo->Baz("a", "b");
183 if (ch == 'a') {
184 // It was an expected call.
185 (*param.count)++;
186 } else {
187 // It was an excessive call.
188 EXPECT_EQ('\0', ch);
189 }
190
191 // An unexpected call.
192 EXPECT_EQ('\0', param.mock_foo->Baz("x", "y")) << "Expected failure.";
193
194 // An uninteresting call.
195 EXPECT_EQ(1, param.mock_foo->Bar(5));
196 }
197 }
198
199 // This should generate 3*kRepeat + 1 failures in total.
200 void TestConcurrentCallsOnSameObject(Dummy /* dummy */) {
201 MockFoo foo;
202
203 ON_CALL(foo, Bar(_))
204 .WillByDefault(Return(1));
205 EXPECT_CALL(foo, Baz(_, "b"))
206 .Times(kRepeat)
207 .WillRepeatedly(Return('a'));
208 EXPECT_CALL(foo, Baz(_, "c")); // Expected to be unsatisfied.
209
210 // This chunk of code should generate kRepeat failures about
211 // excessive calls, and 2*kRepeat failures about unexpected calls.
212 int count1 = 0;
213 const Helper1Param param = { &foo, &count1 };
214 ThreadWithParam<Helper1Param>* const t =
215 new ThreadWithParam<Helper1Param>(Helper1, param, NULL);
216
217 int count2 = 0;
218 const Helper1Param param2 = { &foo, &count2 };
219 Helper1(param2);
220 JoinAndDelete(t);
221
222 EXPECT_EQ(kRepeat, count1 + count2);
223
224 // foo's destructor should generate one failure about unsatisfied
225 // expectation.
226 }
227
228 // Tests using the same mock object in multiple threads when the
229 // expectations are partially ordered.
230
231 void Helper2(MockFoo* foo) {
232 for (int i = 0; i < kRepeat; i++) {
233 foo->Bar(2);
234 foo->Bar(3);
235 }
236 }
237
238 // This should generate no Google Test failures.
239 void TestPartiallyOrderedExpectationsWithThreads(Dummy /* dummy */) {
240 MockFoo foo;
241 Sequence s1, s2;
242
243 {
244 InSequence dummy;
245 EXPECT_CALL(foo, Bar(0));
246 EXPECT_CALL(foo, Bar(1))
247 .InSequence(s1, s2);
248 }
249
250 EXPECT_CALL(foo, Bar(2))
251 .Times(2*kRepeat)
252 .InSequence(s1)
253 .RetiresOnSaturation();
254 EXPECT_CALL(foo, Bar(3))
255 .Times(2*kRepeat)
256 .InSequence(s2);
257
258 {
259 InSequence dummy;
260 EXPECT_CALL(foo, Bar(2))
261 .InSequence(s1, s2);
262 EXPECT_CALL(foo, Bar(4));
263 }
264
265 foo.Bar(0);
266 foo.Bar(1);
267
268 ThreadWithParam<MockFoo*>* const t =
269 new ThreadWithParam<MockFoo*>(Helper2, &foo, NULL);
270 Helper2(&foo);
271 JoinAndDelete(t);
272
273 foo.Bar(2);
274 foo.Bar(4);
275 }
276
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,
285 };
286
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.
293 threads[i] =
294 new ThreadWithParam<Dummy>(test_routines[i % kRoutines], Dummy(), NULL);
295 GTEST_LOG_(INFO) << "Thread #" << i << " running . . .";
296 }
297
298 // At this point, we have many threads running.
299 for (int i = 0; i < kTestThreads; i++) {
300 JoinAndDelete(threads[i]);
301 }
302
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();
310 }
311
312 } // namespace
313 } // namespace testing
314
315 int main(int argc, char **argv) {
316 testing::InitGoogleMock(&argc, argv);
317
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";
320
321 printf("\nPASS\n");
322 return 0;
323 }