Automatically specify the port for rostest

asked 2017-01-09 22:51:24 -0500

hamzamerzic gravatar image

Hello ROS community.

I am writing unit tests for a functionality that monitors the status of the roscore and performs certain steps in case the roscore stops working (properly). Now, one of the tests I have tests the behavior of complete roscore crash. To do that I do the following:

  1. add_custom_target with a command that spawns a roscore and make it a dependency to the test,
  2. In the test I use --reuse-master flag to attach to the spawned roscore,
  3. I kill the attached roscore within the test to test if the functionality works properly.

All of that looks something like this:

    COMMAND nohup roscore > /dev/null 2>&1 & sleep 1

  add_executable(${PROJECT_NAME}-rostest)  #  ... link libraries etc. Here the roscore is being killed by grep-ing for it's pid

    ARGS --reuse-master --clear
    DEPENDENCIES custom_command_${PROJECT_NAME} ${PROJECT_NAME}-rostest

Now, I would like to have control of on which port to spawn the roscore, since I don't want to use the default port. Adapting the command to do so is straightforward - simply adding a -p and a port number, but then --reuse-master does not connect to the desired port. Setting ROS_MASTER_URI with the same port before running

  catkin run_tests

solves the problem, but I would like to avoid setting the ROS_MASTER_URI every time I run the test and instead simply hard code the port I want to use. So I would like to know if there is a way of explicitly setting the port for --reuse-master to connect to, or setting the ROS_MASTER_URI for the test case from within the CMakeLists?

edit retag flag offensive close merge delete


I don't have any answers, but this is particularly tricky because the high-numbered ports can be assigned randomly by the OS, so if you hardcode the port number, there's a small chance that something else will already be using that port when you start your test.

ahendrix gravatar image ahendrix  ( 2017-01-10 00:57:15 -0500 )edit

rostest's random port selection gets around this by allowing the OS to select a random port, and then internally setting the ROS_MASTER_URI to use that port.

ahendrix gravatar image ahendrix  ( 2017-01-10 01:02:48 -0500 )edit