35 #include "gmock/gmock.h" 36 #include "gtest/gtest.h" 42 using ::testing::internal::ThreadWithParam;
46 const int kMaxTestThreads = 50;
49 const int kRepeat = 50;
59 void JoinAndDelete(ThreadWithParam<T>* t) {
64 using internal::linked_ptr;
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));
99 void TestConcurrentCopyAndReadLinkedPtr(
Dummy ) {
108 linked_ptr<Derived1> p1(pointer1);
122 const linked_ptr<Derived1> p0(
new Derived1(1, 2));
126 void TestConcurrentWriteToEqualLinkedPtr(
Dummy ) {
130 linked_ptr<Derived1> p1(p0);
131 linked_ptr<Derived1> p2(p0);
148 void TestConcurrentMockObjects(
Dummy ) {
152 .WillByDefault(
Return(1));
154 .WillByDefault(
Return(
'b'));
156 .WillByDefault(
Return(
'a'));
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");
191 EXPECT_EQ(
'\0', param.mock_foo->Baz(
"x",
"y")) <<
"Expected failure.";
199 void TestConcurrentCallsOnSameObject(
Dummy ) {
203 .WillByDefault(
Return(1));
206 .WillRepeatedly(
Return(
'a'));
212 const Helper1Param
param = { &
foo, &count1 };
213 ThreadWithParam<Helper1Param>*
const t =
214 new ThreadWithParam<Helper1Param>(Helper1, param, NULL);
217 const Helper1Param param2 = { &
foo, &count2 };
230 void Helper2(MockFoo* foo) {
231 for (
int i = 0; i < kRepeat; i++) {
238 void TestPartiallyOrderedExpectationsWithThreads(
Dummy ) {
252 .RetiresOnSaturation();
267 ThreadWithParam<MockFoo*>*
const t =
268 new ThreadWithParam<MockFoo*>(Helper2, &
foo, NULL);
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++) {
293 new ThreadWithParam<Dummy>(test_routines[i % kRoutines],
Dummy(), NULL);
294 GTEST_LOG_(INFO) <<
"Thread #" << i <<
" running . . .";
298 for (
int i = 0; i < kTestThreads; i++) {
299 JoinAndDelete(threads[i]);
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();
314 int main(
int argc,
char **argv) {
318 GTEST_CHECK_(exit_code != 0) <<
"RUN_ALL_TESTS() did not fail as expected";
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
GTEST_API_ Cardinality AtMost(int n)
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
#define ON_CALL(obj, call)
#define MOCK_METHOD2(m,...)
TEST(GTestEnvVarTest, Dummy)
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
#define MOCK_METHOD1(m,...)
internal::DoDefaultAction DoDefault()
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
const TestResult * result() const
static UnitTest * GetInstance()
int main(int argc, char **argv)
#define GTEST_CHECK_(condition)
#define EXPECT_EQ(expected, actual)
#define GTEST_LOG_(severity)
internal::ReturnAction< R > Return(R value)
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)