43using ::testing::internal::ThreadWithParam;
47const int kMaxTestThreads = 50;
50const int kRepeat = 50;
55 MOCK_METHOD2(Baz,
char(
const char* s1,
const std::string& s2));
60void JoinAndDelete(ThreadWithParam<T>* t) {
65using internal::linked_ptr;
71 explicit Base(
int a_x) : x_(a_x) {}
73 int x()
const {
return x_; }
78class Derived1 :
public Base {
80 Derived1(
int a_x,
int a_y) : Base(a_x), y_(a_y) {}
81 int y()
const {
return y_; }
86class Derived2 :
public Base {
88 Derived2(
int a_x,
int a_z) : Base(a_x), z_(a_z) {}
89 int z()
const {
return z_; }
94linked_ptr<Derived1> pointer1(
new Derived1(1, 2));
95linked_ptr<Derived2> pointer2(
new Derived2(3, 4));
100void TestConcurrentCopyAndReadLinkedPtr(Dummy ) {
109 linked_ptr<Derived1> p1(pointer1);
123const linked_ptr<Derived1> p0(
new Derived1(1, 2));
127void TestConcurrentWriteToEqualLinkedPtr(Dummy ) {
131 linked_ptr<Derived1> p1(p0);
132 linked_ptr<Derived1> p2(p0);
149void TestConcurrentMockObjects(Dummy ) {
153 .WillByDefault(
Return(1));
155 .WillByDefault(
Return(
'b'));
157 .WillByDefault(
Return(
'a'));
180void Helper1(Helper1Param param) {
181 for (
int i = 0; i < kRepeat; i++) {
182 const char ch = param.mock_foo->Baz(
"a",
"b");
192 EXPECT_EQ(
'\0', param.mock_foo->Baz(
"x",
"y")) <<
"Expected failure.";
200void TestConcurrentCallsOnSameObject(Dummy ) {
204 .WillByDefault(
Return(1));
207 .WillRepeatedly(
Return(
'a'));
213 const Helper1Param param = { &
foo, &count1 };
214 ThreadWithParam<Helper1Param>*
const t =
215 new ThreadWithParam<Helper1Param>(Helper1, param, NULL);
218 const Helper1Param param2 = { &
foo, &count2 };
231void Helper2(MockFoo*
foo) {
232 for (
int i = 0; i < kRepeat; i++) {
239void TestPartiallyOrderedExpectationsWithThreads(Dummy ) {
253 .RetiresOnSaturation();
268 ThreadWithParam<MockFoo*>*
const t =
269 new ThreadWithParam<MockFoo*>(Helper2, &
foo, NULL);
278TEST(StressTest, CanUseGMockWithThreads) {
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++) {
294 new ThreadWithParam<Dummy>(test_routines[i % kRoutines],
Dummy(), NULL);
299 for (
int i = 0; i < kTestThreads; i++) {
300 JoinAndDelete(threads[i]);
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();
319 GTEST_CHECK_(exit_code != 0) <<
"RUN_ALL_TESTS() did not fail as expected";
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
static UnitTest * GetInstance()
#define MOCK_METHOD2(m,...)
#define MOCK_METHOD1(m,...)
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define GTEST_LOG_(severity)
#define GTEST_CHECK_(condition)
#define EXPECT_EQ(val1, val2)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define TEST(test_case_name, test_name)
constexpr enabler dummy
An instance to use in EnableIf.
static const Reg8 ch(Operand::CH)
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
PolymorphicAction< internal::ReturnVoidAction > Return()
GTEST_API_ Cardinality AtMost(int n)
internal::DoDefaultAction DoDefault()