我开始在Windows上测试GoogleMock(1.8.0版)。我想举一个例子,它不是线程安全的。成功证明这一点后,我想证明同一测试可以在Linux上正常运行。但是,那失败了。那与我的期望不符。
由于GoogleMock文档说在具有pthread的系统上它应该是或应该是线程安全的,因此在Linux上它应该是线程安全的。我确实必须在链接器命令行中添加-pthread来生成可执行文件。这意味着GoogleMock或GoogleTest确实使用了pthreads。

这是我用于测试的代码:

#include <thread>
#include <vector>

#include "gmock/gmock.h"

class Dummy
{
public:
    virtual void SomeMethod(int) {}
};

class DummyMock : public Dummy
{
public:
    MOCK_METHOD1(SomeMethod, void(int));
};

using ::testing::Exactly;

constexpr static int nrCallsPerThread = 100 * 1000;
constexpr static int nrThreads = 10;

TEST(SomeTest, Test100)
{
    DummyMock dummy;

    std::vector<std::thread> threads;
    for (int i = 0; i < nrThreads; i++)
    {
        EXPECT_CALL(dummy, SomeMethod(i)).Times(Exactly(nrCallsPerThread));
        threads.emplace_back([&dummy, i]
        {
            for (int j = 0; j < nrCallsPerThread; j++)
            {
                dummy.SomeMethod(i);
            }
        });
    }

    for (auto& t: threads)
    {
        t.join();
    }
}

int main(int argc, char** argv)
{
    testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

问题是,在Linux上,没有公开每次执行。但是,使用--gtest_repeat=100运行可执行文件的命中率接近100%。

在Windows上,如果使用Debug Assertion Failed!获得Expression: vector iterator not decrementable消息,则使用Visual Studio 2015。

在Linux上,Ubuntu 17.04,当我从命令行运行Debug构建时,我得到[ FATAL ] ../googletest-release-1.8.0/googletest/include/gtest/internal/gtest-port.h:1928:: pthread_mutex_lock(&mutex_)failed with error 22

在Linux上的调试器中运行时,该程序(经常)在gtest-port.h第1100行(此调用堆栈的第2行)处中断:
0 0x5555555a6633 testing::Cardinality::ConservativeUpperBound() const () (??:??)
1 0x5555555a1a13 testing::internal::ExpectationBase::CheckActionCountIfNotDone() const () (??:??)
2 0x555555563f98 testing::internal::TypedExpectation<void (int)>::ShouldHandleArguments(std::tuple<int> const&) const(this=0x5555557f58a0, args=std::tuple containing = {...}) (../googletest-release-1.8.0/googlemock/include/gmock/gmock-spec-builders.h:1100)
3 0x55555556397d testing::internal::FunctionMockerBase<void (int)>::FindMatchingExpectationLocked(std::tuple<int> const&) const(this=0x7fffffffde38, args=std::tuple containing = {...}) (../googletest-release-1.8.0/googlemock/include/gmock/gmock-spec-builders.h:1723)
4 0x555555563578 testing::internal::FunctionMockerBase<void (int)>::UntypedFindMatchingExpectation(void const*, void const**, bool*, std::ostream*, std::ostream*)(this=0x7fffffffde38, untyped_args=0x7fffde7fbe14, untyped_action=0x7fffde7fb7d0, is_excessive=0x7fffde7fb7c7, what=0x7fffde7fb900, why=0x7fffde7fba90) (../googletest-release-1.8.0/googlemock/include/gmock/gmock-spec-builders.h:1687)
5 0x5555555a265e testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*) () (??:??)
6 0x55555555fcba testing::internal::FunctionMockerBase<void (int)>::InvokeWith(std::tuple<int> const&)(this=0x7fffffffde38, args=std::tuple containing = {...}) (../googletest-release-1.8.0/googlemock/include/gmock/gmock-spec-builders.h:1585)
7 0x55555555f16c testing::internal::FunctionMocker<void (int)>::Invoke(int)(this=0x7fffffffde38, a1=1) (../googletest-release-1.8.0/googlemock/include/gmock/gmock-generated-function-mockers.h:101)
8 0x55555555ecb6 DummyMock::SomeMethod(this=0x7fffffffde30, gmock_a1=1) (/home/jos/Programming/ThreadSafeGMock/main.cpp:16)
9 0x55555555d31e SomeTest_Test100_Test::<lambda()>::operator()(void) const(__closure=0x5555557f5478) (/home/jos/Programming/ThreadSafeGMock/main.cpp:36)
10 0x55555555de98 std::_Bind_simple<SomeTest_Test100_Test::TestBody()::<lambda()>()>::_M_invoke<>(std::_Index_tuple<>)(this=0x5555557f5478) (/usr/include/c++/6/functional:1391)
11 0x55555555de22 std::_Bind_simple<SomeTest_Test100_Test::TestBody()::<lambda()>()>::operator()(void)(this=0x5555557f5478) (/usr/include/c++/6/functional:1380)
12 0x55555555ddf2 std::thread::_State_impl<std::_Bind_simple<SomeTest_Test100_Test::TestBody()::<lambda()>()> >::_M_run(void)(this=0x5555557f5470) (/usr/include/c++/6/thread:197)
13 0x7ffff7b0a83f ??() (/usr/lib/x86_64-linux-gnu/libstdc++.so.6:??)
14 0x7ffff76216da start_thread(arg=0x7fffde7fc700) (pthread_create.c:456)
15 0x7ffff735b17f clone() (../sysdeps/unix/sysv/linux/x86_64/clone.S:105)

由于它应该是线程安全的,因此我怀疑自己做错了什么。但是我看不到。
还是我遇到了GoogleTest或GoogleMock中的错误?

最佳答案

从精美的手册中:



您的原始代码在我的系统(cygwin)上间歇性地失败,错误22或有时没有消息/错误代码。此修改可以完美地工作:

for (int i = 0; i < nrThreads; i++)
{
    EXPECT_CALL(dummy, SomeMethod(i)).Times(Exactly(nrCallsPerThread));
}

std::vector<std::thread> threads;
for (int i = 0; i < nrThreads; i++)
{
    threads.emplace_back([&dummy, i] ...

关于c++ - GoogleMock在Linux上不是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44750012/

10-10 21:27