Skip to content

Commit

Permalink
Update codes based on review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Barry Xu <barry.xu@sony.com>
  • Loading branch information
Barry-Xu-2018 committed Oct 29, 2020
1 parent 39a8cba commit 5e1279a
Showing 1 changed file with 59 additions and 75 deletions.
134 changes: 59 additions & 75 deletions rcl/test/rcl/test_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

#include "rcutils/logging_macros.h"
#include "rcutils/logging.h"
#include "rcutils/env.h"

#include "test_msgs/msg/basic_types.h"
#include "test_msgs/srv/basic_types.h"
Expand Down Expand Up @@ -1234,6 +1233,15 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_query_functio
* Note: this test could be impacted by other communications on the same ROS Domain.
*/
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_condition_trigger_check) {
#define CHECK_GUARD_CONDITION_CHANGE(EXPECTED_RESULT) do { \
ret = rcl_wait_set_clear(&wait_set); \
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; \
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL); \
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; \
ret = rcl_wait(&wait_set, time_to_sleep.count()); \
ASSERT_EQ(EXPECTED_RESULT, ret) << rcl_get_error_string().str; \
} while (0)

rcl_ret_t ret;
std::chrono::nanoseconds time_to_sleep = std::chrono::milliseconds(400);

Expand All @@ -1247,7 +1255,7 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi
});

// Test in new ROS domain
ret = rcl_init_options_set_domain_id(&init_options, 66);
ret = rcl_init_options_set_domain_id(&init_options, 42);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

rcl_context_t context = rcl_get_zero_initialized_context();
Expand All @@ -1261,7 +1269,7 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi

rcl_node_t node = rcl_get_zero_initialized_node();
rcl_node_options_t node_options = rcl_node_get_default_options();
ret = rcl_node_init(&node, "test_graph", "", &context, &node_options);
ret = rcl_node_init(&node, "test_graph", "/", &context, &node_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
Expand All @@ -1282,12 +1290,10 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi
rcl_node_get_graph_guard_condition(&node);

// Graph change since first node created
ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since creating the publisher
rcl_publisher_t pub = rcl_get_zero_initialized_publisher();
Expand All @@ -1297,23 +1303,19 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi
"/chatter_test_graph_guard_condition_topics", &pub_ops);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since destroying the publisher
ret = rcl_publisher_fini(&pub, &node);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since creating the subscription
rcl_subscription_t sub = rcl_get_zero_initialized_subscription();
Expand All @@ -1323,23 +1325,19 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi
"/chatter_test_graph_guard_condition_topics", &sub_ops);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since destroying the subscription
ret = rcl_subscription_fini(&sub, &node);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since creating service
rcl_service_t service = rcl_get_zero_initialized_service();
Expand All @@ -1352,23 +1350,19 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi
&service_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since destroy service
ret = rcl_service_fini(&service, &node);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since creating client
rcl_client_t client = rcl_get_zero_initialized_client();
Expand All @@ -1381,54 +1375,44 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_conditi
&client_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since destroying client
ret = rcl_client_fini(&client, &node);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since adding new node
rcl_node_t node_new = rcl_get_zero_initialized_node();
ret = rcl_node_init(&node_new, "test_graph2", "", &context, &node_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Graph change since destroying new node
ret = rcl_node_fini(&node_new);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_OK);
}

// Should not get graph change if no change
ret = rcl_wait_set_clear(&wait_set);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait_set_add_guard_condition(&wait_set, graph_guard_condition, NULL);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ret = rcl_wait(&wait_set, time_to_sleep.count());
ASSERT_EQ(RCL_RET_TIMEOUT, ret) << rcl_get_error_string().str;
{
SCOPED_TRACE("Check guard condition change failed !");
CHECK_GUARD_CONDITION_CHANGE(RCL_RET_TIMEOUT);
}
}

/* Test the rcl_service_server_is_available function.
Expand Down

0 comments on commit 5e1279a

Please sign in to comment.