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
|
/*
* 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 "MockServer.hpp"
#include "ecflow/base/cts/user/FreeDepCmd.hpp"
#include "ecflow/base/stc/ServerToClientCmd.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Expression.hpp"
#include "ecflow/node/Family.hpp"
#include "ecflow/node/Suite.hpp"
#include "ecflow/node/Task.hpp"
#include "ecflow/test/scaffold/Naming.hpp"
using namespace ecf;
BOOST_AUTO_TEST_SUITE(U_Base)
BOOST_AUTO_TEST_SUITE(T_FreeDepCmd)
BOOST_AUTO_TEST_CASE(test_free_dep_cmd) {
ECF_NAME_THIS_TEST();
TestLog test_log("test_free_dep_cmd.log"); // will create log file, and destroy log and remove file at end of scope
// Create a test and add the date and time dependencies
auto theLocalTime = boost::posix_time::ptime(boost::gregorian::date(2011, boost::gregorian::Nov, 4),
boost::posix_time::time_duration(9, 30, 0));
auto time_plus_hour = theLocalTime + boost::posix_time::hours(1);
auto time_plus_2_hour = theLocalTime + boost::posix_time::hours(2);
auto todaysDate = theLocalTime.date();
auto tomorrows_date = todaysDate + boost::gregorian::date_duration(1);
auto tomorrows_date_1 = todaysDate + boost::gregorian::date_duration(2);
// Get tomorrow as a day so that isFree fails.
tm day_1 = to_tm(tomorrows_date);
tm day_2 = to_tm(tomorrows_date_1);
suite_ptr suite;
task_ptr task;
Defs theDefs;
{
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 = fam->add_task("t");
task->addTime(ecf::TimeAttr(TimeSlot(time_plus_hour.time_of_day())));
task->addTime(ecf::TimeAttr(TimeSlot(time_plus_2_hour.time_of_day())));
task->addTime(ecf::TimeAttr(
ecf::TimeSlot(1, 1), ecf::TimeSlot(1, 3), ecf::TimeSlot(0, 1), true /*relative to suite start*/
));
task->addToday(ecf::TodayAttr(TimeSlot(time_plus_hour.time_of_day())));
task->addToday(ecf::TodayAttr(TimeSlot(time_plus_2_hour.time_of_day())));
task->addDate(DateAttr(tomorrows_date));
task->addDate(DateAttr(tomorrows_date_1));
task->addDay(DayAttr(static_cast<DayAttr::Day_t>(day_1.tm_wday)));
task->addDay(DayAttr(static_cast<DayAttr::Day_t>(day_2.tm_wday)));
task->add_trigger("t2 == complete");
CronAttr cronAttr;
cronAttr.addTimeSeries(ecf::TimeSlot(time_plus_hour.time_of_day()),
ecf::TimeSlot(time_plus_2_hour.time_of_day()),
ecf::TimeSlot(0, 1));
task->addCron(cronAttr);
fam->add_task("t2");
// cout << theDefs << "\n";
}
std::vector<task_ptr> vec;
theDefs.get_all_tasks(vec);
BOOST_REQUIRE_MESSAGE(vec.size() == 2, "Error in number of tasks");
// This will initialise the calendar from the Clock attribute
theDefs.beginAll();
// First all trigger should fail to evaluate AND all dependencies should NOT be free
BOOST_REQUIRE_MESSAGE(task->triggerAst(), "Expected trigger abstract syntax tree for task " << task->absNodePath());
BOOST_REQUIRE_MESSAGE(!task->triggerAst()->evaluate(), "Trigger should not evaluate");
const Calendar& cal = suite->calendar();
for (const ecf::TimeAttr& attr : task->timeVec()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Time should not be free");
}
for (const ecf::TodayAttr& attr : task->todayVec()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Today should not be free");
}
for (const ecf::CronAttr& attr : task->crons()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Cron should not be free");
}
for (const DateAttr& attr : task->dates()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Date should not be free");
}
for (const DayAttr& attr : task->days()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), "Day should not be free");
}
// Invoke the FreeDepCmd
MockServer mockServer(&theDefs);
FreeDepCmd cmd(task->absNodePath(), true /*trigger*/, true /* all */);
cmd.setup_user_authentification();
STC_Cmd_ptr returnCmd = cmd.handleRequest(&mockServer);
BOOST_REQUIRE_MESSAGE(returnCmd->ok(), "Failed to for FreeDepCmd");
// Dependencies should now all be free now.
BOOST_REQUIRE_MESSAGE(task->get_trigger()->isFree(), "Trigger should evaluate");
bool at_least_one_free = false;
for (const ecf::TimeAttr& attr : task->timeVec()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Time should be free");
at_least_one_free = false;
for (const ecf::TodayAttr& attr : task->todayVec()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Today should be free");
at_least_one_free = false;
for (const ecf::CronAttr& attr : task->crons()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Cron should be free");
at_least_one_free = false;
for (const DateAttr& attr : task->dates()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Date should be free");
at_least_one_free = false;
for (const DayAttr& attr : task->days()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Day should be free");
}
BOOST_AUTO_TEST_CASE(test_free_dep_cmd_single_time_slot) {
ECF_NAME_THIS_TEST();
TestLog test_log("test_free_dep_cmd_single_time_slot.log"); // will create log file, and destroy log and remove file
// at end of scope
// We add a time dependency *AFTER* the suite/calendar time
// This checks that we DO NOT re-queue a task with a future time dependency
// when a time dependency has been freed
auto theLocalTime = boost::posix_time::ptime(boost::gregorian::date(2011, boost::gregorian::Nov, 4),
boost::posix_time::time_duration(9, 30, 0));
auto time_plus_2minute = theLocalTime + boost::posix_time::minutes(2);
suite_ptr suite;
task_ptr task;
Defs theDefs;
{
suite = theDefs.add_suite("test_time");
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite->addClock(clockAttr);
task = suite->add_task("t");
task->addTime(ecf::TimeAttr(TimeSlot(time_plus_2minute.time_of_day())));
}
// This will initialise the calendar from the Clock attribute
theDefs.beginAll();
// expect task to be holding at 9:30
const Calendar& cal = suite->calendar();
for (const ecf::TimeAttr& attr : task->timeVec()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Time should not be free");
}
// Invoke the FreeDepCmd
MockServer mockServer(&theDefs);
FreeDepCmd cmd(task->absNodePath(), true /*trigger*/, true /* all */);
cmd.setup_user_authentification();
STC_Cmd_ptr returnCmd = cmd.handleRequest(&mockServer);
BOOST_REQUIRE_MESSAGE(returnCmd->ok(), "Failed to for FreeDepCmd");
// expect task to be free
bool at_least_one_free = false;
for (const ecf::TimeAttr& attr : task->timeVec()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Time should be free");
// The crux of the test. Should not requeue.
BOOST_CHECK_MESSAGE(task->state() == NState::ACTIVE,
"Free dependency should have done an immediate job submission. Expected active state");
task->complete();
BOOST_CHECK_MESSAGE(task->state() == NState::COMPLETE,
"Expected Complete but found "
<< NState::toString(task->state())
<< ", free dependency should avoid re-queue for a future single time dependency.");
}
BOOST_AUTO_TEST_CASE(test_free_dep_cmd_with_time_series) {
ECF_NAME_THIS_TEST();
TestLog test_log("test_free_dep_cmd_with_time_series.log"); // will create log file, and destroy log and remove file
// at end of scope
// We start the suite *IN BETWEEN* a time series, and the force free dependency
// This checks that we DO NOT re-queue a task with a future time dependency
// when a time dependency has been freed
auto theLocalTime = boost::posix_time::ptime(boost::gregorian::date(2011, boost::gregorian::Nov, 4),
boost::posix_time::time_duration(9, 29, 0));
suite_ptr suite;
task_ptr task;
Defs theDefs;
{
suite = theDefs.add_suite("test_time");
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite->addClock(clockAttr);
task = suite->add_task("t");
task->addTime(ecf::TimeAttr(
ecf::TimeSlot(9, 28), ecf::TimeSlot(9, 30), ecf::TimeSlot(0, 2), false /*relative to suite start*/
));
}
// This will initialise the calendar from the Clock attribute
theDefs.beginAll();
// expect task to be holding, at 9:29
const Calendar& cal = suite->calendar();
for (const ecf::TimeAttr& attr : task->timeVec()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Time should not be free");
}
// Invoke the FreeDepCmd
MockServer mockServer(&theDefs);
FreeDepCmd cmd(task->absNodePath(), true /*trigger*/, true /* all */);
cmd.setup_user_authentification();
STC_Cmd_ptr returnCmd = cmd.handleRequest(&mockServer);
BOOST_REQUIRE_MESSAGE(returnCmd->ok(), "Failed to for FreeDepCmd");
// expect task to be free
bool at_least_one_free = false;
for (const ecf::TimeAttr& attr : task->timeVec()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Time should be free");
// The crux of the test. Should not requeue, as we should have advanced the time slot beyond the END point
// i.e FreeDepCmd will advance the time slot, beyond end time
BOOST_CHECK_MESSAGE(task->state() == NState::ACTIVE,
"Free dependency should have done an immediate job submission. Expected active state");
task->complete();
BOOST_CHECK_MESSAGE(task->state() == NState::COMPLETE,
"Expected Complete but found "
<< NState::toString(task->state())
<< ", free dependency should avoid re-queue for a future single time dependency.");
}
BOOST_AUTO_TEST_CASE(test_free_dep_cmd_with_time_series_2) {
ECF_NAME_THIS_TEST();
TestLog test_log("test_free_dep_cmd_with_time_series_2.log"); // will create log file, and destroy log and remove
// file at end of scope
// We start the suite *IN BETWEEN* a time series, and the force free dependency
// This time we have a larger range, and *SHOULD* re-queue, even if we miss a time slot
auto theLocalTime = boost::posix_time::ptime(boost::gregorian::date(2011, boost::gregorian::Nov, 4),
boost::posix_time::time_duration(9, 29, 0));
suite_ptr suite;
task_ptr task;
Defs theDefs;
{
suite = theDefs.add_suite("test_time");
ClockAttr clockAttr(theLocalTime, false /*false means use real clock*/);
suite->addClock(clockAttr);
task = suite->add_task("t");
task->addTime(ecf::TimeAttr(
ecf::TimeSlot(9, 28), ecf::TimeSlot(9, 34), ecf::TimeSlot(0, 2), false /*relative to suite start*/
));
}
// This will initialise the calendar from the Clock attribute
// Will initialise nextTimeSlot=09:30
theDefs.beginAll();
// expect task to be holding, at 9:29
const Calendar& cal = suite->calendar();
for (const ecf::TimeAttr& attr : task->timeVec()) {
BOOST_CHECK_MESSAGE(!attr.isFree(cal), " Time should not be free");
}
// Invoke the FreeDepCmd. This will update next nextTimeSlot to 09:32
MockServer mockServer(&theDefs);
FreeDepCmd cmd(task->absNodePath(), true /*trigger*/, true /* all */);
cmd.setup_user_authentification();
STC_Cmd_ptr returnCmd = cmd.handleRequest(&mockServer);
BOOST_REQUIRE_MESSAGE(returnCmd->ok(), "Failed to for FreeDepCmd");
// expect task to be free
bool at_least_one_free = false;
for (const ecf::TimeAttr& attr : task->timeVec()) {
if (attr.isFree(cal)) {
at_least_one_free = true;
break;
}
}
BOOST_CHECK_MESSAGE(at_least_one_free, "At least one Time should be free");
// The crux of the test. Even if we advance the time slot we should *STILL* be in range
BOOST_CHECK_MESSAGE(task->state() == NState::ACTIVE,
"Free dependency should have done an immediate job submission. Expected active state");
task->complete();
BOOST_CHECK_MESSAGE(task->state() == NState::QUEUED,
"Expected QUEUED but found "
<< NState::toString(task->state())
<< ", free dependency should avoid re-queue for a future single time dependency.");
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|