1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
#include "utils/test_server.h"
#include "utils/utils.h"
#include "utils/platform/platform.h"
#include <catch2/catch_get_random_seed.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/reporters/catch_reporter_event_listener.hpp>
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <random>
#include <sstream>
namespace TangoTest
{
namespace
{
class CatchLogger : public Logger
{
public:
void log(const std::string &message) override
{
TANGO_LOG_WARN << message;
WARN(message);
}
~CatchLogger() override { }
};
struct FilenameBuilder
{
// The current test case we are running
std::string current_test_case_name;
// The current test case part that we are running
uint64_t current_part_number;
// Number of servers launch for this part
uint64_t server_count;
std::string build()
{
std::string suffix = [this]()
{
std::stringstream ss;
ss << "_prt" << current_part_number;
ss << "_srv" << server_count;
return ss.str();
}();
server_count += 1;
std::string filename = detail::filename_from_test_case_name(current_test_case_name, suffix);
std::string path = [&]()
{
std::stringstream ss;
ss << TangoTest::platform::k_output_directory_path << "/" << filename;
return ss.str();
}();
return path;
}
};
constexpr int k_min_port = 10000;
constexpr int k_max_port = 20000;
std::minstd_rand g_rng;
FilenameBuilder g_filename_builder;
std::vector<int> g_used_ports; // Work around for #1230
class PlatformListener : public Catch::EventListenerBase
{
using Catch::EventListenerBase::EventListenerBase;
void testRunStarting(Catch::TestRunInfo const &) override
{
g_rng.seed(Catch::getSeed());
TestServer::s_logger = std::make_unique<CatchLogger>();
{
std::uniform_int_distribution dist{k_min_port, k_max_port};
TestServer::s_next_port = dist(g_rng);
}
platform::init();
}
void testCasePartialStarting(Catch::TestCaseInfo const &info, uint64_t part_number) override
{
g_filename_builder.current_test_case_name = info.name;
g_filename_builder.current_part_number = part_number;
g_filename_builder.server_count = 0;
}
};
CATCH_REGISTER_LISTENER(PlatformListener)
/** Append the logs from the in stream to the out stream
*/
bool append_logs(std::istream &in, std::ostream &out)
{
for(std::string line; std::getline(in, line);)
{
out << "\t" << line << "\n";
}
return false;
}
[[noreturn]] void throw_runtime_error(const std::string &message)
{
TANGO_LOG_ERROR << message;
throw std::runtime_error(message);
}
void remove_file(const std::string &filename)
{
if(std::remove(filename.c_str()) != 0)
{
TANGO_LOG_WARN << "Failed to remove \"" << filename << "\": " << strerror(errno);
}
}
} // namespace
int TestServer::s_next_port;
std::unique_ptr<Logger> TestServer::s_logger;
std::ostream &operator<<(std::ostream &os, const ExitStatus &status)
{
using Kind = ExitStatus::Kind;
switch(status.kind)
{
case Kind::Normal:
os << status.code;
break;
case Kind::Aborted:
os << "(Aborted by signal " << status.signal << ")";
break;
case Kind::AbortedNoSignal:
os << "(Aborted)";
}
return os;
}
void TestServer::start(const std::string &instance_name,
const std::vector<std::string> &extra_args,
const std::vector<std::string> &extra_env,
std::chrono::milliseconds timeout)
{
using Kind = platform::StartServerResult::Kind;
TANGO_ASSERT(!is_running());
TANGO_ASSERT(instance_name != "");
m_redirect_file = g_filename_builder.build();
std::vector<std::string> args{
TANGO_TEST_CATCH2_SERVER_BINARY_NAME,
instance_name.c_str(),
"-ORBendPoint",
"", // filled in later
};
for(const auto &arg : extra_args)
{
args.push_back(arg);
}
std::vector<std::string> env = platform::default_env();
for(const auto &e : extra_env)
{
env.push_back(e);
}
// This will point to the slot after "-ORBendPoint"
auto end_point_slot = std::find(args.begin(), args.end(), "");
int num_tries = k_num_port_tries;
// We are restarting the server on the same port and we want to fail if we
// cannot get that port.
if(m_port != -1)
{
s_next_port = m_port;
num_tries = 1;
}
std::uniform_int_distribution dist{k_min_port, k_max_port};
for(int i = 0; i < num_tries; ++i)
{
if(i != 0)
{
std::stringstream ss;
ss << "Port " << m_port << " in use. Retrying...";
s_logger->log(ss.str());
}
// We cannot reuse a port that the ORB has already connected to for a
// new DeviceProxy due to #1230.
do
{
m_port = dist(g_rng);
std::swap(m_port, s_next_port);
} while(std::find(g_used_ports.begin(), g_used_ports.end(), m_port) != g_used_ports.end());
*end_point_slot = [&]()
{
std::stringstream ss;
ss << "giop:tcp::" << m_port;
return ss.str();
}();
TANGO_LOG_INFO << "Starting server with arguments "
<< Catch::StringMaker<std::vector<std::string>>::convert(args) << " and environment "
<< Catch::StringMaker<std::vector<std::string>>::convert(env);
auto start_result = platform::start_server(args, env, m_redirect_file, k_ready_string, timeout);
switch(start_result.kind)
{
case Kind::Started:
{
m_handle = start_result.handle;
return;
}
case Kind::Timeout:
{
std::stringstream ss;
ss << "Timeout waiting for TestServer to start. Server output:\n";
std::ifstream f{m_redirect_file};
m_handle = start_result.handle;
append_logs(f, ss);
throw_runtime_error(ss.str());
}
case Kind::Exited:
{
std::stringstream ss;
ss << "TestServer exited with exit status " << start_result.exit_status << ". Server output:\n";
std::ifstream f{m_redirect_file};
bool port_in_use = false;
for(std::string line; std::getline(f, line);)
{
if(line.find(k_port_in_use_string) != std::string::npos)
{
port_in_use = true;
break;
}
ss << "\t" << line << "\n";
}
f.close();
remove_file(m_redirect_file);
if(!port_in_use)
{
throw_runtime_error(ss.str());
}
}
}
}
}
TestServer::~TestServer()
{
if(is_running())
{
try
{
stop();
}
catch(std::exception &e)
{
std::stringstream ss;
ss << "TestServer::stop() threw an exception during teardown: " << e.what();
s_logger->log(ss.str());
}
}
g_used_ports.push_back(m_port);
}
void TestServer::stop(std::chrono::milliseconds timeout)
{
if(m_handle == nullptr)
{
return;
}
struct CombinedResult
{
enum Kind
{
Timeout, // exit_status undefined
ExitedEarlyUnexpected,
ExitedEarlyExpected,
Exited,
};
Kind kind;
ExitStatus exit_status;
};
using Kind = CombinedResult::Kind;
CombinedResult result;
{
if(m_exit_status.has_value())
{
result.kind = Kind::ExitedEarlyExpected;
result.exit_status = *m_exit_status;
}
using StopKind = platform::StopServerResult::Kind;
using WaitKind = platform::WaitForStopResult::Kind;
auto stop_result = platform::stop_server(m_handle);
if(stop_result.kind == StopKind::Exiting)
{
auto wait_result = platform::wait_for_stop(m_handle, timeout);
if(wait_result.kind == WaitKind::Timeout)
{
result.kind = Kind::Timeout;
}
else
{
result.kind = Kind::Exited;
result.exit_status = wait_result.exit_status;
}
}
else
{
result.kind = Kind::ExitedEarlyUnexpected;
result.exit_status = stop_result.exit_status;
}
}
switch(result.kind)
{
case Kind::Timeout:
{
std::stringstream ss;
ss << "Timeout waiting for TestServer to exit. Server output:\n";
std::ifstream f{m_redirect_file};
append_logs(f, ss);
s_logger->log(ss.str());
break;
}
case Kind::ExitedEarlyUnexpected:
[[fallthrough]];
case Kind::ExitedEarlyExpected:
[[fallthrough]];
case Kind::Exited:
{
// `test_has_failed == true` if we are currently stopping the server
// in some dtor while there is an uncaught exception in flight. This
// means either:
// - the code under test has throw an exception; or
// - some assertion has failed (where Catch2 will throw an exception)
// In either case the test has failed.
bool test_has_failed = std::uncaught_exceptions() > 0;
bool exited_early = result.kind == Kind::ExitedEarlyExpected || result.kind == Kind::ExitedEarlyUnexpected;
std::stringstream ss;
if(exited_early || !result.exit_status.is_success())
{
ss << "TestServer exited with exit status " << result.exit_status << " during the test. Server output:\n";
}
else if(test_has_failed)
{
ss << "Test server exited cleanly, but we detected that test failed. Server output:\n";
}
else
{
ss << "Test server exited cleanly. Server output:\n";
}
std::ifstream f{m_redirect_file};
append_logs(f, ss);
// When we are ExitedEarlyExpected then the test knows what the exit
// status is. So, if it hasn't failed the test, then the exit code isn't
// suspicious even if it non-zero.
bool suspicious_exit_status = !result.exit_status.is_success() && result.kind != Kind::ExitedEarlyExpected;
if(result.kind == Kind::ExitedEarlyUnexpected || suspicious_exit_status || test_has_failed)
{
s_logger->log(ss.str());
}
else
{
TANGO_LOG_INFO << ss.str();
}
break;
}
}
remove_file(m_redirect_file.c_str());
m_handle = nullptr;
m_redirect_file = "";
m_exit_status = std::nullopt;
}
ExitStatus TestServer::wait_for_exit(std::chrono::milliseconds timeout)
{
TANGO_ASSERT(is_running());
using Kind = platform::WaitForStopResult::Kind;
auto result = platform::wait_for_stop(m_handle, timeout);
if(result.kind == Kind::Timeout)
{
throw std::runtime_error("Timeout exceeded");
}
// We don't report the contents of the redirect file here as don't know if
// the test has failed or not at this point and we want to WARN with the
// server output if it has.
//
// We save the m_exit_status for later so we can report it during stop.
m_exit_status = result.exit_status;
return result.exit_status;
}
} // namespace TangoTest
|