refactor init to not be global (#336)

* refactor init to not be global

Signed-off-by: William Woodall <william@osrfoundation.org>

* style changes

Signed-off-by: William Woodall <william@osrfoundation.org>

* refactor to hide use of C11 atomics in implementation

Signed-off-by: William Woodall <william@osrfoundation.org>

* fix new action tests

Signed-off-by: William Woodall <william@osrfoundation.org>

* use alternative atomic init for Windows support

* updates after rebase

Signed-off-by: William Woodall <william@osrfoundation.org>

* cleanup rmw_init_options before copying

Signed-off-by: William Woodall <william@osrfoundation.org>

* fix two bugs in new init code

* relax validity checks in a few places to facilitate post shutdown cleanup

Signed-off-by: William Woodall <william@osrfoundation.org>

* fixing tests for new API behavior

Signed-off-by: William Woodall <william@osrfoundation.org>

* to allocator -> to allocate

* acutally call rmw_shutdown() and address review comments

Signed-off-by: William Woodall <william@osrfoundation.org>
This commit is contained in:
William Woodall 2018-11-29 21:32:54 -08:00 committed by GitHub
parent dfaa412bbf
commit 97ad0013e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 1951 additions and 629 deletions

View file

@ -61,6 +61,7 @@ if(BUILD_TESTING)
find_package(ament_cmake_gtest REQUIRED)
find_package(ament_lint_auto REQUIRED)
find_package(rcl REQUIRED)
find_package(osrf_testing_tools_cpp REQUIRED)
ament_lint_auto_find_test_dependencies()
# Gtests
@ -70,6 +71,7 @@ if(BUILD_TESTING)
if(TARGET test_default_state_machine)
target_include_directories(test_default_state_machine PUBLIC
${rcl_INCLUDE_DIRS}
${osrf_testing_tools_cpp_INCLUDE_DIRS}
)
target_link_libraries(test_default_state_machine ${PROJECT_NAME})
endif()
@ -79,6 +81,7 @@ if(BUILD_TESTING)
if(TARGET test_multiple_instances)
target_include_directories(test_multiple_instances PUBLIC
${rcl_INCLUDE_DIRS}
${osrf_testing_tools_cpp_INCLUDE_DIRS}
)
target_link_libraries(test_multiple_instances ${PROJECT_NAME})
endif()

View file

@ -24,6 +24,7 @@
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>osrf_testing_tools_cpp</test_depend>
<export>
<build_type>ament_cmake</build_type>

View file

@ -19,6 +19,8 @@
#include <gtest/gtest.h>
#include <vector>
#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "lifecycle_msgs/msg/state.h"
#include "lifecycle_msgs/msg/transition.h"
@ -32,20 +34,30 @@
class TestDefaultStateMachine : public ::testing::Test
{
protected:
public:
rcl_context_t * context_ptr;
rcl_node_t * node_ptr;
const rcl_allocator_t * allocator;
void SetUp()
{
rcl_ret_t ret;
ret = rcl_init(0, nullptr, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str;
});
this->context_ptr = new rcl_context_t;
*this->context_ptr = rcl_get_zero_initialized_context();
ret = rcl_init(0, nullptr, &init_options, this->context_ptr);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
}
this->node_ptr = new rcl_node_t;
*this->node_ptr = rcl_get_zero_initialized_node();
const char * name = "test_state_machine_node";
rcl_node_options_t node_options = rcl_node_get_default_options();
ret = rcl_node_init(this->node_ptr, name, "", &node_options);
ret = rcl_node_init(this->node_ptr, name, "", this->context_ptr, &node_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
const rcl_node_options_t * node_ops = rcl_node_get_options(this->node_ptr);
this->allocator = &node_ops->allocator;
@ -56,7 +68,10 @@ protected:
rcl_ret_t ret = rcl_node_fini(this->node_ptr);
delete this->node_ptr;
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_shutdown();
ret = rcl_shutdown(this->context_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_context_fini(this->context_ptr);
delete this->context_ptr;
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
}
};

View file

@ -19,6 +19,8 @@
#include <gtest/gtest.h>
#include <vector>
#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "lifecycle_msgs/msg/state.h"
#include "lifecycle_msgs/msg/transition.h"
@ -30,19 +32,30 @@
class TestMultipleInstances : public ::testing::Test
{
protected:
public:
rcl_context_t * context_ptr;
rcl_node_t * node_ptr;
const rcl_allocator_t * allocator;
void SetUp()
{
rcl_ret_t ret;
ret = rcl_init(0, nullptr, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str;
});
this->context_ptr = new rcl_context_t;
*this->context_ptr = rcl_get_zero_initialized_context();
ret = rcl_init(0, nullptr, &init_options, this->context_ptr);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
}
this->node_ptr = new rcl_node_t;
*this->node_ptr = rcl_get_zero_initialized_node();
const char * name = "test_multiple_instances_node";
const char * name = "test_state_machine_node";
rcl_node_options_t node_options = rcl_node_get_default_options();
ret = rcl_node_init(this->node_ptr, name, "", &node_options);
ret = rcl_node_init(this->node_ptr, name, "", this->context_ptr, &node_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
const rcl_node_options_t * node_ops = rcl_node_get_options(this->node_ptr);
this->allocator = &node_ops->allocator;
@ -53,7 +66,10 @@ protected:
rcl_ret_t ret = rcl_node_fini(this->node_ptr);
delete this->node_ptr;
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_shutdown();
ret = rcl_shutdown(this->context_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_context_fini(this->context_ptr);
delete this->context_ptr;
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
}
};