File: TestTime.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (449 lines) | stat: -rw-r--r-- 19,380 bytes parent folder | download
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
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * 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 <cstdlib>
#include <fstream>
#include <iostream>

#include <boost/test/unit_test.hpp>

#include "ServerTestHarness.hpp"
#include "TestFixture.hpp"
#include "ecflow/attribute/VerifyAttr.hpp"
#include "ecflow/core/Chrono.hpp"
#include "ecflow/core/Converter.hpp"
#include "ecflow/core/PrintStyle.hpp"
#include "ecflow/core/Timer.hpp"
#include "ecflow/node/Defs.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(S_Test)

BOOST_AUTO_TEST_SUITE(T_Time)

BOOST_AUTO_TEST_CASE(test_single_real_time) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // Create the defs file corresponding to the text below
    // ECF_HOME variable is automatically added by the test harness.
    // ECF_INCLUDE variable is automatically added by the test harness.
    // SLEEPTIME variable is automatically added by the test harness.
    // ECF_CLIENT_EXE_PATH variable is automatically added by the test harness.
    //                     This is substituted in sms includes
    //                     Allows test to run without requiring installation

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite suite
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <todays date>
    //   family family
    //     task t1
    //       time 10:00
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // Initialise clock with fixed date and time, then create a time attribute
        // with todays time + minute
        // Avoid adding directly to TimeSlot
        // i.e if local time is 9:59 and we create a TimeSlot like
        //       task->addTime( ecf::TimeAttr( ecf::TimeSlot(theTm.tm_hour,theTm.tm_min+3) )  );
        // The the minute will be 62, which is illegal and will not parse
        auto theLocalTime =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(10, 0, 0));
        auto time1 = theLocalTime + boost::posix_time::minutes(1);

        // For each 2 seconds of poll in the server update calendar by 1 minute
        // Note: if we use 1 seconds poll to update calendar by 1 minute, then
        //       we will find that state change happens at job submission interval,
        //       and hence skews time series.  Which can leave state in a queued state,
        //       and hence test never completes
        suite_ptr suite = theDefs.add_suite("test_single_real_time");
        ClockAttr clockAttr(theLocalTime, false);
        suite->addClock(clockAttr);

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time1.time_of_day())));
        task->addVerify(VerifyAttr(NState::COMPLETE, 1)); // task should complete 1 times
    }

    // The test harness will create corresponding directory structure
    // and populate with standard ecf files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_single_real_time.def"));

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_CASE(test_single_time_trigger) {
    ECF_NAME_THIS_TEST();

    // ECFLOW-833
    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // Create the defs file corresponding to the text below
    // ECF_HOME variable is automatically added by the test harness.
    // ECF_INCLUDE variable is automatically added by the test harness.
    // SLEEPTIME variable is automatically added by the test harness.
    // ECF_CLIENT_EXE_PATH variable is automatically added by the test harness.
    //                     This is substituted in sms includes
    //                     Allows test to run without requiring installation

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite suite
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <todays date>
    //   family family
    //     task t1
    //       trigger /suite:TIME == 1001
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // Initialise clock with fixed date and time, then create a time attribute
        // with todays time + minute
        // Avoid adding directly to TimeSlot
        // i.e if local time is 9:59 and we create a TimeSlot like
        //       task->addTime( ecf::TimeAttr( ecf::TimeSlot(theTm.tm_hour,theTm.tm_min+3) )  );
        // The the minute will be 62, which is illegal and will not parse
        auto theLocalTime =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(10, 0, 0));

        // For each 2 seconds of poll in the server update calendar by 1 minute
        // Note: if we use 1 seconds poll to update calendar by 1 minute, then
        //       we will find that state change happens at job submission interval,
        //       and hence skews time series.  Which can leave state in a queued state,
        //       and hence test never completes
        suite_ptr suite = theDefs.add_suite("test_single_time_trigger");
        ClockAttr clockAttr(theLocalTime, false);
        suite->addClock(clockAttr);

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        std::stringstream ss;
        ss << "/test_single_time_trigger:TIME == "
           << "1001";
        task->add_trigger(ss.str());
        task->addVerify(VerifyAttr(NState::COMPLETE, 1)); // task should complete 1 times
    }

    // The test harness will create corresponding directory structure
    // and populate with standard ecf files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_single_time_trigger.def"));

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_CASE(test_time_multiple_single_slot) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // SLOW SYSTEMS
    // for each time attribute leave GAP of 3 * job submission interval
    // on slow systems submitted->active->complete > TestFixture::job_submission_interval()
    // Also the task duration must be greater than job_submission_interval,  otherwise
    // we will get multiple invocation for the same time step.
    // *sometimes* just submitted->active can take many times job submission interval.

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite test_time_multiple_single_slot
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <todays date>
    //   family family
    //     task t1
    //       time 10:01
    //       time 10:04
    //       time 10:07
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // Initialise clock with todays date and time, then create a time attribute
        // with todays time + minute.
        auto theLocalTime =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(10, 0, 0));
        auto time1 = theLocalTime + boost::posix_time::minutes(1);
        auto time2 = time1 + boost::posix_time::minutes(TestFixture::job_submission_interval());
        auto time3 = time2 + boost::posix_time::minutes(TestFixture::job_submission_interval());

        suite_ptr suite = theDefs.add_suite("test_time_multiple_single_slot");
        ClockAttr clockAttr(theLocalTime, false);
        suite->addClock(clockAttr);
        suite->add_variable("SLEEPTIME", "1");

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time1.time_of_day())));
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time2.time_of_day())));
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time3.time_of_day())));
        task->addVerify(VerifyAttr(NState::COMPLETE, 3)); // task should complete 3 times
    }

    // The test harness will create corresponding directory structure
    // and populate with standard ecf files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_time_multiple_single_slot.def"));

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_CASE(test_time_relative_time_series) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // SLOW SYSTEMS
    // for each time attribute leave GAP of 3 * job submission interval
    // on slow systems submitted->active->complete > TestFixture::job_submission_interval()
    // Also the task duration must be greater than job_submission_interval,  otherwise
    // we will get multiple invocation for the same time step

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite test_time_relative_time_series
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <todays date>
    //   family family
    //     task t1
    //       time +<start> <finish> incr
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // Initialise clock with todays date and time, then create a time attribute
        // with a time series, so that task runs 3 times relative to suite start
        suite_ptr suite = theDefs.add_suite("test_time_relative_time_series");
        ClockAttr clockAttr(Calendar::second_clock_time(), false);
        suite->addClock(clockAttr);
        suite->add_variable("SLEEPTIME", ecf::convert_to<std::string>(TestFixture::job_submission_interval() - 2));

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        task->addTime(ecf::TimeAttr(
            ecf::TimeSlot(0, 1), ecf::TimeSlot(0, 7), ecf::TimeSlot(0, 3), true /*relative to suite start*/
            ));
        task->addVerify(VerifyAttr(NState::COMPLETE, 3)); // task should complete 3 times
    }

    // The test harness will create corresponding directory structure
    // and populate with standard ecf files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_time_relative_time_series.def"));

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_CASE(test_time_real_series) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // SLOW SYSTEMS
    // for each time attribute leave GAP of 3 * job submission interval
    // on slow systems submitted->active->complete > TestFixture::job_submission_interval()
    // Also the task duration must be greater than job_submission_interval,  otherwise
    // we will get multiple invocation for the same time step

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite test_time_real_series
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <date>
    //   family family
    //     task t1
    //       time <start> <finish> <incr>
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // Initialise clock with todays date and time, then create a time attribute
        // with a time series, so that task runs 3 times
        auto theLocalTime =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(10, 0, 0));
        auto time1 = theLocalTime + boost::posix_time::minutes(1);
        auto time2 = time1 + boost::posix_time::minutes(TestFixture::job_submission_interval() * 2);

        suite_ptr suite = theDefs.add_suite("test_time_real_series");
        suite->add_variable("SLEEPTIME", "1");

        ClockAttr clockAttr(theLocalTime, false);
        suite->addClock(clockAttr);

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time1.time_of_day()),
                                    ecf::TimeSlot(time2.time_of_day()),
                                    ecf::TimeSlot(0, TestFixture::job_submission_interval())));
        task->addVerify(VerifyAttr(NState::COMPLETE, 3)); // task should complete 3 times
                                                          // 1 +    7  + 13
                                                          // 1 + (2*n) + (2*n) = 1 + 4n
        // start = 1, finish = 13, when n=3, when n = job submission interval
        // to complete 3 times, we must use interval of n*2
    }

    // The test harness will create corresponding directory structure
    // and populate with standard sms files.
    ServerTestHarness serverTestHarness;
    // serverTestHarness.add_default_sleep_time(false); // avoid missing time steps due to submit->active->complete >
    // job submission interval
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_time_real_series.def"));

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_CASE(test_single_real_time_near_midnight) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite suite
    //   edit SLEEPTIME 4
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <todays date>
    //   family family
    //     task t1
    //       time 23:59
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // ECFLOW-130
        // Make sure job completes after midnight.
        // The task SHOULD stay complete and *NOT* requeue
        // Since TestHarness requires suite completion, we dont need to do anything.
        auto time_start =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(23, 59, 0));
        auto clock_start = time_start - boost::posix_time::minutes(1);

        suite_ptr suite = theDefs.add_suite("test_single_real_time_near_midnight");
        ClockAttr clockAttr(clock_start, false);
        suite->addClock(clockAttr);
        suite->add_variable("SLEEPTIME", ecf::convert_to<std::string>(TestFixture::job_submission_interval() * 2));

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time_start.time_of_day())));
        task->addVerify(VerifyAttr(NState::COMPLETE, 1)); // task should complete 1 times
    }

    // The test harness will create corresponding directory structure and populate with standard ecf files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_single_real_time_near_midnight.def"));

#ifdef DEBUG_ME
    BOOST_REQUIRE_MESSAGE(TestFixture::client().sync_local() == 0,
                          "Could not get the defs from server\n"
                              << TestFixture::client().errorMsg());
    defs_ptr defs = TestFixture::client().defs();
    PrintStyle style(PrintStyle::STATE);
    std::cout << *defs;
#endif

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_CASE(test_time_real_series_near_midnight) {
    ECF_NAME_THIS_TEST();

    if (auto server_version = TestFixture::server_version(); server_version == "4.0.3") {
        std::cout << " SKIPPED! This test does not work with server version " << server_version << "\n";
        return;
    }

    DurationTimer timer;

    TestClean clean_at_start_and_end;

    // SLOW SYSTEMS
    // for each time attribute leave GAP of 3 * job submission interval
    // on slow systems submitted->active->complete > TestFixture::job_submission_interval()
    // Also the task duration must be greater than job_submission_interval,  otherwise
    // we will get multiple invocation for the same time step

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite test_time_real_series
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   clock real <date>
    //   family family
    //     task t1
    //       time <start> <finish> <incr>
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        // ECFLOW-130
        // make sure that last job, *runs* and completes after midnight.
        // It should stay complete and not requeue.
        // Test harness, will check suite task->family->suite completes, hence no need to do anything
        auto last_time =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(23, 59, 0));
        auto first_time  = last_time - boost::posix_time::minutes(TestFixture::job_submission_interval() * 2);
        auto clock_start = first_time - boost::posix_time::minutes(1);

        suite_ptr suite = theDefs.add_suite("test_time_real_series_near_midnight");
        suite->add_variable("SLEEPTIME", ecf::convert_to<std::string>(TestFixture::job_submission_interval() * 2));

        ClockAttr clockAttr(clock_start, false);
        suite->addClock(clockAttr);

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t");
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(first_time.time_of_day()),
                                    ecf::TimeSlot(last_time.time_of_day()),
                                    ecf::TimeSlot(0, TestFixture::job_submission_interval() * 2)));
        task->addVerify(VerifyAttr(NState::COMPLETE, 2)); // task should complete 2 times
    }

    // The test harness will create corresponding directory structure and populate with standard ecf files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_time_real_series_near_midnight.def"));

#ifdef DEBUG_ME
    BOOST_REQUIRE_MESSAGE(TestFixture::client().sync_local() == 0,
                          "Could not get the defs from server\n"
                              << TestFixture::client().errorMsg());
    defs_ptr defs = TestFixture::client().defs();
    PrintStyle style(PrintStyle::STATE);
    std::cout << *defs;
#endif

    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()