Skip to content

Commit

Permalink
Refs #22518: Cancel fork in cpp tool
Browse files Browse the repository at this point in the history
Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>
  • Loading branch information
cferreiragonz committed Jan 9, 2025
1 parent b897f34 commit 51c837c
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 262 deletions.
131 changes: 66 additions & 65 deletions test/system/tools/fds/CliDiscoveryManagerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,71 +835,72 @@ TEST_F(CliDiscoveryManagerTest, GetLocalServers)
}
}

TEST_F(CliDiscoveryManagerTest, StartServerInBackground)
{
DomainId_t d0 = 0;
ASSERT_FALSE(manager.isServerRunning(d0));
uint16_t p0 = manager.getDiscoveryServerPort(d0);
pid_t child_pid = manager.startServerInBackground(p0, d0, false);
EXPECT_GT(child_pid, 0);
EXPECT_TRUE(manager.isServerRunning(d0));
EXPECT_EQ(manager.getPidOfServer(p0), child_pid);

// Manually stop the child process
kill(child_pid, SIGTERM);
int status;
waitpid(child_pid, &status, 0);
EXPECT_FALSE(manager.isServerRunning(d0));
EXPECT_EQ(manager.getPidOfServer(p0), 0);
}

// This test verify that the fastdds_discovery_auto and fastdds_discovery_stop functions work as expected
// Other methods are not tested here, but in the discovery_server repo as their functionality is not critical
TEST_F(CliDiscoveryManagerTest, StartAndStopServers)
{
DomainId_t d0 = 0;
uint16_t p0 = manager.getDiscoveryServerPort(d0);
ASSERT_FALSE(manager.isServerRunning(d0));

const char* argv[] = {"-d", "0"};
createOptionsAndParser(2, argv);
manager.fastdds_discovery_auto(options, parse);
std::this_thread::sleep_for(std::chrono::seconds(1));
EXPECT_TRUE(manager.isServerRunning(d0));
EXPECT_GT(manager.getPidOfServer(p0), 0);
pid_t child_pid = manager.getPidOfServer(p0);

// Manually stop the child process
createOptionsAndParser(2, argv);
EXPECT_EQ(manager.fastdds_discovery_stop(options, parse), 0);

int status;
waitpid(child_pid, &status, 0);
EXPECT_FALSE(manager.isServerRunning(d0));
EXPECT_EQ(manager.getPidOfServer(p0), 0);
}

// This test verify that the fastdds_discovery_stop is capable of parsing the 'all' argument.
// The behavior of stopping all servers is tested in the discovery_server repo
TEST_F(CliDiscoveryManagerTest, StopAllRunningServers)
{
// Start server 1
DomainId_t d1 = 1;
ASSERT_FALSE(manager.isServerRunning(d1));
const char* argv_0[] = {"-d", "1"};
createOptionsAndParser(2, argv_0);
manager.fastdds_discovery_auto(options, parse);
std::this_thread::sleep_for(std::chrono::seconds(1));

ASSERT_TRUE(manager.isServerRunning(d1));

// Stop server with all
const char* argv_stop[] = {"all"};
createOptionsAndParser(1, argv_stop);
EXPECT_EQ(manager.fastdds_discovery_stop(options, parse), 0);
std::this_thread::sleep_for(std::chrono::seconds(1));
ASSERT_FALSE(manager.isServerRunning(d1));
}
// TODO (Carlos): Update tests to handle the new StartServerInBackground which does not daemonize itself
// TEST_F(CliDiscoveryManagerTest, StartServerInBackground)
// {
// DomainId_t d0 = 0;
// ASSERT_FALSE(manager.isServerRunning(d0));
// uint16_t p0 = manager.getDiscoveryServerPort(d0);
// pid_t child_pid = manager.startServerInBackground(p0, d0, false);
// EXPECT_GT(child_pid, 0);
// EXPECT_TRUE(manager.isServerRunning(d0));
// EXPECT_EQ(manager.getPidOfServer(p0), child_pid);

// // Manually stop the child process
// kill(child_pid, SIGTERM);
// int status;
// waitpid(child_pid, &status, 0);
// EXPECT_FALSE(manager.isServerRunning(d0));
// EXPECT_EQ(manager.getPidOfServer(p0), 0);
// }

// // This test verify that the fastdds_discovery_auto and fastdds_discovery_stop functions work as expected
// // Other methods are not tested here, but in the discovery_server repo as their functionality is not critical
// TEST_F(CliDiscoveryManagerTest, StartAndStopServers)
// {
// DomainId_t d0 = 0;
// uint16_t p0 = manager.getDiscoveryServerPort(d0);
// ASSERT_FALSE(manager.isServerRunning(d0));

// const char* argv[] = {"-d", "0"};
// createOptionsAndParser(2, argv);
// manager.fastdds_discovery_auto(options, parse);
// std::this_thread::sleep_for(std::chrono::seconds(1));
// EXPECT_TRUE(manager.isServerRunning(d0));
// EXPECT_GT(manager.getPidOfServer(p0), 0);
// pid_t child_pid = manager.getPidOfServer(p0);

// // Manually stop the child process
// createOptionsAndParser(2, argv);
// EXPECT_EQ(manager.fastdds_discovery_stop(options, parse), 0);

// int status;
// waitpid(child_pid, &status, 0);
// EXPECT_FALSE(manager.isServerRunning(d0));
// EXPECT_EQ(manager.getPidOfServer(p0), 0);
// }

// // This test verify that the fastdds_discovery_stop is capable of parsing the 'all' argument.
// // The behavior of stopping all servers is tested in the discovery_server repo
// TEST_F(CliDiscoveryManagerTest, StopAllRunningServers)
// {
// // Start server 1
// DomainId_t d1 = 1;
// ASSERT_FALSE(manager.isServerRunning(d1));
// const char* argv_0[] = {"-d", "1"};
// createOptionsAndParser(2, argv_0);
// manager.fastdds_discovery_auto(options, parse);
// std::this_thread::sleep_for(std::chrono::seconds(1));

// ASSERT_TRUE(manager.isServerRunning(d1));

// // Stop server with all
// const char* argv_stop[] = {"all"};
// createOptionsAndParser(1, argv_stop);
// EXPECT_EQ(manager.fastdds_discovery_stop(options, parse), 0);
// std::this_thread::sleep_for(std::chrono::seconds(1));
// ASSERT_FALSE(manager.isServerRunning(d1));
// }
#endif // _WIN32

int main(
Expand Down
2 changes: 1 addition & 1 deletion tools/fastdds/discovery/fast_daemon/daemon/daemonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def daemonize(callable_, tags={}, timeout=None, debug=False):
# Spawn child process
env = os.environ.copy()
env['PYTHONPATH'] = ':'.join(sys.path)
process = subprocess.Popen(cmd, env=env, **kwargs)
process = subprocess.Popen(cmd, env=env, **kwargs, preexec_fn=os.setsid)

# Send serialized callable object through stdin pipe
pickler = PicklerForProcess(process)
Expand Down
4 changes: 2 additions & 2 deletions tools/fds/CliDiscoveryEntrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ int main (
switch (command_int)
{
case ToolCommand::AUTO:
return cli_manager.fastdds_discovery_auto(options, parse);
return cli_manager.fastdds_discovery_auto_start(options, parse);
break;
case ToolCommand::START:
return cli_manager.fastdds_discovery_start(options, parse);
return cli_manager.fastdds_discovery_auto_start(options, parse);
break;
case ToolCommand::STOP:
return cli_manager.fastdds_discovery_stop(options, parse);
Expand Down
Loading

0 comments on commit 51c837c

Please sign in to comment.