我构建了一个非常简单的测试,类似于:
#include <ros/node_handle.h>
#include <gtest/gtest.h>
struct SimpleTicker {
SimpleTicker(ros::NodeHandle &nh) : _nh(nh) {
_stateTickTimer = _nh.createTimer(ros::Duration(0.25f),
&SimpleTicker::incrementVal, this, false, true);
}
void incrementVal(const ros::TimerEvent&) {
val++;
}
int val = 0;
ros::Timer _stateTickTimer;
ros::NodeHandle _nh;
};
TEST(SlamManagerTest, run_simple_tick) {
ros::NodeHandle nh(~);
SimpleTicker s(nh);
ros::Duration{1}.sleep();
ASSERT_GT(s.val, 0);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
ros::init(argc, argv, "simple_timer_test");
ros::NodeHandle nh;
if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME,
ros::console::levels::Debug))
{
ros::console::notifyLoggerLevelsChanged(); // show debug output in tests
}
return RUN_ALL_TESTS();
}这个非常简单的测试,用一个计时器启动一个类,这个计时器应该每0.25秒回调一个值。我使用测试文件启动此程序:
<launch>
<test test-name="simple_timer_test" pkg="manager" type="simple_timer_test">
</test>
</launch>只需通过
rostest src/manager/test/simple_timer.test但是测试失败,并且该值永远不会递增。我怀疑我设置rostest来使用时钟的方式有问题。对此有什么想法吗?
发布于 2021-06-07 14:11:30
所以看起来系统没有旋转。我猜是因为这种运行rostest节点的特定方式,所以永远不会调用ros::spin(),因为它忙于运行测试。因此,在需要睡眠的地方,我们需要转而旋转,就像这样:
template <typename F>
void spin_sleep(ros::Duration duration, F break_condition) {
auto end = ros::Time::now() + duration;
while (end > ros::Time::now() && !break_condition()) {
ros::spinOnce();
}
}
void spin_sleep(ros::Duration duration) {
spin_sleep(duration, []() { return false;});
}然后做一些像这样的事情,而不是睡觉:
ros::NodeHandle nh(~);
SimpleTicker s(nh);
spin_sleep(ros::Duration{1});
ASSERT_GT(s.val, 0);https://stackoverflow.com/questions/67866449
复制相似问题