ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In the end I've managed to do it like this:

TEST(SmokeTests, NodeStartingCorrectly) {
    char  arg0[] = "--ros-args";
    char  arg1[] = "--params-file";
    char  arg2[] = "test.yaml";
    char* argv[] = { &arg0[0], &arg1[0], &arg2[0], NULL };
    int   argc   = (int)(sizeof(argv) / sizeof(argv[0])) - 1;

    // Initialization
    rclcpp::init(argc, argv);

    // Instantiation
    rclcpp::Node::SharedPtr node = std::make_shared<PelicanUnit>();
    rclcpp::executors::MultiThreadedExecutor executor;

    executor.add_node(node);

    std::thread t(
        [&](){
            ASSERT_NO_THROW(executor.spin(););
        }
    );

    std::this_thread::sleep_for(std::chrono::seconds(5));
    executor.cancel();
    t.join(); // Wait for thread completion


    rclcpp::shutdown();
}

I have not found another way with spin_some or spin_until_future_complete, probably due to something related to these issues:

  • https://github.com/ros2/rclcpp/issues/1916
  • https://github.com/ros2/rclpy/issues/585
  • https://github.com/ros2/rclcpp/issues/1454