Automatically specify the port for rostest
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:
- addcustomtarget with a command that spawns a roscore and make it a dependency to the test,
- In the test I use --reuse-master flag to attach to the spawned roscore,
- I kill the attached roscore within the test to test if the functionality works properly.
All of that looks something like this:
add_custom_target(custom_command_${PROJECT_NAME}
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
add_rostest(test/test_kill_roscore.test
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 ROSMASTERURI with the same port before running
catkin run_tests
solves the problem, but I would like to avoid setting the ROSMASTERURI 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 ROSMASTERURI for the test case from within the CMakeLists?
Asked by hamzamerzic on 2017-01-09 23:51:24 UTC
Comments
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.
Asked by ahendrix on 2017-01-10 01:57:15 UTC
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.
Asked by ahendrix on 2017-01-10 02:02:48 UTC