-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration_test_node.cpp
67 lines (54 loc) · 2.26 KB
/
integration_test_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2023 Nick Morales.
//
// 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.
/// @file This is an example ROS 2 node that checks assertions using Catch2.
/// It simply checks if the "test_service" service is available at least once
/// during the duration of the test.
#include "catch_ros2/catch_ros2.hpp"
#include "rclcpp/rclcpp.hpp"
#include "std_srvs/srv/empty.hpp"
using namespace std::chrono_literals;
TEST_CASE("example_integration_test", "[integration]") {
// Create a simple client node to check if the auxiliary node
// has a service available
auto node = rclcpp::Node::make_shared("integration_test_node");
// Declare a parameter on the node
// (the default catch_ros2 node main will allow ROS arguments
// like parameters to be passed to nodes in test files)
node->declare_parameter<double>("test_duration");
// Get value of the parameter
// This line will cause a runtime error if a value
// for the "test_duration" parameter is not passed to the node
const auto TEST_DURATION =
node->get_parameter("test_duration").get_parameter_value().get<double>();
// Create a client for the service we're looking for
auto client = node->create_client<std_srvs::srv::Empty>("test_service");
rclcpp::Time start_time = rclcpp::Clock().now();
bool service_found = false;
// Keep test running only for the length of the "test_duration" parameter
// (in seconds)
while (
rclcpp::ok() &&
((rclcpp::Clock().now() - start_time) < rclcpp::Duration::from_seconds(TEST_DURATION))
)
{
// Repeatedly check for the dummy service until its found
if (client->wait_for_service(0s)) {
service_found = true;
break;
}
rclcpp::spin_some(node);
}
// Test assertions - check that the dummy node was found
CHECK(service_found);
}