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
|
/*
* Copyright 2009- ECMWF.
*
* This software is licensed under the terms of the Apache Licence version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/
#include <iostream>
#include <boost/test/unit_test.hpp>
#include "TestUtil.hpp"
#include "ecflow/attribute/VerifyAttr.hpp"
#include "ecflow/core/Chrono.hpp"
#include "ecflow/core/Filesystem.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Family.hpp"
#include "ecflow/node/Suite.hpp"
#include "ecflow/node/Task.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"
#include "ecflow/simulator/Simulator.hpp"
using namespace ecf;
/// Simulate definition files that are created on then fly. This allows us to create
/// tests with todays date/time this speeds up the testr, we can also validate
/// Defs file, to check for correctness
BOOST_AUTO_TEST_SUITE(S_Simulator)
BOOST_AUTO_TEST_SUITE(T_Time)
BOOST_AUTO_TEST_CASE(test_time) {
std::cout << "Simulator:: ...test_time\n";
// # Note: we have to use relative paths, since these tests are relocatable
// suite suite
// clock real <todays date>
// family family
// task t1
// time <start>
// endfamily
// endsuite
// Initialise clock with todays date then create a time attribute + minutes
// such that the task should only run once, in the next minute
auto theLocalTime = Calendar::second_clock_time();
auto time_plus_minute = theLocalTime + boost::posix_time::minutes(1);
Defs theDefs;
{
suite_ptr suite = theDefs.add_suite("test_time");
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
task->addTime(ecf::TimeAttr(TimeSlot(time_plus_minute.time_of_day())));
task->addVerify(VerifyAttr(NState::COMPLETE, 1)); // expect task to complete 1 time
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(simulator.run(theDefs, findTestDataLocation("test_time.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_time.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_time_series) {
std::cout << "Simulator:: ...test_time_series\n";
// suite suite
// clock real <sunday>
// family family
// task t1
// time 00:30 23:59 04:00 # should run 6 times 00:30 4:30 8:30 12:30 16:30 20:30
// endfamily
// endsuite
// Initialise real clock on a Moday, such that task should _only_ run
// on Monday since we are using a hybrid clock
Defs theDefs;
{
ClockAttr clockAttr(true /*false means use hybrid clock*/);
clockAttr.date(12, 10, 2009); // 12 October 2009 was a Monday
suite_ptr suite = theDefs.add_suite("test_time_series");
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
TimeSeries timeSeries(TimeSlot(00, 30), TimeSlot(23, 59), TimeSlot(4, 0), false /* relative */);
task->addTime(TimeAttr(timeSeries));
task->addVerify(VerifyAttr(NState::COMPLETE, 6)); // expect task to complete 6 times
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(simulator.run(theDefs, findTestDataLocation("test_time_series.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_time_series.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_time_and_date) {
std::cout << "Simulator:: ...test_time_and_date\n";
// # Note: we have to use relative paths, since these tests are relocatable
// suite suite
// clock real <todays date>
// family family
// task t1
// date <today date>
// time <start>
// endfamily
// endsuite
Defs theDefs;
{
// Initialise clock with todays date then create a time attribute + minutes
// such that the task should only run once, in the next minute
auto theLocalTime = Calendar::second_clock_time();
auto todaysDate = theLocalTime.date();
auto time_plus_minute = theLocalTime + boost::posix_time::minutes(1);
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite_ptr suite = theDefs.add_suite("test_time_and_date");
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
task->addDate(DateAttr(todaysDate));
task->addTime(ecf::TimeAttr(TimeSlot(time_plus_minute.time_of_day())));
task->addVerify(VerifyAttr(NState::COMPLETE, 1)); // expect task to complete 1 time
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(simulator.run(theDefs, findTestDataLocation("test_time_and_date.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_time_and_date.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_time_and_tomorrows_date) {
std::cout << "Simulator:: ...test_time_and_tomorrows_date\n";
// # Note: we have to use relative paths, since these tests are relocatable
// suite suite
// clock real <todays date>
// family family
// task t1
// date <tomorrows date>
// time <start>
// endfamily
// endsuite
Defs theDefs;
{
// Initialise clock with todays date then create a time attribute + minutes
// such that the task should only run once, in the next minute
auto theLocalTime = Calendar::second_clock_time();
auto tomorrows_date = theLocalTime.date();
tomorrows_date += boost::gregorian::days(1);
auto time_plus_minute = theLocalTime + boost::posix_time::minutes(1);
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite_ptr suite = theDefs.add_suite("test_time_and_tomorrows_date");
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
task->addDate(DateAttr(tomorrows_date));
task->addTime(ecf::TimeAttr(TimeSlot(time_plus_minute.time_of_day())));
task->addVerify(VerifyAttr(NState::COMPLETE, 1)); // expect task to complete 1 time
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(simulator.run(theDefs, findTestDataLocation("test_time_and_tomorrows_date.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_time_and_tomorrows_date.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_multiple_times_and_dates) {
std::cout << "Simulator:: ...test_multiple_times_and_dates\n";
// # Note: we have to use relative paths, since these tests are relocatable
// suite suite
// clock real <todays date>
// family family
// task t1
// date <today date>
// date <tomorrows date>
// time <start>
// time <start>
// endfamily
// endsuite
Defs theDefs;
{
// Initialise clock with todays date then create a time attribute + minutes
// Note: we don't use:
// auto theLocalTime = Calendar::second_clock_time();
// because if run at late we can end up with illegal for the hour, ie. > 24
auto todaysDate = boost::gregorian::date(2009, 2, 10);
auto theLocalTime = boost::posix_time::ptime(todaysDate, boost::posix_time::hours(3));
auto tomarrows_date = todaysDate + boost::gregorian::date_duration(1);
auto td_plus_minute = theLocalTime.time_of_day() + boost::posix_time::minutes(1);
auto td_plus_hour = theLocalTime.time_of_day() + boost::posix_time::hours(1);
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite_ptr suite = theDefs.add_suite("test_multiple_times_and_dates");
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
task->addDate(DateAttr(todaysDate));
task->addDate(DateAttr(tomarrows_date));
task->addTime(TimeAttr(TimeSlot(td_plus_minute)));
task->addTime(TimeAttr(TimeSlot(td_plus_hour)));
task->addVerify(VerifyAttr(NState::COMPLETE, 4)); // expect task to complete 4 time
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(simulator.run(theDefs, findTestDataLocation("test_multiple_times_and_dates.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_multiple_times_and_dates.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_multiple_times_and_dates_hybrid) {
std::cout << "Simulator:: ...test_multiple_times_and_dates_hybrid\n";
// # Note: we have to use relative paths, since these tests are relocatable
// suite suite
// clock hybrid <todays date>
// family family
// task t1
// date <today date>
// date <tomrrows date>
// time <start>
// time <start>
// endfamily
// endsuite
Defs theDefs;
{
// Initialise clock with todays date then create a time attribute + minutes
// such that the task should only run once, in the next minute
auto theLocalTime =
boost::posix_time::ptime(boost::gregorian::date(2012, 2, 22), boost::posix_time::time_duration(10, 20, 0));
auto todaysDate = theLocalTime.date();
auto tomorrows_date = todaysDate + boost::gregorian::date_duration(1);
auto td_plus_minute = theLocalTime.time_of_day() + boost::posix_time::minutes(1);
auto td_plus_10_minute = theLocalTime.time_of_day() + boost::posix_time::minutes(10);
suite_ptr suite = theDefs.add_suite("test_multiple_times_and_dates_hybrid");
suite->addClock(ClockAttr(theLocalTime, true)); // true means use hybrid clock
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
task->addDate(DateAttr(todaysDate));
task->addDate(DateAttr(tomorrows_date));
task->addTime(TimeAttr(TimeSlot(td_plus_minute)));
task->addTime(TimeAttr(TimeSlot(td_plus_10_minute)));
task->addVerify(VerifyAttr(NState::COMPLETE, 2)); // expect task to complete 2 time
// std::cout << theDefs << "\n";
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(
simulator.run(theDefs, findTestDataLocation("test_multiple_times_and_dates_hybrid.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_multiple_times_and_dates_hybrid.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_multiple_times_and_days) {
std::cout << "Simulator:: ...test_multiple_times_and_days\n";
// suite suite
// clock real <sunday>
// family family
// rep integer 0 1 1 # repeat twice
// task t1
// day monday
// day tuesday
// time <start>
// time <start>
// endfamily
// endsuite
// Initialise real clock on a sunday, such that task should run on Monday & tuesday twice
Defs theDefs;
{
ClockAttr clockAttr(false /*false means use real clock*/);
clockAttr.date(11, 10, 2009); // 11 October 2009 was a sunday
suite_ptr suite = theDefs.add_suite("test_multiple_times_and_days");
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
// fam->addRepeat( RepeatInteger("rep",0,1,1) );
task_ptr task = fam->add_task("t");
task->addDay(DayAttr(DayAttr::MONDAY));
task->addDay(DayAttr(DayAttr::TUESDAY));
task->addTime(TimeAttr(TimeSlot(10, 0)));
task->addTime(TimeAttr(TimeSlot(20, 0)));
task->addVerify(VerifyAttr(NState::COMPLETE, 4)); // expect task to complete 4 time
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(simulator.run(theDefs, findTestDataLocation("test_multiple_times_and_days.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_multiple_times_and_days.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_CASE(test_multiple_times_and_days_hybrid) {
std::cout << "Simulator:: ...test_multiple_times_and_days_hybrid\n";
// suite suite
// clock hybrid <monday>
// family family
// task t1
// day monday
// day tuesday
// time <start>
// time <start>
// endfamily
// endsuite
// Initialise real clock on a Monday, such that task should _only_ run
// on Monday since we are using a hybrid clock
Defs theDefs;
{
ClockAttr clockAttr(true /*false means use hybrid clock*/);
clockAttr.date(12, 10, 2009); // 12 October 2009 was a Monday
suite_ptr suite = theDefs.add_suite("test_multiple_times_and_days_hybrid");
suite->addClock(clockAttr);
family_ptr fam = suite->add_family("family");
task_ptr task = fam->add_task("t");
task->addDay(DayAttr(DayAttr::MONDAY));
task->addDay(DayAttr(DayAttr::TUESDAY));
task->addTime(TimeAttr(TimeSlot(10, 0)));
task->addTime(TimeAttr(TimeSlot(11, 0)));
task->addTime(TimeAttr(TimeSlot(20, 0)));
task->addVerify(VerifyAttr(NState::COMPLETE, 3)); // expect task to complete 3 times
}
Simulator simulator;
std::string errorMsg;
BOOST_CHECK_MESSAGE(
simulator.run(theDefs, findTestDataLocation("test_multiple_times_and_days_hybrid.def"), errorMsg),
errorMsg << "\n"
<< ecf::as_string(theDefs, PrintStyle::DEFS));
// remove generated log file. Comment out to debug
std::string logFileName = findTestDataLocation("test_multiple_times_and_days_hybrid.def") + ".log";
fs::remove(logFileName);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|