Skip to content

Commit

Permalink
Refs #22518: Improve unittest robustness
Browse files Browse the repository at this point in the history
Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>
  • Loading branch information
cferreiragonz committed Jan 22, 2025
1 parent 321e284 commit 06e6282
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 44 deletions.
109 changes: 68 additions & 41 deletions test/system/tools/fds/CliDiscoveryManagerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,70 @@ TEST_F(CliDiscoveryManagerTest, ConfigureTransports)
}

#ifndef _WIN32
// This test verifies that the get_listening_ports method returns *at least* the ports we know that
// are being used by the servers. We cannot check the exact number of ports because the number of
// ports is not fixed and depends on the number of TCP connection of the host.
TEST_F(CliDiscoveryManagerTest, GetListeningPorts)
{
// Check that the ports are not being used
std::vector<uint16_t> ports = manager.real_get_listening_ports();
bool port_7402_7652 = false;
for (const uint16_t port : ports)
{
port_7402_7652 |= (port == 7402 || port == 7652);
}
ASSERT_FALSE(port_7402_7652);

addServers(test_case_map.at("tcp_2_ip_2_port"));
manager.configure_transports();
DomainParticipant* server = DomainParticipantFactory::get_instance()->create_participant(0,
manager.getServerQos());

ports.clear();
ports = manager.real_get_listening_ports();
bool port_7402 = false;
bool port_7652 = false;
for (const uint16_t port : ports)
{
port_7402 |= (port == 7402);
port_7652 |= (port == 7652);
}
EXPECT_TRUE(port_7402);
EXPECT_TRUE(port_7652);
ASSERT_EQ(DomainParticipantFactory::get_instance()->delete_participant(server), RETCODE_OK);
}

// This test also checks:
// - get_listening_ports method, which is used in the get_local_servers method.
// - is_server_running method, which checks if a server is running in a specific domain.
// - get_pid_of_server method, which returns the pid of the running server in the specified port.
TEST_F(CliDiscoveryManagerTest, GetLocalServers)
{
// Lambda to verify servers
auto verify_servers = [&](const std::vector<MetaInfo_DS>& servers,
std::vector<uint16_t> expected_ports,
std::vector<uint32_t> expected_domains)
{
for (const MetaInfo_DS& server : servers)
{
auto it = std::find(expected_ports.begin(), expected_ports.end(), server.port);
EXPECT_NE(it, expected_ports.end()) << "Expected port not found: " << server.port;
if (it != expected_ports.end())
{
expected_ports.erase(it);
}
auto it_d = std::find(expected_domains.begin(), expected_domains.end(), server.domain_id);
EXPECT_NE(it_d, expected_domains.end()) << "Expected domain not found: " << server.domain_id;
if (it_d != expected_domains.end())
{
expected_domains.erase(it_d);
}
EXPECT_TRUE(manager.is_server_running(server.domain_id));
}
EXPECT_EQ(expected_ports.size(), 0);
EXPECT_EQ(expected_domains.size(), 0);
};

// Use MockCliDiscoveryManager overwritten method to get the listening ports
std::vector<MetaInfo_DS> servers = manager.get_local_servers();
EXPECT_TRUE(servers.empty());

Expand All @@ -729,24 +787,15 @@ TEST_F(CliDiscoveryManagerTest, GetLocalServers)
uint16_t port = port_params.get_discovery_server_port(domain);
ASSERT_FALSE(manager.is_server_running(domain));
EXPECT_EQ(manager.get_pid_of_server(port), 0);
addServers(test_case_map.at("tcp_1_ip_1_port"));
manager.configure_transports();
DomainParticipant* server = DomainParticipantFactory::get_instance()->create_participant(0,
manager.getServerQos());
// Simulate the creation of the DS with the mocked listening port
manager.mocked_ports.push_back(7402);
servers = manager.get_local_servers();
EXPECT_EQ(servers.size(), 1);
for (const MetaInfo_DS& server : servers)
{
EXPECT_EQ(server.domain_id, domain);
EXPECT_EQ(server.port, port);
}
EXPECT_TRUE(manager.is_server_running(domain));
EXPECT_GT(manager.get_pid_of_server(port), 0);
ASSERT_EQ(DomainParticipantFactory::get_instance()->delete_participant(server), RETCODE_OK);
EXPECT_FALSE(manager.is_server_running(domain));
verify_servers(servers, {port}, {domain});
}

// Multiple servers
manager.mocked_ports.clear();
{
DomainId_t d0 = 0;
DomainId_t d1 = 1;
Expand All @@ -757,34 +806,12 @@ TEST_F(CliDiscoveryManagerTest, GetLocalServers)
ASSERT_FALSE(manager.is_server_running(d1));
EXPECT_EQ(manager.get_pid_of_server(p0), 0);
EXPECT_EQ(manager.get_pid_of_server(p1), 0);
addServers(test_case_map.at("tcp_2_ip_2_port"));
manager.configure_transports();
DomainParticipant* server = DomainParticipantFactory::get_instance()->create_participant(0,
manager.getServerQos());
// Simulate the creation of the DS with the mocked listening port
manager.mocked_ports.push_back(7402);
manager.mocked_ports.push_back(7652);
servers = manager.get_local_servers();
EXPECT_EQ(servers.size(), 2);
std::vector<uint16_t> expected_ports({p0, p1});
std::vector<uint32_t> expected_domains({d0, d1});
for (const MetaInfo_DS& server : servers)
{
auto it = std::find(expected_ports.begin(), expected_ports.end(), server.port);
EXPECT_NE(it, expected_ports.end());
if (it != expected_ports.end())
{
expected_ports.erase(it);
}
auto it_d = std::find(expected_domains.begin(), expected_domains.end(), server.domain_id);
EXPECT_NE(it_d, expected_domains.end());
if (it_d != expected_domains.end())
{
expected_domains.erase(it_d);
}
EXPECT_TRUE(manager.is_server_running(server.domain_id));
EXPECT_GT(manager.get_pid_of_server(server.port), 0);
}
EXPECT_EQ(expected_ports.size(), 0);
EXPECT_EQ(expected_domains.size(), 0);
ASSERT_EQ(DomainParticipantFactory::get_instance()->delete_participant(server), RETCODE_OK);
verify_servers(servers, {p0, p1}, {d0, d1});
}
}
#endif // _WIN32
Expand All @@ -793,6 +820,6 @@ int main(
int argc,
char** argv)
{
::testing::InitGoogleTest(&argc, argv);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
16 changes: 14 additions & 2 deletions test/system/tools/fds/MockCliDiscoveryManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>

#include <gmock/gmock.h>

using namespace eprosima::fastdds::dds;
using namespace eprosima::fastdds::rtps;
using namespace eprosima::option;
Expand All @@ -34,6 +32,19 @@ class MockCliDiscoveryManager : public CliDiscoveryManager
{
public:

#ifndef _WIN32
std::vector<uint16_t> get_listening_ports() override
{
return mocked_ports;
}

std::vector<uint16_t> real_get_listening_ports()
{
return CliDiscoveryManager::get_listening_ports();
}

#endif // _WIN32

DomainParticipantQos getServerQos()
{
return serverQos;
Expand Down Expand Up @@ -73,6 +84,7 @@ class MockCliDiscoveryManager : public CliDiscoveryManager
serverQos = DomainParticipantQos();
}

std::vector<uint16_t> mocked_ports;
};
} // namespace dds
} // namespace fastdds
Expand Down
2 changes: 1 addition & 1 deletion tools/fds/CliDiscoveryManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class CliDiscoveryManager
* @brief Get the listening TCP ports of the machine.
* @return An ordered vector with the listening ports
*/
std::vector<uint16_t> get_listening_ports();
virtual std::vector<uint16_t> get_listening_ports();

/**
* @brief Get the local Discovery Servers running in the machine.
Expand Down

0 comments on commit 06e6282

Please sign in to comment.