Skip to content

Commit

Permalink
Allow tracing tests to be run in parallel with other tests (#95)
Browse files Browse the repository at this point in the history
* Only consider relevant test trace data in tracing tests

* Allow having existing tracing sessions during tracing tests

But make sure the tests do not leave tracing sessions behind.

Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai>
  • Loading branch information
christophebedard committed Mar 20, 2024
1 parent 975bf6a commit dfeb3d1
Show file tree
Hide file tree
Showing 20 changed files with 410 additions and 73 deletions.
1 change: 1 addition & 0 deletions test_ros2trace/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<test_depend>test_tracetools</test_depend>
<test_depend>tracetools</test_depend>
<test_depend>tracetools_read</test_depend>
<test_depend>tracetools_test</test_depend>
<test_depend>tracetools_trace</test_depend>

<!-- TODO(clalancette): This is actually a false dependency,
Expand Down
173 changes: 114 additions & 59 deletions test_ros2trace/test/test_ros2trace/test_trace.py

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions test_tracetools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,36 @@ if(BUILD_TESTING)
find_package(lifecycle_msgs REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(rcpputils REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)

# The utility lib is needed even if TRACETOOLS_DISABLED; it's just empty
add_library(${PROJECT_NAME}_mark_process src/mark_process.cpp)
if(NOT TRACETOOLS_DISABLED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LTTNG REQUIRED lttng-ust)
target_link_libraries(${PROJECT_NAME}_mark_process PRIVATE
${LTTNG_LIBRARIES}
rcpputils::rcpputils
)
endif()
target_include_directories(${PROJECT_NAME}_mark_process PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
)
if(TRACETOOLS_DISABLED)
target_compile_definitions(${PROJECT_NAME}_mark_process PRIVATE TRACETOOLS_DISABLED)
endif()
install(
DIRECTORY include/
DESTINATION include/${PROJECT_NAME}
)

add_executable(test_publisher
src/test_publisher.cpp
)
target_link_libraries(test_publisher PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
${std_msgs_TARGETS}
)
Expand All @@ -51,6 +74,7 @@ if(BUILD_TESTING)
src/test_intra.cpp
)
target_link_libraries(test_intra PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
${std_msgs_TARGETS}
)
Expand All @@ -59,6 +83,7 @@ if(BUILD_TESTING)
src/test_lifecycle_node.cpp
)
target_link_libraries(test_lifecycle_node PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
rclcpp_lifecycle::rclcpp_lifecycle
)
Expand All @@ -67,6 +92,7 @@ if(BUILD_TESTING)
src/test_lifecycle_client.cpp
)
target_link_libraries(test_lifecycle_client PRIVATE
${PROJECT_NAME}_mark_process
${lifecycle_msgs_TARGETS}
rclcpp::rclcpp
)
Expand All @@ -75,6 +101,7 @@ if(BUILD_TESTING)
src/test_ping.cpp
)
target_link_libraries(test_ping PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
${std_msgs_TARGETS}
)
Expand All @@ -83,6 +110,7 @@ if(BUILD_TESTING)
src/test_pong.cpp
)
target_link_libraries(test_pong PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
${std_msgs_TARGETS}
)
Expand All @@ -91,13 +119,15 @@ if(BUILD_TESTING)
src/test_timer.cpp
)
target_link_libraries(test_timer PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
)

add_executable(test_service_ping
src/test_service_ping.cpp
)
target_link_libraries(test_service_ping PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
${std_srvs_TARGETS}
)
Expand All @@ -106,6 +136,7 @@ if(BUILD_TESTING)
src/test_service_pong.cpp
)
target_link_libraries(test_service_pong PRIVATE
${PROJECT_NAME}_mark_process
rclcpp::rclcpp
${std_srvs_TARGETS}
)
Expand Down
41 changes: 41 additions & 0 deletions test_tracetools/include/test_tracetools/mark_process.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TEST_TRACETOOLS__MARK_PROCESS_HPP_
#define TEST_TRACETOOLS__MARK_PROCESS_HPP_

#include <string>

namespace test_tracetools
{

/// Mark process to link it to a trace test.
/**
* This should be called once by each test application (process) to mark the process so that it can
* be linked to the trace test that spawned it.
*
* The process responsible for spawning the current process is responsible for setting the
* `TRACETOOLS_TEST_TRACE_TEST_ID` environment variable to a unique value.
*
* If the `TRACETOOLS_TEST_TRACE_TEST_ID` environment variable is set and not empty, this marks the
* current process by triggering an `lttng_ust_tracef:event` trace event with a `msg` field value
* equal to: `"TRACETOOLS_TEST_TRACE_TEST_ID=<value of TRACETOOLS_TEST_TRACE_TEST_ID>"`.
*
* \see tracetools_test
*/
void mark_trace_test_process();

} // namespace test_tracetools

#endif // TEST_TRACETOOLS__MARK_PROCESS_HPP_
4 changes: 4 additions & 0 deletions test_tracetools/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
the dependencies that those packages use so that colcon properly sets up the
environment when running those tests.
-->
<build_export_depend>liblttng-ust-dev</build_export_depend>
<build_export_depend>lifecycle_msgs</build_export_depend>
<build_export_depend>rclcpp</build_export_depend>
<build_export_depend>rclcpp_lifecycle</build_export_depend>
<build_export_depend>rcpputils</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>std_srvs</build_export_depend>

Expand All @@ -31,11 +33,13 @@
<test_depend>ament_cmake_pytest</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>liblttng-ust-dev</test_depend>
<test_depend>lifecycle_msgs</test_depend>
<test_depend>python3-pytest</test_depend>
<test_depend>python3-pytest-cov</test_depend>
<test_depend>rclcpp</test_depend>
<test_depend>rclcpp_lifecycle</test_depend>
<test_depend>rcpputils</test_depend>
<test_depend>std_msgs</test_depend>
<test_depend>std_srvs</test_depend>
<test_depend>tracetools</test_depend>
Expand Down
48 changes: 48 additions & 0 deletions test_tracetools/src/mark_process.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TRACETOOLS_DISABLED
#include <lttng/tracef.h>

#include "rcpputils/env.hpp"
#endif // TRACETOOLS_DISABLED

#include "test_tracetools/mark_process.hpp"

namespace test_tracetools
{

namespace
{

/**
* \see tracetools_test
*/
constexpr std::string_view trace_test_id_env_var = "TRACETOOLS_TEST_TRACE_TEST_ID";

} // namespace

void mark_trace_test_process()
{
#ifndef TRACETOOLS_DISABLED
// See tracetools_test.mark_process for more details
const std::string env_var{trace_test_id_env_var};
const auto test_id = rcpputils::get_env_var(env_var.c_str());
if (!test_id.empty()) {
lttng_ust__tracef("%s=%s", env_var.c_str(), test_id.c_str());
}
#endif // TRACETOOLS_DISABLED
}

} // namespace test_tracetools
3 changes: 3 additions & 0 deletions test_tracetools/src/test_intra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "test_tracetools/mark_process.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -73,6 +74,8 @@ class SubIntraNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_lifecycle_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "lifecycle_msgs/srv/get_state.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rcutils/logging_macros.h"
#include "test_tracetools/mark_process.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -256,6 +257,8 @@ void cycle_through_states(std::shared_ptr<LifecycleNodeClient> client_node)

int main(int argc, char ** argv)
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_lifecycle_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "test_tracetools/mark_process.hpp"

class LifecycleNode : public rclcpp_lifecycle::LifecycleNode
{
Expand Down Expand Up @@ -66,6 +67,8 @@ class LifecycleNode : public rclcpp_lifecycle::LifecycleNode

int main(int argc, char ** argv)
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "test_tracetools/mark_process.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -70,6 +71,8 @@ class PingNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

bool do_only_one = true;
for (int i = 0; i < argc; ++i) {
if (strncmp(argv[i], "do_more", 7) == 0) {
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "test_tracetools/mark_process.hpp"

#define NODE_NAME "test_pong"
#define SUB_TOPIC_NAME "ping"
Expand Down Expand Up @@ -58,6 +59,8 @@ class PongNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

bool do_only_one = true;
for (int i = 0; i < argc; ++i) {
if (strncmp(argv[i], "do_more", 7) == 0) {
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "test_tracetools/mark_process.hpp"

#define NODE_NAME "test_publisher"
#define TOPIC_NAME "the_topic"
Expand All @@ -43,6 +44,8 @@ class PubNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_srvs/srv/empty.hpp"
#include "test_tracetools/mark_process.hpp"

#define NODE_NAME "test_service"
#define SERVICE_NAME "the_service"
Expand Down Expand Up @@ -53,6 +54,8 @@ class ServiceNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_service_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_srvs/srv/empty.hpp"
#include "test_tracetools/mark_process.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -71,6 +72,8 @@ class PingNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_service_pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "rclcpp/rclcpp.hpp"
#include "std_srvs/srv/empty.hpp"
#include "test_tracetools/mark_process.hpp"

#define NODE_NAME "test_service_pong"
#define SERVICE_NAME "ping"
Expand Down Expand Up @@ -59,6 +60,8 @@ class PongNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
3 changes: 3 additions & 0 deletions test_tracetools/src/test_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <chrono>

#include "rclcpp/rclcpp.hpp"
#include "test_tracetools/mark_process.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -50,6 +51,8 @@ class TimerNode : public rclcpp::Node

int main(int argc, char * argv[])
{
test_tracetools::mark_trace_test_process();

rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor exec;
Expand Down
Loading

0 comments on commit dfeb3d1

Please sign in to comment.