File: TestRepeat.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 (370 lines) | stat: -rw-r--r-- 14,666 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
/*
 * 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/base/WhyCmd.hpp"
#include "ecflow/core/Chrono.hpp"
#include "ecflow/core/Converter.hpp"
#include "ecflow/core/PrintStyle.hpp"
#include "ecflow/core/Str.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_Repeat)

// In the test case we will dynamically create all the test data.
// The data is created dynamically so that we can stress test the server
// This test does not have any time dependencies in the def file.
BOOST_AUTO_TEST_CASE(test_repeat_integer) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // ********************************************************************************
    // IMPORTANT: A family will only complete when it has reached the end of the repeats
    // *********************************************************************************

    // 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 test_repeat_integer
    //   repeat integer VAR 0 1 1          # run at 0, 1    2 times
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   family family
    //     repeat integer VAR 0 2 1     # run at 0, 1     2 times
    //     task t<n>
    //     ....
    //   endfamily
    // endsuite

    // Each task/job should be run *4* times, according to the repeats
    // Mimics nested loops
    Defs theDefs;
    {
        suite_ptr suite = theDefs.add_suite("test_repeat_integer");
        suite->addRepeat(RepeatInteger("VAR", 0, 1, 1)); // repeat suite 2 times,
        suite->addVerify(VerifyAttr(NState::COMPLETE, 2));
        family_ptr fam = suite->add_family("family");
        fam->addRepeat(RepeatInteger("VAR", 0, 1, 1)); // repeat family 2 times
        fam->addVerify(VerifyAttr(NState::COMPLETE, 4));
        int taskSize = 2; // on linux 1024 tasks take ~4 seconds for job submission
        for (int i = 0; i < taskSize; i++) {
            task_ptr task = fam->add_task("t" + ecf::convert_to<std::string>(i));
            task->addVerify(VerifyAttr(NState::COMPLETE, 4)); // task should complete 4 times
        }
    }

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

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

BOOST_AUTO_TEST_CASE(test_repeat_date) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // ********************************************************************************
    // IMPORTANT: A family will only complete when it has reached the end of the repeats
    // *********************************************************************************
    // suite test_repeat_date
    // family family
    //   repeat date DATE 20110630 20110632
    //   task t<n>
    //      ....
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        suite_ptr suite = theDefs.add_suite("test_repeat_date");
        suite->addVerify(VerifyAttr(NState::COMPLETE, 1));
        family_ptr fam = suite->add_family("family");
        fam->addRepeat(RepeatDate("DATE", 20110630, 20110704));
        fam->addVerify(VerifyAttr(NState::COMPLETE, 5));
        int taskSize = 2;
        for (int i = 0; i < taskSize; i++) {
            task_ptr task = fam->add_task("t" + ecf::convert_to<std::string>(i));
            task->addVerify(VerifyAttr(NState::COMPLETE, 5));
        }
    }

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

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

BOOST_AUTO_TEST_CASE(test_repeat_date_list) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // ********************************************************************************
    // IMPORTANT: A family will only complete when it has reached the end of the repeats
    // *********************************************************************************
    // suite test_repeat_date
    // family family
    //   repeat datelist DATE 20110630 20110632
    //   task t<n>
    //   ....
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        suite_ptr suite = theDefs.add_suite("test_repeat_date_list");
        suite->addVerify(VerifyAttr(NState::COMPLETE, 1));
        family_ptr fam = suite->add_family("family");
        fam->addRepeat(RepeatDateList("DATE", {20110630, 20110704}));
        fam->addVerify(VerifyAttr(NState::COMPLETE, 2));
        int taskSize = 2;
        for (int i = 0; i < taskSize; i++) {
            task_ptr task = fam->add_task("t" + ecf::convert_to<std::string>(i));
            task->addVerify(VerifyAttr(NState::COMPLETE, 2));
        }
    }

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

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

BOOST_AUTO_TEST_CASE(test_repeat_enumerator) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // ********************************************************************************
    // IMPORTANT: A family will only complete when it has reached the end of the repeats
    // *********************************************************************************
    // suite test_repeat_enumerator
    //   family top
    //     family plot
    //       family iasi_plots
    //         repeat enumerated month "200801" "200802"
    //         task t1
    //       endfamily
    //     endfamily
    //   endfamily
    // endsuite

    Defs theDefs;
    {
        suite_ptr suite       = theDefs.add_suite("test_repeat_enumerator");
        family_ptr top        = suite->add_family("top");
        family_ptr plot       = top->add_family("plot");
        family_ptr iasi_plots = plot->add_family("iasi_plots");
        std::vector<std::string> months;
        months.reserve(12);
        months.push_back("200801");
        months.push_back("200802");
        iasi_plots->addRepeat(RepeatEnumerated("month", months));
        task_ptr t1 = iasi_plots->add_task("t1");
        t1->addVerify(VerifyAttr(NState::COMPLETE, 2));
    }

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

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

BOOST_AUTO_TEST_CASE(test_repeat_defstatus) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // TEST SHOULD COMPLETE STRAIGHT AWAY SINCE WE HAVE A DEFSTATUS COMPLETE
    // since the complete state is set on all children of suite.

    // Create the defs file corresponding to the text below
    // # Note: we have to use relative paths, since these tests are relocatable
    // suite test_repeat_defstatus
    //   defstatus complete
    //   repeat integer VAR 0 1 1          # run at 0, 1    2 times
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   family family
    //     repeat integer VAR 0 2 1     # run at 0, 1     2 times
    //     task t<n>
    //     ....
    //   endfamily
    // endsuite
    Defs theDefs;
    {
        suite_ptr suite = theDefs.add_suite("test_repeat_defstatus");
        suite->addDefStatus(DState::COMPLETE);
        suite->addRepeat(RepeatInteger("VAR", 0, 1, 1));
        suite->addVerify(VerifyAttr(NState::COMPLETE, 1));

        family_ptr fam = suite->add_family("family");
        fam->addRepeat(RepeatInteger("VAR", 0, 1, 1));
        fam->addVerify(VerifyAttr(NState::COMPLETE, 1));
        int taskSize = 2; // on linux 1024 tasks take ~4 seconds for job submission
        for (int i = 0; i < taskSize; i++) {

            task_ptr task = fam->add_task("t" + ecf::convert_to<std::string>(i));
            task->addVerify(VerifyAttr(NState::COMPLETE, 1));
        }
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs, ServerTestHarness::testDataDefsLocation("test_repeat_defstatus.def"));
    std::cout << timer.duration() << " update-calendar-count(" << serverTestHarness.serverUpdateCalendarCount()
              << ")\n";
}

// #define DEBUG_ME 1
BOOST_AUTO_TEST_CASE(test_repeat_clears_user_edit) {
    ECF_NAME_THIS_TEST();

    // Tests code:: Node::requeueOrSetMostSignificantStateUpNodeTree()
    // In *PARTICULAR* THE REQUE caused by the repeat, this ensures we clear NO_REQUE_IF_SINGLE_TIME_DEP
    // So that the effect of manual run/force complete are negated via automated re-queue caused by a REPEAT

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    // # Note: we have to use relative paths, since these tests are relocatable
    // suite test_repeat_clears_user_edit
    //   edit SLEEPTIME 1
    //   edit ECF_INCLUDE $ECF_HOME/includes
    //   family family
    //     repeat integer VAR 0 3     # run at 0,1,2    i.e 3 times
    //     task t<n>
    //       time <current time>
    //   endfamily
    // endsuite

    Defs theDefs;
    task_ptr task;
    {
        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(3);

        suite_ptr suite = theDefs.add_suite("test_repeat_clears_user_edit");
        ClockAttr clockAttr(theLocalTime, false);
        suite->addClock(clockAttr);

        suite->addDefStatus(DState::SUSPENDED);
        family_ptr fam = suite->add_family("family");
        fam->addRepeat(RepeatInteger("VAR", 0, 2, 1)); // repeat family 3 times
        task = fam->add_task("t1");
        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time1.time_of_day())));
        task->addVerify(VerifyAttr(NState::COMPLETE, 3));
    }

    // The test harness will create corresponding directory structure
    // and populate with standard sms files.
    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_repeat_integer.def"),
                          40,
                          false /* waitFortestcompletion */);

#ifdef DEBUG_ME
    PrintStyle style(PrintStyle::STATE);
    TestFixture::client().sync_local();
    defs_ptr server_defs = TestFixture::client().defs();
    cout << "At start ============================================\n";
    cout << server_defs << "\n";
#endif

    // USER EDIT, on task with a time. The force complete will expire the time.
    // Forcing a task with the time attribute, to complete state, should invalidate/expire the time,
    // Hence it should hold indefinitely, or until it is re-queued manually, or automatically via a Repeat/cron.
    // This Test ensures the the *REQUEUE* via the repeat, resets the time based attribute
    TestFixture::client().force(task->absNodePath(), "complete", true);
    TestFixture::client().force(task->absNodePath(), "complete", true);

#ifdef DEBUG_ME
    TestFixture::client().sync_local();
    server_defs = TestFixture::client().defs();
    cout << "After 2 force complete ============================================\n";
    cout << server_defs << "\n";

    node_ptr the_task = server_defs->findAbsNode(task->absNodePath());
    BOOST_REQUIRE_MESSAGE(the_task, "Task " << task->absNodePath() << " not found");
    WhyCmd whyCmd(server_defs, the_task->absNodePath());
    std::string reason = whyCmd.why();
    cout << "Why command ============================================\n";
    std::cout << reason << "\n\n";
#endif

    // resume the suspend suite
    TestFixture::client().resume("/test_repeat_clears_user_edit");

#ifdef DEBUG_ME
    TestFixture::client().sync_local();
    server_defs = TestFixture::client().defs();
    cout << "At Resume ============================================\n";
    cout << server_defs << "\n";
#endif

    // Wait for final LOOP of the repeat, and test to finish
    int timeout             = 20;
    bool verifyAttrInServer = true;
    defs_ptr serverDefs     = serverTestHarness.testWaiter(theDefs, timeout, verifyAttrInServer);
    BOOST_REQUIRE_MESSAGE(serverDefs.get(), " Failed to return defs after wait of 20 seconds");

#ifdef DEBUG_ME
    cout << "At test finish ============================================\n";
    cout << serverDefs << "\n";
#endif

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

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()