Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for bazel on garden #399

Merged
merged 10 commits into from
Dec 8, 2023
6 changes: 5 additions & 1 deletion log/src/Log_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*
*/
#include "gtest/gtest.h"

#include <chrono>
#include <ios>
#include <string>
#include <unordered_set>

#include "gz/transport/log/Log.hh"
#include "gtest/gtest.h"

#include "test_utils.hh"

Expand Down Expand Up @@ -255,13 +255,17 @@ TEST(Log, NullDescriptorUnopenedLog)
TEST(Log, OpenCorruptDatabase)
{
log::Log logFile;
<<<<<<< HEAD
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
std::string path =
testing::portablePathUnion(GZ_TRANSPORT_LOG_TEST_PATH, "data");
path = testing::portablePathUnion(path, "state.tlog");

std::cout << path << std::endl;

logFile.Open(path);
=======
logFile.Open(kCorruptDbTestPath);
>>>>>>> gz-transport12
EXPECT_GT(logFile.EndTime(), 0ns) << "logFile.EndTime() == "
<< logFile.EndTime().count() << "ns";;
}
3 changes: 3 additions & 0 deletions src/CIface_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
* limitations under the License.
*
*/
#include "gtest/gtest.h"

#include <gz/msgs/stringmsg.pb.h>

#include "gz/transport/CIface.h"

#include "test_utils.hh"
#include <gz/utils/Environment.hh>

static int count;

Expand Down
5 changes: 4 additions & 1 deletion src/Discovery_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
* limitations under the License.
*
*/
#include "gtest/gtest.h"

#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>

#include "gtest/gtest.h"
#include "gz/transport/AdvertiseOptions.hh"
#include "gz/transport/Discovery.hh"
#include "gz/transport/Publisher.hh"
#include "gz/transport/TransportTypes.hh"
#include "gz/transport/Uuid.hh"

#include "test_utils.hh"
#include "gz/utils/Environment.hh"
#include "gz/utils/ExtraTestMacros.hh"


using namespace gz;
using namespace transport;
Expand Down
4 changes: 3 additions & 1 deletion src/Helpers_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* limitations under the License.
*
*/
#include "gtest/gtest.h"

#include "gz/transport/Helpers.hh"
#include "test_utils.hh"
#include "gtest/gtest.h"

#include <gz/utils/Environment.hh>

using namespace gz;

Expand Down
5 changes: 4 additions & 1 deletion src/NodeOptions_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*
*/

#include "gtest/gtest.h"

#include <string>

#include "gz/transport/NetUtils.hh"
#include "gz/transport/NodeOptions.hh"
#include "gtest/gtest.h"

#include <gz/utils/Environment.hh>

using namespace gz;

Expand Down
9 changes: 4 additions & 5 deletions src/Node_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*
*/
#include "gtest/gtest.h"

#include <gz/msgs/int32.pb.h>
#include <gz/msgs/stringmsg.pb.h>
#include <gz/msgs/vector3d.pb.h>
Expand All @@ -26,14 +28,11 @@
#include <string>
#include <thread>

#include "gz/transport/AdvertiseOptions.hh"
#include "gz/transport/MessageInfo.hh"
#include "gz/transport/Node.hh"
#include "gz/transport/NodeOptions.hh"
#include "gz/transport/TopicStatistics.hh"
#include "gz/transport/TopicUtils.hh"
#include "gz/transport/TransportTypes.hh"

#include <gz/utils/Environment.hh>

#include "test_utils.hh"

using namespace gz;
Expand Down
13 changes: 5 additions & 8 deletions src/cmd/gz_src_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@
* limitations under the License.
*
*/
#include "gtest/gtest.h"

#include <gz/msgs/int32.pb.h>

#include <future>
#include <string>
#include <iostream>
#include <sstream>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251)
#endif
#include <gz/msgs/int32.pb.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include "gz.hh"
#include "gz/transport/Node.hh"

#include <gz/utils/Environment.hh>

using namespace gz;

// Global constants.
Expand Down
10 changes: 1 addition & 9 deletions test/test_config.hh.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@
#define GZ_CONFIG_PATH "@CMAKE_BINARY_DIR@/test/conf"
#define GZ_VERSION_FULL "@PROJECT_VERSION_FULL@"

#ifdef DETAIL_GZ_TRANSPORT_TEST_DIR
// The DETAIL_GZ_TRANSPORT_TEST_DIR macro is defined using generator
// expressions in CMakeLists.txt files. See test/integration/CMakeLists.txt for
// an example.
#define GZ_TRANSPORT_TEST_DIR \
DETAIL_GZ_TRANSPORT_TEST_DIR
#endif

#endif // header guard
#endif // GZ_TRANSPORT_TEST_CONFIG_HH_
152 changes: 4 additions & 148 deletions test/test_utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,159 +19,14 @@
#define GZ_TRANSPORT_TEST_UTILS_HH_

#include <climits>
#include <filesystem>
#include <iostream>
#include <random>
#include <string>

#ifdef _WIN32
#include <windows.h>
#else
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif

#include "gz/transport/Helpers.hh"

namespace testing
{
/// \brief Join _str1 and _str2 considering both as storing system paths.
/// \param[in] _str1 string containing a path.
/// \param[in] _str2 string containing a path.
/// \return The string representation of the union of two paths.
std::string portablePathUnion(const std::string &_str1,
const std::string &_str2)
{
return (std::filesystem::path(_str1) / std::filesystem::path(_str2)).string();
}

#ifdef _WIN32
using forkHandlerType = PROCESS_INFORMATION;
#else
using forkHandlerType = pid_t;
#endif

/// \brief create a new process and run command on it. This function is
/// implementing the creation of a new process on both Linux (fork) and
/// Windows (CreateProcess) and the execution of the command provided.
/// \param[in] _command The full system path to the binary to run into the
/// new process.
/// \param[in] _partition Name of the Gazebo partition (GZ_PARTITION)
/// \param[in] _username Username for authentication
/// (GZ_TRANSPORT_USERNAME)
/// \param[in] _password Password for authentication
/// (GZ_TRANSPORT_PASSWORD)
/// \return On success, the PID of the child process is returned in the
/// parent, an 0 is returned in the child. On failure, -1 is returned in the
/// parent and no child process is created.
forkHandlerType forkAndRun(const char *_command, const char *_partition,
const char *_username = nullptr, const char *_password = nullptr)
{
#ifdef _WIN32
STARTUPINFO info= {sizeof(info)};
PROCESS_INFORMATION processInfo;

char cmd[500];
// We should put quotes around the _command string to make sure we are
// robust to file paths that contain spaces.
gz_strcpy(cmd, "\"");
gz_strcat(cmd, _command);
gz_strcat(cmd, "\"");
gz_strcat(cmd, " ");
gz_strcat(cmd, _partition);

if (_username && _password)
{
gz_strcat(cmd, " ");
gz_strcat(cmd, _username);
gz_strcat(cmd, " ");
gz_strcat(cmd, _password);
}

// We set the first argument to NULL, because we want the behavior that
// CreateProcess exhibits when the first argument is NULL: i.e. Windows will
// automatically add the .exe extension onto the filename. When the first
// argument is non-NULL, it will not automatically add the extension, which
// makes more work for us.
//
// It should also be noted that the lookup behavior for the application is
// different when the first argument is non-NULL, so we should take that
// into consideration when determining what to put into the first and second
// arguments of CreateProcess.
if (!CreateProcess(NULL, const_cast<LPSTR>(cmd), NULL, NULL,
TRUE, 0, NULL, NULL, &info, &processInfo))
{
std::cerr << "CreateProcess call failed: " << cmd << std::endl;
}

return processInfo;
#else
pid_t pid = fork();

if (pid == 0)
{
if (_username && _password)
{
if (execl(_command, _command, _partition, _username, _password,
reinterpret_cast<char *>(0)) == -1)
{
std::cerr << "Error running execl call: " << _command << std::endl;
}
}
else
{
if (execl(_command, _command, _partition,
reinterpret_cast<char *>(0)) == -1)
{
std::cerr << "Error running execl call: " << _command << std::endl;
}
}
}

return pid;
#endif
}

/// \brief Wait for the end of a process and handle the termination
/// \param[in] pi Process handler of the process to wait for
/// (PROCESS_INFORMATION in windows or forkHandlerType in UNIX).
void waitAndCleanupFork(const forkHandlerType pi)
{
#ifdef _WIN32
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);

// Close process and thread handler.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
#else
// Wait for the child process to return.
int status;
waitpid(pi, &status, 0);
if (status == -1)
std::cerr << "Error while running waitpid" << std::endl;
#endif
}

/// \brief Send a termination signal to the process handled by pi.
/// \param[in] pi Process handler of the process to stop
/// (PROCESS_INFORMATION in windows or forkHandlerType in UNIX).
void killFork(const forkHandlerType pi)
{
#ifdef _WIN32
// TerminateProcess return 0 on error
if (TerminateProcess(pi.hProcess, 0) == 0)
std::cerr << "Error running TerminateProcess: " << GetLastError();
#else
kill(pi, SIGTERM);
#endif
}

/// \brief Get a random number based on an integer converted to string.
/// \return A random integer converted to string.
std::string getRandomNumber()
inline std::string getRandomNumber()
{
// Initialize random number generator.
uint32_t seed = std::random_device {}();
Expand All @@ -182,6 +37,7 @@ namespace testing

return std::to_string(d(randGenerator));
}
}
} // namespace testing

#endif // GZ_TRANSPORT_TEST_UTILS_HH_

#endif // header guard
You are viewing a condensed version of this merge commit. You can view the full changes here.