首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gtest EXPECT_CALL时的竞赛条件分段故障,而另一种期望是使用相同的方法

使用gtest EXPECT_CALL时的竞赛条件分段故障,而另一种期望是使用相同的方法
EN

Stack Overflow用户
提问于 2017-10-10 11:43:06
回答 1查看 2.5K关注 0票数 1

在使用带有模拟和多线程的GTest时,我遇到了一个争用条件分段错误。我在CentOS 6.9上使用CentOS 1.8,在G++ 6.3.1上使用。可以通过使用给定方法在模拟上调用EXPECT_CALL来触发分段错误,而另一个线程则在同一模拟上调用该方法。下面是一个很小的例子:

代码语言:javascript
复制
#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在发生分段错误时的一些输出:

代码语言:javascript
复制
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来解决这个问题,但是还有其他人遇到过吗?这是一个真正的错误吗?有修复吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-11 16:00:34

Google 文档明确禁止交错期望设置和调用嘲讽:

重要注意: Google要求在调用模拟函数之前设置期望,否则行为就没有定义。特别是,不能将EXPECT_CALL()的调用和对模拟函数的调用交织在一起。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46665898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档