Rob
unread,Apr 19, 2011, 1:50:47 PM4/19/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google C++ Mocking Framework
Hi,
I am using the latest gmock 1.6.0 release.
I have an example test that uses two mock objects and
boost::shared_ptr's.
When I run the test under MS Visual Studio 2010 it works fine.
When I run it on Fedora 14 with g++ (GCC) 4.5.1 20100924 (Red Hat
4.5.1-4) using pthreads it locks up on a call to
pthread_mutex_lock(). I believe the problem is that the pthread mutex
is not recursive and should be defined as type PTHREAD_MUTEX_RECURSIVE
in the mutex's attributes.
In my example I have two tests. pthreadTest1 and pthreadTest2.
pthreadTest1 runs fine.
pthreadTest2 locks up on the pthread_mutex_lock() when the test is
being destroyed.
The only difference between the two is that in pthreadTest1 the
_mockContainer is being allocated in the constructor of pthreadTest1
and deleted in the destructor and in pthreadTest2 it is just a member
variable.
Does the pthread mutex in gmock need to be defined as type
PTHREAD_MUTEX_RECURSIVE or am I missing something?
Below is my test code and stack trace.
Thanks,
Rob
TEST OUTPUT:
Running main() from gmock_main.cc
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from pthreadTest1
[ RUN ] pthreadTest1.createConfig1
[ OK ] pthreadTest1.createConfig1 (1 ms)
[----------] 1 test from pthreadTest1 (1 ms total)
[----------] 1 test from pthreadTest2
[ RUN ] pthreadTest2.createConfig2
CODE:
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <boost/shared_ptr.hpp>
#include <string>
using ::testing::Return;
using ::testing::StrEq;
using std::string;
using boost::shared_ptr;
class Item
{
public:
virtual string getName()=0;
};
class Container
{
public:
virtual boost::shared_ptr<Item> getItem(const string& name)=0;
};
class MockItem : public Item
{
public:
MOCK_METHOD0(getName, string ());
};
class MockContainer : public Container
{
public:
MOCK_METHOD1(getItem, boost::shared_ptr<Item>(const string& name));
};
class ItemGroup
{
// The resource tree to use for resolving property weak references
Container *_container;
public:
ItemGroup(Container *container) :
_container(container)
{
}
void createItemGroup(string& name)
{
// Resolve Item
_container->getItem(name);
}
};
/***************************************** pthreadTest1
*********************************************************
* Works fine.
*/
class pthreadTest1 : public ::testing::Test
{
public:
// Create some mock objects and data values
MockContainer *_mockContainer;
MockItem *_mockItem1;
boost::shared_ptr<Item> _mockItem1Ptr;
pthreadTest1()
{
_mockContainer = new MockContainer();
}
~pthreadTest1()
{
delete _mockContainer;
}
void using1()
{
_mockItem1 = new MockItem();
_mockItem1Ptr = boost::shared_ptr<Item>(_mockItem1);
EXPECT_CALL(*_mockContainer, getItem(StrEq("Item1")))
.Times(1)
.WillOnce(Return(_mockItem1Ptr));
}
};
TEST_F(pthreadTest1, createConfig1)
{
using1();
string item1 = "Item1";
boost::shared_ptr<ItemGroup> group(new ItemGroup(_mockContainer) );
group->createItemGroup(item1);
}
/***************************************** pthreadTest2
*********************************************************
* Locks Up.
*/
class pthreadTest2 : public ::testing::Test
{
public:
// Create some mock objects and data values
MockContainer _mockContainer;
MockItem *_mockItem1;
boost::shared_ptr<Item> _mockItem1Ptr;
void using1()
{
_mockItem1 = new MockItem();
_mockItem1Ptr = boost::shared_ptr<Item>(_mockItem1);
EXPECT_CALL(_mockContainer, getItem(StrEq("Item1")))
.Times(1)
.WillOnce(Return(_mockItem1Ptr));
}
};
TEST_F(pthreadTest2, createConfig2)
{
using1();
string item1 = "Item1";
boost::shared_ptr<ItemGroup> group(new
ItemGroup(&_mockContainer) );
group->createItemGroup(item1);
}
STACK TRACE:
__kernel_vsyscall() at 0x110416
__lll_lock_wait() at 0xb62072
_L_lock_684() at 0xb5d0b0
pthread_mutex_lock() at 0xb5cf81
testing::internal::MutexBase::Lock() at gtest-port.h:1,208 0x804b568
testing::internal::GTestMutexLock::GTestMutexLock() at gtest-port.h:
1,268 0x804b781
testing::internal::FunctionMockerBase<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >() at gmock-spec-
builders.h:1,398 0x804d2d3
testing::internal::FunctionMocker<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >() at gmock-spec-
builders.h:89 0x804c58e
MockItem::~MockItem() at pthreadTest.cpp:25 0x804ea3b
boost::checked_delete<MockItem>() at checked_delete.hpp:34 0x804ea53
boost::detail::sp_counted_impl_p<MockItem>::dispose() at
sp_counted_impl.hpp:78 0x80509ee
boost::detail::sp_counted_base::release() at
sp_counted_base_gcc_x86.hpp:145 0x804c11e
boost::detail::shared_count::~shared_count() at shared_count.hpp:217
0x804c190
boost::shared_ptr<Item>::~shared_ptr() at shared_ptr.hpp:169
0x804c240
Impl<boost::shared_ptr<Item>() at gmock-actions.h:478 0x805063a
Impl<boost::shared_ptr<Item>() at gmock-actions.h:478 0x805068d
testing::internal::linked_ptr<testing::ActionInterface<boost::shared_ptr<Item>()
at gtest-linked_ptr.h:195 0x804e3a5
testing::internal::linked_ptr<testing::ActionInterface<boost::shared_ptr<Item>()
at gtest-linked_ptr.h:143 0x804d7cd
testing::Action<boost::shared_ptr<Item>() at gmock-actions.h:278
0x804c667
testing::internal::TypedExpectation<boost::shared_ptr<Item>() at gmock-
spec-builders.h:889 0x8050c0a
testing::internal::TypedExpectation<boost::shared_ptr<Item>() at gmock-
spec-builders.h:891 0x8050cfd
testing::internal::linked_ptr<testing::internal::ExpectationBase>::depart()
at gtest-linked_ptr.h:195 0x804dbfb
testing::internal::linked_ptr<testing::internal::ExpectationBase>::~linked_ptr()
at gtest-linked_ptr.h:143 0x804cf99
std::_Destroy<testing::internal::linked_ptr<testing::internal::ExpectationBase>
>() at stl_construct.h:89 0x8050376
std::_Destroy_aux<false>::__destroy<testing::internal::linked_ptr<testing::internal::ExpectationBase>*>()
at stl_construct.h:99 0x805020c
std::_Destroy<testing::internal::linked_ptr<testing::internal::ExpectationBase>*>()
at stl_construct.h:122 0x804ffbd
std::_Destroy<testing::internal::linked_ptr<testing::internal::ExpectationBase>*,
testing::internal::linked_ptr<testing::internal::ExpectationBase> >()
at stl_construct.h:148 0x804fc09
std::vector<testing::internal::linked_ptr<testing::internal::ExpectationBase>,
std::allocator<testing::internal::linked_ptr<testing::internal::ExpectationBase>
> >::_M_erase_at_end() at 0x807c3ca
std::vector<testing::internal::linked_ptr<testing::internal::ExpectationBase>,
std::allocator<testing::internal::linked_ptr<testing::internal::ExpectationBase>
> >::clear() at 0x807b7f0
testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
at 0x8078ae1
testing::internal::FunctionMockerBase<boost::shared_ptr<Item>() at
gmock-spec-builders.h:1,399 0x804d1d2
testing::internal::FunctionMocker<boost::shared_ptr<Item>() at gmock-
spec-builders.h:89 0x804c37c
MockContainer::~MockContainer() at pthreadTest.cpp:31 0x804c49d
pthreadTest2::~pthreadTest2() at pthreadTest.cpp:108 0x804cbec
pthreadTest2_createConfig2_Test::~pthreadTest2_createConfig2_Test() at
pthreadTest.cpp:128 0x8050732
pthreadTest2_createConfig2_Test::~pthreadTest2_createConfig2_Test() at
pthreadTest.cpp:128 0x805075d
testing::Test::DeleteSelf_() at 0x80657de
void
testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test,
void>() at 0x806dfcf
void
testing::internal::HandleExceptionsInMethodIfSupported<testing::Test,
void>() at 0x8069833
testing::TestInfo::Run() at 0x8058f73
testing::TestCase::Run() at 0x8059437
testing::internal::UnitTestImpl::RunAllTests() at 0x805d960
bool
testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl,
bool>() at 0x806f175
bool
testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl,
bool>() at 0x806a58d
testing::UnitTest::Run() at 0x805c91d
main() at 0x8054138