Search code examples
deadlockgoogletestgooglemock

gtest hangs during deconstructing fixture when a mock returns a mock as default value


I have found a strange behavior I can't understand nor resolve. I have a factory FooFactory, which delivers some real-live objects of type Foo. To test method calls of Fooobjects I mocked FooFactory, in that way that MockFooFactory returns MockFooobjects for which I can expect calls.

The tests (not included) are working fine, but after the test gmock/gtest hangs (seems like mutex problem) during the deconstruction of MockFooFactory. To be precise the deletion of the Default ON_CALL leads to problems when the Mutex is created.

There must an issue with the smart pointers, when I build a version without smart pointers, it works fine. But the software I test uses shared_ptr as smart pointers, and so I can't get rid of them.

Here is a sample I build which reproduces the error:

#include <boost/shared_ptr.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

class Foo
{
public:
    void doSomething() {}
};
typedef boost::shared_ptr<Foo> FooPtr;

class FooFactory {
public:
    FooPtr create() {
        return FooPtr(new Foo());
    }
};
typedef boost::shared_ptr<FooFactory> FooFactoryPtr;

class MockFoo : public Foo {
public:
    MOCK_METHOD0(doSomething, void());
};
typedef boost::shared_ptr<MockFoo> MockFooPtr;

class MockFactory : public FooFactory
{
public:
    MOCK_METHOD0(create, FooPtr());
};
typedef boost::shared_ptr<MockFactory> MockFactoryPtr;

using ::testing::Return;
class Fixture : public ::testing::Test {
protected:

    virtual void SetUp() {
        mockFoo = MockFooPtr(new MockFoo());
        mockFactory = MockFactoryPtr(new MockFactory());

        ON_CALL(*mockFactory, create()).WillByDefault(Return(mockFoo));
    }

    MockFactoryPtr mockFactory;
    MockFooPtr mockFoo;
};

TEST_F(Fixture, Test)
{
    /* Not needed */
}

Has anyone experienced the same problem or has a solution for it?


Solution

  • Okay after updating to the trunk version of gmock everything is fine, because they fixed this issue:

    https://code.google.com/p/googlemock/issues/detail?id=79