2017-10-10 40 views
0

当使用GTest进行模拟和多线程时,我遇到了竞态条件段错误。我在CentOS 6.9,G ++ 6.3.1上使用GTest 1.8。通过在给定方法的模拟上调用EXPECT_CALL来触发段错误是可能的,而另一个线程在相同的模拟上调用该方法。下面是一个小例子:当使用gtest EXPECT_CALL时竞态条件段错误,而另一个期望正在执行相同的方法

#include <gmock/gmock.h> 
#include <thread> 
#include <atomic> 

class MyMock 
{ 
public: 
    MOCK_METHOD1(myMethod, void(int n)); 
}; 

TEST(MyClassTest, one) 
{ 
    MyMock mock; 
    std::atomic<int> g_arg(5); 
    bool keepRunning = true; 
    bool isRunning = false; 
    bool canEnd = false; 
    EXPECT_CALL(mock, myMethod(5)) 
    .Times(::testing::AtLeast(1)) 
    .WillRepeatedly(::testing::InvokeWithoutArgs([&isRunning](){ isRunning = true; })); 
    std::thread t1([&mock, &keepRunning, &g_arg]() 
    { 
    while (keepRunning) 
    { 
     mock.myMethod(g_arg.load()); 
    } 
    }); 
    while (!isRunning) {} 
    EXPECT_CALL(mock, myMethod(6)) 
    .Times(::testing::AtLeast(1)) 
    .WillRepeatedly(::testing::InvokeWithoutArgs([&canEnd](){ canEnd = true; })); 
    g_arg.store(6); 
    while (!canEnd) {} 
    keepRunning = false; 
    t1.join(); 
} 

它可以被编译: g++ -lpthread -lgmock_main -g Gtest_test.cpp -o Gtest_test

有时,问题会立即发生,有时你做了几百运行没有问题。我一直在运行这样的: gdb --args ./Gtest_test --gtest_repeat=-1 --gtest_break_on_failure

下面是GDB,当段错误发生一些输出:

Repeating all tests (iteration 10) . . . 

[==========] Running 1 test from 1 test case. 
[----------] Global test environment set-up. 
[----------] 1 test from MyClassTest 
[ RUN  ] MyClassTest.one 
[New Thread 0x7ffff6d8b700 (LWP 14092)] 

Thread 11 "Gtest_test" received signal SIGSEGV, Segmentation fault. 
[Switching to Thread 0x7ffff6d8b700 (LWP 14092)] 
0x00007ffff7bc8470 in pthread_mutex_lock() from /lib64/libpthread.so.0 
Missing separate debuginfos, use: debuginfo-install libgcc-4.4.7-18.el6.x86_64 libstdc++-4.4.7-18.el6.x86_64 
(gdb) bt 
#0 0x00007ffff7bc8470 in pthread_mutex_lock() from /lib64/libpthread.so.0 
#1 0x0000000000407b5b in testing::internal::MutexBase::Lock (this=0x98) at /usr/include/gtest/internal/gtest-port.h:1928 
#2 0x0000000000407da8 in testing::internal::GTestMutexLock::GTestMutexLock (this=0x7ffff6d8a540, mutex=0x98) at /usr/include/gtest/internal/gtest-port.h:1999 
#3 0x00007ffff7979628 in testing::internal::ExpectationBase::CheckActionCountIfNotDone() const() from /usr/lib64/libgmock_main.so 
#4 0x000000000040ce9c in testing::internal::TypedExpectation<void (int)>::ShouldHandleArguments(std::tuple<int> const&) const (this=0x0, args=std::tuple containing = {...}) at /usr/include/gmock/gmock-spec-builders.h:1100 
#5 0x000000000040c91a in testing::internal::FunctionMockerBase<void (int)>::FindMatchingExpectationLocked(std::tuple<int> const&) const (this=0x7fffffffd850, args=std::tuple containing = {...}) 
    at /usr/include/gmock/gmock-spec-builders.h:1723 
#6 0x000000000040c569 in testing::internal::FunctionMockerBase<void (int)>::UntypedFindMatchingExpectation(void const*, void const**, bool*, std::ostream*, std::ostream*) (this=0x7fffffffd850, untyped_args=0x7ffff6d8ad90, 
    untyped_action=0x7ffff6d8a7e8, is_excessive=0x7ffff6d8ac4f, what=0x7ffff6d8aae0, why=0x7ffff6d8a970) at /usr/include/gmock/gmock-spec-builders.h:1687 
#7 0x00007ffff797a212 in testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)() from /usr/lib64/libgmock_main.so 
#8 0x00000000004096cd in testing::internal::FunctionMockerBase<void (int)>::InvokeWith(std::tuple<int> const&) (this=0x7fffffffd850, args=std::tuple containing = {...}) at /usr/include/gmock/gmock-spec-builders.h:1585 
#9 0x0000000000408ba1 in testing::internal::FunctionMocker<void (int)>::Invoke(int) (this=0x7fffffffd850, a1=5) at /usr/include/gmock/gmock-generated-function-mockers.h:101 
#10 0x00000000004087a8 in MyMock::myMethod (this=0x7fffffffd850, gmock_a1=5) at Gtest_test.cpp:11 
#11 0x0000000000406e25 in MyClassTest_one_Test::<lambda()>::operator()(void) const (__closure=0x618d48) at Gtest_test.cpp:28 
#12 0x0000000000407a86 in std::_Bind_simple<MyClassTest_one_Test::TestBody()::<lambda()>()>::_M_invoke<>(std::_Index_tuple<>) (this=0x618d48) at /opt/rh/devtoolset-6/root/usr/include/c++/6.3.1/functional:1391 
#13 0x00000000004079e7 in std::_Bind_simple<MyClassTest_one_Test::TestBody()::<lambda()>()>::operator()(void) (this=0x618d48) at /opt/rh/devtoolset-6/root/usr/include/c++/6.3.1/functional:1380 
#14 0x0000000000407972 in std::thread::_State_impl<std::_Bind_simple<MyClassTest_one_Test::TestBody()::<lambda()>()> >::_M_run(void) (this=0x618d40) at /opt/rh/devtoolset-6/root/usr/include/c++/6.3.1/thread:197 
#15 0x000000000040e33f in execute_native_thread_routine() 
#16 0x00007ffff7bc6aa1 in start_thread() from /lib64/libpthread.so.0 
#17 0x00007ffff6e74bcd in clone() from /lib64/libc.so.6 
(gdb) info threads 
    Id Target Id   Frame 
    1 Thread 0x7ffff7fdc720 (LWP 14079) "Gtest_test" MyClassTest_one_Test::TestBody (this=0x618510) at Gtest_test.cpp:36 
* 11 Thread 0x7ffff6d8b700 (LWP 14092) "Gtest_test" 0x00007ffff7bc8470 in pthread_mutex_lock() from /lib64/libpthread.so.0 
(gdb) t 1 
[Switching to thread 1 (Thread 0x7ffff7fdc720 (LWP 14079))] 
#0 MyClassTest_one_Test::TestBody (this=0x618510) at Gtest_test.cpp:36 
36 while (!canEnd) {} 
(gdb) bt 
#0 MyClassTest_one_Test::TestBody (this=0x618510) at Gtest_test.cpp:36 
#1 0x00007ffff7968edf in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)() from /usr/lib64/libgmock_main.so 
#2 0x00007ffff7962cd4 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)() from /usr/lib64/libgmock_main.so 
#3 0x00007ffff7947a7c in testing::Test::Run()() from /usr/lib64/libgmock_main.so 
#4 0x00007ffff7948326 in testing::TestInfo::Run()() from /usr/lib64/libgmock_main.so 
#5 0x00007ffff79489ff in testing::TestCase::Run()() from /usr/lib64/libgmock_main.so 
#6 0x00007ffff794f5c9 in testing::internal::UnitTestImpl::RunAllTests()() from /usr/lib64/libgmock_main.so 
#7 0x00007ffff796a2f2 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)() 
    from /usr/lib64/libgmock_main.so 
#8 0x00007ffff7963a48 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)() 
    from /usr/lib64/libgmock_main.so 
#9 0x00007ffff794e17f in testing::UnitTest::Run()() from /usr/lib64/libgmock_main.so 
#10 0x00007ffff798702e in RUN_ALL_TESTS()() from /usr/lib64/libgmock_main.so 
#11 0x00007ffff7986fbd in main() from /usr/lib64/libgmock_main.so 
#12 0x00007ffff6daad1d in __libc_start_main() from /lib64/libc.so.6 
#13 0x0000000000406cc9 in _start() 

我知道,我可以通过前加入我的EXPECT_CALLs解决问题我开始线程,但有其他人遇到它?它是一个真正的bug,并且有修复吗?

回答

2

谷歌模拟docs明确禁止交织设定期望值,并呼吁嘲笑:

重要提示:谷歌模拟需要的期望被称为 模拟功能之前设置,否则行为是不确定的。特别是,在 中,您不得交错EXPECT_CALL()和调用 模拟函数。

+0

谢谢,没有发现,在文档中 – Obiphil

相关问题