Implement rclcpp::is_initialized() (#522)

* Implement rclcpp::is_initialized()

* linter
This commit is contained in:
chapulina 2018-07-26 13:17:33 -07:00 committed by William Woodall
parent 8f2052d65a
commit fba891c0df
6 changed files with 53 additions and 2 deletions

View file

@ -386,6 +386,14 @@ if(BUILD_TESTING)
target_link_libraries(test_utilities ${PROJECT_NAME})
endif()
ament_add_gtest(test_init test/test_init.cpp
APPEND_LIBRARY_DIRS "${append_library_dirs}")
if(TARGET test_init)
ament_target_dependencies(test_init
"rcl")
target_link_libraries(test_init ${PROJECT_NAME})
endif()
ament_add_gtest(test_multi_threaded_executor test/executors/test_multi_threaded_executor.cpp
APPEND_LIBRARY_DIRS "${append_library_dirs}")
if(TARGET test_multi_threaded_executor)

View file

@ -87,6 +87,12 @@ RCLCPP_PUBLIC
bool
ok();
/// Returns true if init() has already been called.
/** \return True if init() has been called, false otherwise. */
RCLCPP_PUBLIC
bool
is_initialized();
/// Notify the signal handler and rmw that rclcpp is shutting down.
RCLCPP_PUBLIC
void

View file

@ -264,6 +264,12 @@ rclcpp::ok()
return ::g_signal_status == 0;
}
bool
rclcpp::is_initialized()
{
return rcl_ok();
}
static std::mutex on_shutdown_mutex_;
static std::vector<std::function<void(void)>> on_shutdown_callbacks_;

View file

@ -28,7 +28,7 @@
using namespace std::chrono_literals;
class TestExcutors : public ::testing::Test
class TestExecutors : public ::testing::Test
{
protected:
static void SetUpTestCase()
@ -50,7 +50,7 @@ protected:
};
// Make sure that executors detach from nodes when destructing
TEST_F(TestExcutors, detachOnDestruction) {
TEST_F(TestExecutors, detachOnDestruction) {
{
rclcpp::executors::SingleThreadedExecutor executor;
executor.add_node(node);

29
rclcpp/test/test_init.cpp Normal file
View file

@ -0,0 +1,29 @@
// Copyright 2018 Open Source Robotics Foundation, 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.
#include <gtest/gtest.h>
#include "rclcpp/utilities.hpp"
TEST(TestInit, is_initialized) {
EXPECT_FALSE(rclcpp::is_initialized());
rclcpp::init(0, nullptr);
EXPECT_TRUE(rclcpp::is_initialized());
rclcpp::shutdown();
EXPECT_TRUE(rclcpp::is_initialized());
}

View file

@ -48,4 +48,6 @@ TEST(TestUtilities, init_with_args) {
ASSERT_EQ(1u, other_args.size());
ASSERT_EQ(std::string{"process_name"}, other_args[0]);
EXPECT_TRUE(rclcpp::is_initialized());
}