Skip to content

Commit

Permalink
Add rmw_get_gid_for_client & tests (#206)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ihasdapie and jacobperron authored Sep 23, 2022
1 parent 4a20512 commit 8c3c133
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
7 changes: 7 additions & 0 deletions rmw_implementation/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ RMW_INTERFACE_FN(
rmw_ret_t, RMW_RET_ERROR,
3, ARG_TYPES(const rmw_node_t *, const char *, size_t *))

RMW_INTERFACE_FN(
rmw_get_gid_for_client,
rmw_ret_t, RMW_RET_ERROR,
2, ARG_TYPES(const rmw_client_t *, rmw_gid_t *))

RMW_INTERFACE_FN(
rmw_get_gid_for_publisher,
rmw_ret_t, RMW_RET_ERROR,
Expand Down Expand Up @@ -816,6 +821,7 @@ void prefetch_symbols(void)
GET_SYMBOL(rmw_get_node_names_with_enclaves)
GET_SYMBOL(rmw_count_publishers)
GET_SYMBOL(rmw_count_subscribers)
GET_SYMBOL(rmw_get_gid_for_client)
GET_SYMBOL(rmw_get_gid_for_publisher)
GET_SYMBOL(rmw_compare_gids_equal)
GET_SYMBOL(rmw_service_response_publisher_get_actual_qos)
Expand Down Expand Up @@ -934,6 +940,7 @@ unload_library()
symbol_rmw_get_node_names_with_enclaves = nullptr;
symbol_rmw_count_publishers = nullptr;
symbol_rmw_count_subscribers = nullptr;
symbol_rmw_get_gid_for_client = nullptr;
symbol_rmw_get_gid_for_publisher = nullptr;
symbol_rmw_compare_gids_equal = nullptr;
symbol_rmw_service_server_is_available = nullptr;
Expand Down
107 changes: 106 additions & 1 deletion test_rmw_implementation/test/test_unique_identifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rmw/rmw.h"

#include "test_msgs/msg/basic_types.h"
#include "test_msgs/srv/basic_types.h"


#ifdef RMW_IMPLEMENTATION
Expand Down Expand Up @@ -57,12 +58,18 @@ class CLASSNAME (TestUniqueIdentifierAPI, RMW_IMPLEMENTATION) : public ::testing
constexpr char topic_name[] = "/test0";
pub = rmw_create_publisher(node, ts, topic_name, &qos_profile, &options);
ASSERT_NE(nullptr, pub) << rmw_get_error_string().str;

constexpr char service_name[] = "/test_service0";
client = rmw_create_client(node, srv_ts, service_name, &qos_profile);
ASSERT_NE(nullptr, client) << rmw_get_error_string().str;
}

void TearDown() override
{
rmw_ret_t ret = rmw_destroy_publisher(node, pub);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
ret = rmw_destroy_client(node, client);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
ret = rmw_destroy_node(node);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
ret = rmw_shutdown(&context);
Expand All @@ -75,11 +82,14 @@ class CLASSNAME (TestUniqueIdentifierAPI, RMW_IMPLEMENTATION) : public ::testing
rmw_node_t * node{nullptr};
const rosidl_message_type_support_t * ts{
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes)};
const rosidl_service_type_support_t * srv_ts{
ROSIDL_GET_SRV_TYPE_SUPPORT(test_msgs, srv, BasicTypes)};
rmw_qos_profile_t qos_profile{rmw_qos_profile_default};
rmw_publisher_t * pub{nullptr};
rmw_client_t * client{nullptr};
};

TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_gid_with_bad_args) {
TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_pub_gid_with_bad_args) {
rmw_gid_t gid{};
rmw_ret_t ret = rmw_get_gid_for_publisher(pub, &gid);
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
Expand Down Expand Up @@ -113,6 +123,41 @@ TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_gid_with_bad_
EXPECT_TRUE(gids_are_equal);
}


TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), get_client_gid_with_bad_args) {
rmw_gid_t gid{};
rmw_ret_t ret = rmw_get_gid_for_client(client, &gid);
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
rmw_gid_t expected_gid = gid;

ret = rmw_get_gid_for_client(nullptr, &gid);
EXPECT_EQ(RMW_RET_INVALID_ARGUMENT, ret);
rmw_reset_error();
bool gids_are_equal = false;
ret = rmw_compare_gids_equal(&expected_gid, &gid, &gids_are_equal);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
EXPECT_TRUE(gids_are_equal);

ret = rmw_get_gid_for_client(client, nullptr);
EXPECT_EQ(RMW_RET_INVALID_ARGUMENT, ret);
rmw_reset_error();
gids_are_equal = false;
ret = rmw_compare_gids_equal(&expected_gid, &gid, &gids_are_equal);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
EXPECT_TRUE(gids_are_equal);

const char * implementation_identifier = client->implementation_identifier;
client->implementation_identifier = "not-an-rmw-implementation-identifier";
ret = rmw_get_gid_for_client(client, &gid);
client->implementation_identifier = implementation_identifier;
EXPECT_EQ(RMW_RET_INCORRECT_RMW_IMPLEMENTATION, ret);
rmw_reset_error();
gids_are_equal = false;
ret = rmw_compare_gids_equal(&expected_gid, &gid, &gids_are_equal);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
EXPECT_TRUE(gids_are_equal);
}

TEST_F(CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION), compare_gids_with_bad_args) {
rmw_gid_t gid{};
rmw_ret_t ret = rmw_get_gid_for_publisher(pub, &gid);
Expand Down Expand Up @@ -221,3 +266,63 @@ TEST_F(CLASSNAME(TestUniqueIdentifiersForMultiplePublishers, RMW_IMPLEMENTATION)
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
EXPECT_FALSE(are_equal);
}


class CLASSNAME (TestUniqueIdentifiersForMultipleClients, RMW_IMPLEMENTATION)
: public CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION)
{
protected:
using Base = CLASSNAME(TestUniqueIdentifierAPI, RMW_IMPLEMENTATION);
void SetUp() override
{
Base::SetUp();
constexpr char service_name[] = "/test_service1";
first_client_for_service1 = rmw_create_client(
node, srv_ts, service_name, &qos_profile);
ASSERT_NE(nullptr, first_client_for_service1) << rmw_get_error_string().str;
second_client_for_service1 = rmw_create_client(
node, srv_ts, service_name, &qos_profile);
ASSERT_NE(nullptr, second_client_for_service1) << rmw_get_error_string().str;
client_for_service0 = client;
}

void TearDown() override
{
rmw_ret_t ret = rmw_destroy_client(node, first_client_for_service1);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
ret = rmw_destroy_client(node, second_client_for_service1);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
Base::TearDown();
}

rmw_client_t * client_for_service0{nullptr};
rmw_client_t * first_client_for_service1{nullptr};
rmw_client_t * second_client_for_service1{nullptr};
};


TEST_F(CLASSNAME(TestUniqueIdentifiersForMultipleClients, RMW_IMPLEMENTATION), different_clis) {
rmw_gid_t gid_of_client_for_service0{};
rmw_ret_t ret = rmw_get_gid_for_client(client_for_service0, &gid_of_client_for_service0);
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;

rmw_gid_t gid_of_first_client_for_service1{};
ret = rmw_get_gid_for_client(first_client_for_service1, &gid_of_first_client_for_service1);
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;

rmw_gid_t gid_of_second_client_for_service1{};
ret = rmw_get_gid_for_client(second_client_for_service1, &gid_of_second_client_for_service1);
ASSERT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;

bool are_equal = true;
ret = rmw_compare_gids_equal(
&gid_of_client_for_service0, &gid_of_first_client_for_service1, &are_equal);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
EXPECT_FALSE(are_equal);

are_equal = true;
ret = rmw_compare_gids_equal(
&gid_of_first_client_for_service1, &gid_of_second_client_for_service1, &are_equal);
EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str;
EXPECT_FALSE(are_equal);
}

0 comments on commit 8c3c133

Please sign in to comment.