|
| 1 | +// Copyright 2024 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <stdexcept> |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "rcl_lifecycle/rcl_lifecycle.h" |
| 22 | + |
| 23 | +#include "rclcpp_lifecycle/lifecycle_node.hpp" |
| 24 | + |
| 25 | +#include "./mocking_utils/patch.hpp" |
| 26 | + |
| 27 | +class TestDefaultStateMachine : public ::testing::Test |
| 28 | +{ |
| 29 | +protected: |
| 30 | + static void SetUpTestCase() |
| 31 | + { |
| 32 | + rclcpp::init(0, nullptr); |
| 33 | + } |
| 34 | + static void TearDownTestCase() |
| 35 | + { |
| 36 | + rclcpp::shutdown(); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +class EmptyLifecycleNode : public rclcpp_lifecycle::LifecycleNode |
| 41 | +{ |
| 42 | +public: |
| 43 | + explicit EmptyLifecycleNode(const std::string & node_name) |
| 44 | + : rclcpp_lifecycle::LifecycleNode(node_name) |
| 45 | + {} |
| 46 | +}; |
| 47 | + |
| 48 | +// This test is split out of test_lifecycle_node.cpp for an esoteric reason. When running on |
| 49 | +// RedHat-based distributions (like Fedora or RHEL), the way that glibc is compiled does not |
| 50 | +// allow mocking_utils::inject_on_return to work. Thus the test has to patch_and_return(). |
| 51 | +// Unfortunately, this means that the resources are not actually cleaned up, and thus other tests |
| 52 | +// may return incorrect results. By having it in a separate process we ensure that the resources |
| 53 | +// will at least be cleaned up by the process dying. |
| 54 | +TEST_F(TestDefaultStateMachine, empty_initializer_rcl_errors) |
| 55 | +{ |
| 56 | + { |
| 57 | + auto patch = mocking_utils::patch_and_return( |
| 58 | + "lib:rclcpp_lifecycle", rcl_lifecycle_state_machine_init, RCL_RET_ERROR); |
| 59 | + EXPECT_THROW( |
| 60 | + std::make_shared<EmptyLifecycleNode>("testnode").reset(), |
| 61 | + std::runtime_error); |
| 62 | + } |
| 63 | + { |
| 64 | + auto test_node = std::make_shared<EmptyLifecycleNode>("testnode"); |
| 65 | + auto patch = mocking_utils::patch_and_return( |
| 66 | + "lib:rclcpp_lifecycle", rcl_lifecycle_state_machine_fini, RCL_RET_ERROR); |
| 67 | + EXPECT_NO_THROW(test_node.reset()); |
| 68 | + } |
| 69 | +} |
0 commit comments