Skip to content

Commit 8c3c133

Browse files
Add rmw_get_gid_for_client & tests (#206)
* add rmw_get_gid_for_client Signed-off-by: Brian Chen <[email protected]> * remove whitespace Signed-off-by: Brian Chen <[email protected]> * Add missing include Signed-off-by: Jacob Perron <[email protected]> * Remove unused variable Signed-off-by: Jacob Perron <[email protected]> * Fix test failure + minor refactor * Use absolute service names, otherwise tests fail. * Rename variables and structure new tests to match existing tests for consistency Signed-off-by: Brian Chen <[email protected]> Signed-off-by: Jacob Perron <[email protected]> Co-authored-by: Jacob Perron <[email protected]>
1 parent 4a20512 commit 8c3c133

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

rmw_implementation/src/functions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ RMW_INTERFACE_FN(
646646
rmw_ret_t, RMW_RET_ERROR,
647647
3, ARG_TYPES(const rmw_node_t *, const char *, size_t *))
648648

649+
RMW_INTERFACE_FN(
650+
rmw_get_gid_for_client,
651+
rmw_ret_t, RMW_RET_ERROR,
652+
2, ARG_TYPES(const rmw_client_t *, rmw_gid_t *))
653+
649654
RMW_INTERFACE_FN(
650655
rmw_get_gid_for_publisher,
651656
rmw_ret_t, RMW_RET_ERROR,
@@ -816,6 +821,7 @@ void prefetch_symbols(void)
816821
GET_SYMBOL(rmw_get_node_names_with_enclaves)
817822
GET_SYMBOL(rmw_count_publishers)
818823
GET_SYMBOL(rmw_count_subscribers)
824+
GET_SYMBOL(rmw_get_gid_for_client)
819825
GET_SYMBOL(rmw_get_gid_for_publisher)
820826
GET_SYMBOL(rmw_compare_gids_equal)
821827
GET_SYMBOL(rmw_service_response_publisher_get_actual_qos)
@@ -934,6 +940,7 @@ unload_library()
934940
symbol_rmw_get_node_names_with_enclaves = nullptr;
935941
symbol_rmw_count_publishers = nullptr;
936942
symbol_rmw_count_subscribers = nullptr;
943+
symbol_rmw_get_gid_for_client = nullptr;
937944
symbol_rmw_get_gid_for_publisher = nullptr;
938945
symbol_rmw_compare_gids_equal = nullptr;
939946
symbol_rmw_service_server_is_available = nullptr;

test_rmw_implementation/test/test_unique_identifiers.cpp

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "rmw/rmw.h"
2424

2525
#include "test_msgs/msg/basic_types.h"
26+
#include "test_msgs/srv/basic_types.h"
2627

2728

2829
#ifdef RMW_IMPLEMENTATION
@@ -57,12 +58,18 @@ class CLASSNAME (TestUniqueIdentifierAPI, RMW_IMPLEMENTATION) : public ::testing
5758
constexpr char topic_name[] = "/test0";
5859
pub = rmw_create_publisher(node, ts, topic_name, &qos_profile, &options);
5960
ASSERT_NE(nullptr, pub) << rmw_get_error_string().str;
61+
62+
constexpr char service_name[] = "/test_service0";
63+
client = rmw_create_client(node, srv_ts, service_name, &qos_profile);
64+
ASSERT_NE(nullptr, client) << rmw_get_error_string().str;
6065
}
6166

6267
void TearDown() override
6368
{
6469
rmw_ret_t ret = rmw_destroy_publisher(node, pub);
6570
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
71+
ret = rmw_destroy_client(node, client);
72+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
6673
ret = rmw_destroy_node(node);
6774
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
6875
ret = rmw_shutdown(&context);
@@ -75,11 +82,14 @@ class CLASSNAME (TestUniqueIdentifierAPI, RMW_IMPLEMENTATION) : public ::testing
7582
rmw_node_t * node{nullptr};
7683
const rosidl_message_type_support_t * ts{
7784
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes)};
85+
const rosidl_service_type_support_t * srv_ts{
86+
ROSIDL_GET_SRV_TYPE_SUPPORT(test_msgs, srv, BasicTypes)};
7887
rmw_qos_profile_t qos_profile{rmw_qos_profile_default};
7988
rmw_publisher_t * pub{nullptr};
89+
rmw_client_t * client{nullptr};
8090
};
8191

82-
TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_gid_with_bad_args) {
92+
TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_pub_gid_with_bad_args) {
8393
rmw_gid_t gid{};
8494
rmw_ret_t ret = rmw_get_gid_for_publisher(pub, &gid);
8595
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
@@ -113,6 +123,41 @@ TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_gid_with_bad_
113123
EXPECT_TRUE(gids_are_equal);
114124
}
115125

126+
127+
TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_client_gid_with_bad_args) {
128+
rmw_gid_t gid{};
129+
rmw_ret_t ret = rmw_get_gid_for_client(client, &gid);
130+
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
131+
rmw_gid_t expected_gid = gid;
132+
133+
ret = rmw_get_gid_for_client(nullptr, &gid);
134+
EXPECT_EQ(RMW_RET_INVALID_ARGUMENT, ret);
135+
rmw_reset_error();
136+
bool gids_are_equal = false;
137+
ret = rmw_compare_gids_equal(&expected_gid, &gid, &gids_are_equal);
138+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
139+
EXPECT_TRUE(gids_are_equal);
140+
141+
ret = rmw_get_gid_for_client(client, nullptr);
142+
EXPECT_EQ(RMW_RET_INVALID_ARGUMENT, ret);
143+
rmw_reset_error();
144+
gids_are_equal = false;
145+
ret = rmw_compare_gids_equal(&expected_gid, &gid, &gids_are_equal);
146+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
147+
EXPECT_TRUE(gids_are_equal);
148+
149+
const char * implementation_identifier = client->implementation_identifier;
150+
client->implementation_identifier = "not-an-rmw-implementation-identifier";
151+
ret = rmw_get_gid_for_client(client, &gid);
152+
client->implementation_identifier = implementation_identifier;
153+
EXPECT_EQ(RMW_RET_INCORRECT_RMW_IMPLEMENTATION, ret);
154+
rmw_reset_error();
155+
gids_are_equal = false;
156+
ret = rmw_compare_gids_equal(&expected_gid, &gid, &gids_are_equal);
157+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
158+
EXPECT_TRUE(gids_are_equal);
159+
}
160+
116161
TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), compare_gids_with_bad_args) {
117162
rmw_gid_t gid{};
118163
rmw_ret_t ret = rmw_get_gid_for_publisher(pub, &gid);
@@ -221,3 +266,63 @@ TEST_F(CLASSNAME(TestUniqueIdentifiersForMultiplePublishers, RMW_IMPLEMENTATION)
221266
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
222267
EXPECT_FALSE(are_equal);
223268
}
269+
270+
271+
class CLASSNAME (TestUniqueIdentifiersForMultipleClients, RMW_IMPLEMENTATION)
272+
: public CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION)
273+
{
274+
protected:
275+
using Base = CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION);
276+
void SetUp() override
277+
{
278+
Base::SetUp();
279+
constexpr char service_name[] = "/test_service1";
280+
first_client_for_service1 = rmw_create_client(
281+
node, srv_ts, service_name, &qos_profile);
282+
ASSERT_NE(nullptr, first_client_for_service1) << rmw_get_error_string().str;
283+
second_client_for_service1 = rmw_create_client(
284+
node, srv_ts, service_name, &qos_profile);
285+
ASSERT_NE(nullptr, second_client_for_service1) << rmw_get_error_string().str;
286+
client_for_service0 = client;
287+
}
288+
289+
void TearDown() override
290+
{
291+
rmw_ret_t ret = rmw_destroy_client(node, first_client_for_service1);
292+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
293+
ret = rmw_destroy_client(node, second_client_for_service1);
294+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
295+
Base::TearDown();
296+
}
297+
298+
rmw_client_t * client_for_service0{nullptr};
299+
rmw_client_t * first_client_for_service1{nullptr};
300+
rmw_client_t * second_client_for_service1{nullptr};
301+
};
302+
303+
304+
TEST_F(CLASSNAME(TestUniqueIdentifiersForMultipleClients, RMW_IMPLEMENTATION), different_clis) {
305+
rmw_gid_t gid_of_client_for_service0{};
306+
rmw_ret_t ret = rmw_get_gid_for_client(client_for_service0, &gid_of_client_for_service0);
307+
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
308+
309+
rmw_gid_t gid_of_first_client_for_service1{};
310+
ret = rmw_get_gid_for_client(first_client_for_service1, &gid_of_first_client_for_service1);
311+
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
312+
313+
rmw_gid_t gid_of_second_client_for_service1{};
314+
ret = rmw_get_gid_for_client(second_client_for_service1, &gid_of_second_client_for_service1);
315+
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
316+
317+
bool are_equal = true;
318+
ret = rmw_compare_gids_equal(
319+
&gid_of_client_for_service0, &gid_of_first_client_for_service1, &are_equal);
320+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
321+
EXPECT_FALSE(are_equal);
322+
323+
are_equal = true;
324+
ret = rmw_compare_gids_equal(
325+
&gid_of_first_client_for_service1, &gid_of_second_client_for_service1, &are_equal);
326+
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
327+
EXPECT_FALSE(are_equal);
328+
}

0 commit comments

Comments
 (0)