unit test timeouts: first attempt

use an SDL_Alarm to try to abort the process if it goes over time.
This commit is contained in:
Chris Beck 2014-04-06 03:54:40 -04:00
parent 25e4e71e87
commit e5b3951054
4 changed files with 28 additions and 2 deletions

View file

@ -157,7 +157,8 @@ commandline_options::commandline_options ( int argc, char** argv ) :
("password", po::value<std::string>(), "uses <password> when connecting to a server, ignoring other preferences.")
("strict-validation", "makes validation errors fatal")
("test,t", po::value<std::string>()->implicit_value(std::string()), "runs the game in a small test scenario. If specified, scenario <arg> will be used instead.")
("unit,u", po::value<std::string>()->implicit_value(std::string()), "runs a unit test scenario. Works like test, except that the exit code of the program reflects the victory / defeat conditions of the scenario.")
("unit,u", po::value<std::string>()->implicit_value(std::string()), "runs a unit test scenario. Works like test, except that the exit code of the program reflects the victory / defeat conditions of the scenario.\n\t0 - PASS\n\t1 - FAIL\n\t2 - FAIL (TIMEOUT)")
("timeout", po::value<unsigned int>(), "sets a timeout (milliseconds) for the unit test. If unused there is no timeout or threading.")
("userconfig-dir", po::value<std::string>(), "sets the path of the user config directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory. Defaults to $HOME/.config/wesnoth on X11 and to the userdata-dir on other systems.")
("userconfig-path", "prints the path of the user config directory and exits.")
("userdata-dir", po::value<std::string>(), "sets the path of the userdata directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory.")
@ -385,6 +386,8 @@ commandline_options::commandline_options ( int argc, char** argv ) :
test = vm["test"].as<std::string>();
if (vm.count("unit"))
unit_test = vm["unit"].as<std::string>();
if (vm.count("timeout"))
timeout = vm["timeout"].as<unsigned int>();
if (vm.count("turns"))
multiplayer_turns = vm["turns"].as<std::string>();
if (vm.count("strict-validation"))

View file

@ -165,6 +165,8 @@ public:
boost::optional<std::string> test;
/// Non-empty if --unit was given on the command line. Goes directly into unit test mode, into a scenario, if specified.
boost::optional<std::string> unit_test;
/// Non-empty if --timeout was given on the command line. Dependent on --unit.
boost::optional<unsigned int> timeout;
/// True if --userconfig-path was given on the command line. Prints path to user config directory and exits.
bool userconfig_path;
/// Non-empty if --userconfig-dir was given on the command line. Sets the user config dir to the specified one.

View file

@ -247,8 +247,13 @@ game_controller::game_controller(const commandline_options& cmdline_opts, const
}
if (cmdline_opts_.unit_test)
{
if (!cmdline_opts_.unit_test->empty())
if (!cmdline_opts_.unit_test->empty()) {
test_scenario_ = *cmdline_opts_.unit_test;
if (cmdline_opts_.timeout) {
timeout = *cmdline_opts_.timeout;
}
}
}
if (cmdline_opts_.windowed)
preferences::set_fullscreen(false);
@ -439,6 +444,14 @@ bool game_controller::play_test()
return false;
}
unsigned int unit_test_timeout(unsigned int, void* param)
{
std::string * scen = (std::string *)param;
std::cerr << "Test timed out!" << std::endl;
std::cout << ("FAIL TEST (TIMEOUT): ") << *scen << std::endl;
exit(2);
}
// Same as play_test except that we return the results of play_game.
int game_controller::unit_test()
{
@ -459,6 +472,11 @@ int game_controller::unit_test()
resources::config_manager->
load_game_config_for_game(state_.classification());
if(cmdline_opts_.timeout && timeout > 0) {
std::cerr << "Adding timer for " << timeout << " ms." << std::endl;
SDL_AddTimer(timeout, unit_test_timeout, & test_scenario_);
}
try {
LEVEL_RESULT res = play_game(disp(),state_,resources::config_manager->game_config());
return ((res == VICTORY || res == NONE) ? 0 : 1);
@ -467,6 +485,8 @@ int game_controller::unit_test()
}
}
#undef Uint32
bool game_controller::play_screenshot_mode()
{
if(!cmdline_opts_.screenshot) {

View file

@ -107,6 +107,7 @@ private:
resize_monitor resize_monitor_;
std::string test_scenario_;
unsigned int timeout;
std::string screenshot_map_, screenshot_filename_;