Skip to content

Commit 4d20bda

Browse files
committed
Implementing get_qos_for_publishers/subscribers
- Implemented the functions defined in ros2#511 - rcl_get_qos_for_publishers - rcl_get_qos_for_subscribers - Wrote tests for the same Note: colcon build --packages-up-to rcl && colcon test --packages-select rcl && colcon test-result --verbose --test-result-base build/rcl runs successfully without any errors or failures. Signed-off-by: Jaison Titus <[email protected]>
1 parent 4b1aea0 commit 4d20bda

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

rcl/src/rcl/graph.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,67 @@ rcl_count_subscribers(
375375
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
376376
}
377377

378+
379+
typedef rmw_ret_t (* get_qos_func)(
380+
const rmw_node_t * node, const char * topic_name,
381+
rmw_participants_t * participants);
382+
383+
rcl_ret_t
384+
__rcl_get_qos_for_participants(
385+
const rcl_node_t * node,
386+
const char * topic_name,
387+
rmw_participants_t * participants,
388+
get_qos_func get_qos_for_participants)
389+
{
390+
if (!rcl_node_is_valid(node)) {
391+
RCL_SET_ERROR_MSG("Invalid node provided.");
392+
return RCL_RET_NODE_INVALID; // error already set
393+
}
394+
const rcl_node_options_t * node_options = rcl_node_get_options(node);
395+
if (!node_options) {
396+
RCL_SET_ERROR_MSG("Node options are invalid.");
397+
return RCL_RET_NODE_INVALID; // shouldn't happen, but error is already set if so
398+
}
399+
RCL_CHECK_ARGUMENT_FOR_NULL(topic_name, RCL_RET_INVALID_ARGUMENT);
400+
RCL_CHECK_ARGUMENT_FOR_NULL(participants, RCL_RET_INVALID_ARGUMENT);
401+
if (participants->participants != NULL) {
402+
RCL_SET_ERROR_MSG("The variable participants inside rmw_participants_t should be set to null. "
403+
"It will be malloc'd where it gets populated.");
404+
return RCL_RET_INVALID_ARGUMENT;
405+
}
406+
rmw_ret_t rmw_ret = get_qos_for_participants(
407+
rcl_node_get_rmw_handle(node),
408+
topic_name,
409+
participants);
410+
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
411+
}
412+
413+
rcl_ret_t
414+
rcl_get_qos_for_publishers(
415+
const rcl_node_t * node,
416+
const char * topic_name,
417+
rmw_participants_t * publishers)
418+
{
419+
return __rcl_get_qos_for_participants(
420+
node,
421+
topic_name,
422+
publishers,
423+
rmw_get_qos_for_publishers);
424+
}
425+
426+
rcl_ret_t
427+
rcl_get_qos_for_subscribers(
428+
const rcl_node_t * node,
429+
const char * topic_name,
430+
rmw_participants_t * subscribers)
431+
{
432+
return __rcl_get_qos_for_participants(
433+
node,
434+
topic_name,
435+
subscribers,
436+
rmw_get_qos_for_subscribers);
437+
}
438+
378439
rcl_ret_t
379440
rcl_service_server_is_available(
380441
const rcl_node_t * node,

rcl/test/rcl/test_graph.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class CLASSNAME (TestGraphFixture, RMW_IMPLEMENTATION) : public ::testing::Test
6363
rcl_wait_set_t * wait_set_ptr;
6464
const char * test_graph_node_name = "test_graph_node";
6565

66+
rmw_participants_t * participants;
67+
const char * topic_name = "valid_topic_name";
68+
6669
void SetUp()
6770
{
6871
rcl_ret_t ret;
@@ -101,6 +104,12 @@ class CLASSNAME (TestGraphFixture, RMW_IMPLEMENTATION) : public ::testing::Test
101104
ret = rcl_wait_set_init(
102105
this->wait_set_ptr, 0, 1, 0, 0, 0, 0, this->context_ptr, rcl_get_default_allocator());
103106
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
107+
108+
rmw_participants_t valid_participants = {
109+
0, /*count*/
110+
nullptr /*participants*/
111+
};
112+
this->participants = &valid_participants;
104113
}
105114

106115
void TearDown()
@@ -1321,3 +1330,126 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_rcl_service_server_
13211330
wait_for_service_state_to_change(false, is_available);
13221331
ASSERT_FALSE(is_available);
13231332
}
1333+
1334+
1335+
/*
1336+
* This does not test content of the response.
1337+
* It only tests if the return code is the one expected.
1338+
*/
1339+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1340+
test_rcl_get_qos_for_publishers_null_node)
1341+
{
1342+
auto ret = rcl_get_qos_for_publishers(nullptr, this->topic_name, this->participants);
1343+
EXPECT_EQ(RCL_RET_NODE_INVALID, ret);
1344+
}
1345+
1346+
/*
1347+
* This does not test content of the response.
1348+
* It only tests if the return code is the one expected.
1349+
*/
1350+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1351+
test_rcl_get_qos_for_subscribers_null_node)
1352+
{
1353+
auto ret = rcl_get_qos_for_subscribers(nullptr, this->topic_name, this->participants);
1354+
EXPECT_EQ(RCL_RET_NODE_INVALID, ret);
1355+
}
1356+
1357+
/*
1358+
* This does not test content of the response.
1359+
* It only tests if the return code is the one expected.
1360+
*/
1361+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1362+
test_rcl_get_qos_for_publishers_invalid_node)
1363+
{
1364+
// this->old_node_ptr is a pointer to an invalid node.
1365+
auto ret =
1366+
rcl_get_qos_for_publishers(this->old_node_ptr, this->topic_name, this->participants);
1367+
EXPECT_EQ(RCL_RET_NODE_INVALID, ret);
1368+
}
1369+
1370+
/*
1371+
* This does not test content of the response.
1372+
* It only tests if the return code is the one expected.
1373+
*/
1374+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1375+
test_rcl_get_qos_for_subscribers_invalid_node)
1376+
{
1377+
// this->old_node_ptr is a pointer to an invalid node.
1378+
auto ret = rcl_get_qos_for_subscribers(this->old_node_ptr, this->topic_name,
1379+
this->participants);
1380+
EXPECT_EQ(RCL_RET_NODE_INVALID, ret);
1381+
}
1382+
1383+
/*
1384+
* This does not test content of the response.
1385+
* It only tests if the return code is the one expected.
1386+
*/
1387+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1388+
test_rcl_get_qos_for_publishers_null_topic)
1389+
{
1390+
auto ret = rcl_get_qos_for_publishers(this->node_ptr, nullptr, this->participants);
1391+
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret);
1392+
}
1393+
1394+
/*
1395+
* This does not test content of the response.
1396+
* It only tests if the return code is the one expected.
1397+
*/
1398+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1399+
test_rcl_get_qos_for_subscribers_null_topic)
1400+
{
1401+
auto ret = rcl_get_qos_for_subscribers(this->node_ptr, nullptr, this->participants);
1402+
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret);
1403+
}
1404+
1405+
/*
1406+
* This does not test content of the response.
1407+
* It only tests if the return code is the one expected.
1408+
*/
1409+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1410+
test_rcl_get_qos_for_publishers_null_participants)
1411+
{
1412+
auto ret = rcl_get_qos_for_publishers(this->node_ptr, this->topic_name, nullptr);
1413+
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret);
1414+
}
1415+
1416+
/*
1417+
* This does not test content of the response.
1418+
* It only tests if the return code is the one expected.
1419+
*/
1420+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1421+
test_rcl_get_qos_for_subscribers_null_participants)
1422+
{
1423+
auto ret = rcl_get_qos_for_subscribers(this->node_ptr, this->topic_name, nullptr);
1424+
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret);
1425+
}
1426+
1427+
/*
1428+
* This does not test content of the response.
1429+
* It only tests if the return code is the one expected.
1430+
*/
1431+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1432+
test_rcl_get_qos_for_publishers_invalid_participants)
1433+
{
1434+
// this participant is invalid as the pointer "participants" inside is expected to be null.
1435+
this->participants->participants =
1436+
reinterpret_cast<rmw_participant_qos_profile_t *>(malloc(
1437+
sizeof(rmw_participant_qos_profile_t *)));
1438+
auto ret = rcl_get_qos_for_publishers(this->node_ptr, this->topic_name, this->participants);
1439+
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret);
1440+
}
1441+
1442+
/*
1443+
* This does not test content of the response.
1444+
* It only tests if the return code is the one expected.
1445+
*/
1446+
TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION),
1447+
test_rcl_get_qos_for_subscribers_invalid_participants)
1448+
{
1449+
// this participant is invalid as the pointer "participants" inside is expected to be null.
1450+
this->participants->participants =
1451+
reinterpret_cast<rmw_participant_qos_profile_t *>(malloc(
1452+
sizeof(rmw_participant_qos_profile_t *)));
1453+
auto ret = rcl_get_qos_for_subscribers(this->node_ptr, this->topic_name, this->participants);
1454+
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret);
1455+
}

0 commit comments

Comments
 (0)