Skip to content

Commit

Permalink
test: add verbose mode to the test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
OpatrilPeter committed Sep 24, 2024
1 parent 250c9ae commit b9492d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
19 changes: 14 additions & 5 deletions tests/db_access/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct Setting
std::string database{"test_superior_sqlpp"};
std::uint16_t port{0};
std::string containerId{""};
bool verbose{false};
};

inline Setting& getSettingsRef()
Expand All @@ -35,6 +36,10 @@ inline Setting& getSettingsRef()

inline void systemExecute(std::string command)
{
if (getSettingsRef().verbose)
{
std::cout << "Executing: " << command << std::endl;
}
auto returnCode = std::system(command.c_str());
if (returnCode != 0)
{
Expand All @@ -45,32 +50,36 @@ inline void systemExecute(std::string command)

inline void startMySql()
{
systemExecute("docker exec " + getSettingsRef().containerId + " supervisorctl start mysqld 1>/dev/null");
systemExecute("docker exec " + getSettingsRef().containerId + " supervisorctl start mysqld");
}

inline void stopMySql()
{
systemExecute("docker exec " + getSettingsRef().containerId + " supervisorctl stop mysqld 1>/dev/null");
systemExecute("docker exec " + getSettingsRef().containerId + " supervisorctl stop mysqld");
}

inline void restartMySql()
{
systemExecute("docker exec " + getSettingsRef().containerId + " supervisorctl restart mysqld 1>/dev/null");
systemExecute("docker exec " + getSettingsRef().containerId + " supervisorctl restart mysqld");
}

inline void waitForMySql()
{
auto& s = getSettingsRef();
while (true)
{
try
{
auto& s = getSettingsRef();
std::this_thread::sleep_for(std::chrono::milliseconds{100});
SuperiorMySqlpp::Connection connection{"", s.user, s.password, s.host, s.port};
}
catch (std::exception& e)
{
std::this_thread::sleep_for(std::chrono::milliseconds{50});
if (s.verbose)
{
std::cout << "Connecting to " << s.user << "@" << s.host << ":" << s.port << " failed: " << e.what() << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds{10});
continue;
}

Expand Down
14 changes: 12 additions & 2 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Singleton

void printUsage()
{
std::cerr << "Usage: ./tester <MySqlIpAddress> <MySqlPort> <MySqlContainerId> <LocalFwdSocket> [bandit-options...]" << std::endl;
std::cerr << "Usage: [VERBOSE=1] ./tester <MySqlIpAddress> <MySqlPort> <MySqlContainerId> <LocalFwdSocket> [bandit-options...]" << std::endl;
}

int main(int argc, char* argv[])
Expand All @@ -48,6 +48,13 @@ int main(int argc, char* argv[])
}

auto& s = getSettingsRef();
s.verbose = []{
if (char* varText = std::getenv("VERBOSE"))
{
return std::stoi(varText) != 0;
}
return false;
};
s.host = argv[1];
try
{
Expand All @@ -64,7 +71,10 @@ int main(int argc, char* argv[])

Singleton::getInstance();

// SuperiorMySqlpp::DefaultLogger::getModifiableInstance().setLoggerPtr(std::make_shared<SuperiorMySqlpp::Loggers::Full>());
if (s.verbose)
{
SuperiorMySqlpp::DefaultLogger::getModifiableInstance().setLoggerPtr(std::make_shared<SuperiorMySqlpp::Loggers::Full>());
}

std::cout << "Waiting for MySQL to become ready..." << std::endl;
waitForMySql();
Expand Down

0 comments on commit b9492d7

Please sign in to comment.