- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在将 GTest 与模拟和多线程一起使用时,我遇到了竞争条件段错误。我在 CentOS 6.9、G++ 6.3.1 上使用 GTest 1.8。可以通过使用给定方法在模拟上调用 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_CALL 来解决这个问题,但是其他人遇到过吗?这是一个真正的错误吗?有修复方法吗?
最佳答案
谷歌模拟 docs明确禁止交错期望设置和调用模拟:
Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions.
关于c++ - 使用 gtest EXPECT_CALL 时出现竞争条件段错误,而另一个期望正在执行相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46665898/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!