Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic fix rcl lifecycle test issue #715

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions rcl_lifecycle/src/default_state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "rcl/error_handling.h"
#include "rcl/rcl.h"
#include "rcutils/strdup.h"

#include "rcl_lifecycle/transition_map.h"

Expand Down Expand Up @@ -658,7 +659,11 @@ rcl_lifecycle_init_default_state_machine(
{
rcl_ret_t ret = RCL_RET_ERROR;
// Used for concatenating error messages in the fail: block.
const char * fail_error_message = "";
// The cause or error which leads to jump to fail:
char * fail_error_message = NULL;
// The error happens in fail:
char * fini_error_message = NULL;
rcl_allocator_t default_allocator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not initializing it directly? That would say it later on.

Suggested change
rcl_allocator_t default_allocator;
rcl_allocator_t default_allocator = rcl_get_default_allocator();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default_allocator is only used in failure case.
I think it happens seldom.
So need not call rcl_get_default_allocator() each time.
What do you think ?


// ***************************
// register all primary states
Expand Down Expand Up @@ -695,25 +700,40 @@ rcl_lifecycle_init_default_state_machine(
fail:
// If rcl_lifecycle_transition_map_fini() fails, it will clobber the error string here.
// Concatenate the error strings if that happens
default_allocator = rcl_get_default_allocator();

if (rcl_error_is_set()) {
fail_error_message = rcl_get_error_string().str;
fail_error_message = rcutils_strdup(rcl_get_error_string().str, default_allocator);
rcl_reset_error();
}

if (rcl_lifecycle_transition_map_fini(&state_machine->transition_map, allocator) != RCL_RET_OK) {
const char * fini_error = "";
if (rcl_error_is_set()) {
fini_error = rcl_get_error_string().str;
fini_error_message = rcutils_strdup(rcl_get_error_string().str, default_allocator);
rcl_reset_error();
}
RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Freeing transition map failed while handling a previous error. Leaking memory!"
"\nOriginal error:\n\t%s\nError encountered in rcl_lifecycle_transition_map_fini():\n\t%s\n",
fail_error_message, fini_error);
fail_error_message != NULL ?
fail_error_message : "Failed to duplicate error while init state machine !",
fini_error_message != NULL ?
fini_error_message : "Failed to duplicate error while fini transition map !");
}

if (!rcl_error_is_set()) {
RCL_SET_ERROR_MSG("Unspecified error in default_state_machine _register_transitions()");
RCL_SET_ERROR_MSG(
(fail_error_message != NULL) ?
fail_error_message : "Unspecified error in rcl_lifecycle_init_default_state_machine() !");
}

if (fail_error_message != NULL) {
default_allocator.deallocate(fail_error_message, default_allocator.state);
}
if (fini_error_message != NULL) {
default_allocator.deallocate(fini_error_message, default_allocator.state);
}

return RCL_RET_ERROR;
}

Expand Down
36 changes: 24 additions & 12 deletions rcl_lifecycle/test/test_rcl_lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,23 @@ TEST(TestRclLifecycle, lifecycle_transition) {

ret = rcl_lifecycle_transition_init(
&transition, expected_id, &expected_label[0], nullptr, nullptr, &allocator);
EXPECT_EQ(ret, RCL_RET_OK);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;
Karsten1987 marked this conversation as resolved.
Show resolved Hide resolved
rcutils_reset_error();
ret = rcl_lifecycle_transition_fini(&transition, &allocator);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;
rcutils_reset_error();

ret = rcl_lifecycle_transition_init(
&transition, expected_id, &expected_label[0], start, nullptr, &allocator);
EXPECT_EQ(ret, RCL_RET_OK);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;
rcutils_reset_error();
ret = rcl_lifecycle_transition_fini(&transition, &allocator);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;
rcutils_reset_error();

start = reinterpret_cast<rcl_lifecycle_state_t *>(
allocator.allocate(sizeof(rcl_lifecycle_state_t), allocator.state));
*start = rcl_lifecycle_get_zero_initialized_state();
Karsten1987 marked this conversation as resolved.
Show resolved Hide resolved
rcl_allocator_t bad_allocator = rcl_get_default_allocator();
bad_allocator.allocate = bad_malloc;
bad_allocator.reallocate = bad_realloc;
Expand Down Expand Up @@ -190,15 +199,16 @@ TEST(TestRclLifecycle, state_machine) {
ret = rcl_init(0, nullptr, &init_options, &context);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;

ret = rcl_node_init(&node, "node", "namespace", &context, &options);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;

OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
ASSERT_EQ(RCL_RET_OK, rcl_shutdown(&context));
ASSERT_EQ(RCL_RET_OK, rcl_context_fini(&context));
ASSERT_EQ(RCL_RET_OK, rcl_node_fini(&node)) << rcl_get_error_string().str;
ASSERT_EQ(RCL_RET_OK, rcl_shutdown(&context)) << rcl_get_error_string().str;
ASSERT_EQ(RCL_RET_OK, rcl_context_fini(&context)) << rcl_get_error_string().str;
});

ret = rcl_node_init(&node, "node", "namespace", &context, &options);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;

const rosidl_message_type_support_t * pn =
ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent);
const rosidl_service_type_support_t * cs =
Expand Down Expand Up @@ -326,6 +336,7 @@ TEST(TestRclLifecycle, state_machine) {
// Node is null
ret = rcl_lifecycle_state_machine_fini(&state_machine, nullptr, &allocator);
EXPECT_EQ(ret, RCL_RET_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect to see << rcl_get_error_string().str here as well.

Copy link
Contributor Author

@Barry-Xu-2018 Barry-Xu-2018 Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPECT_EQ(ret, RCL_RET_ERROR) << rcl_get_error_string().str;

It means rcl_get_error_string().str is output if ret != RCL_RET_ERROR.
That is, ret is RCL_RET_OK. There is no error message.
So I think we need not to add <<rcl_get_error_string().str.
What do you think ?

rcutils_reset_error();
std::cout << "state_machine: " << __LINE__ << std::endl;
}

Expand All @@ -349,15 +360,16 @@ TEST(TestRclLifecycle, state_transitions) {
ret = rcl_init(0, nullptr, &init_options, &context);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;

ret = rcl_node_init(&node, "node", "namespace", &context, &options);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;

OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
ASSERT_EQ(RCL_RET_OK, rcl_shutdown(&context));
ASSERT_EQ(RCL_RET_OK, rcl_context_fini(&context));
ASSERT_EQ(RCL_RET_OK, rcl_node_fini(&node)) << rcl_get_error_string().str;
ASSERT_EQ(RCL_RET_OK, rcl_shutdown(&context)) << rcl_get_error_string().str;
ASSERT_EQ(RCL_RET_OK, rcl_context_fini(&context)) << rcl_get_error_string().str;
});

ret = rcl_node_init(&node, "node", "namespace", &context, &options);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;

const rosidl_message_type_support_t * pn =
ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent);
const rosidl_service_type_support_t * cs =
Expand Down