File: TestInLimitAndLimit.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 (1300 lines) | stat: -rw-r--r-- 65,739 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
/*
 * 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 <stdexcept>
#include <string>

#include <boost/test/unit_test.hpp>

#include "TestHelper.hpp"
#include "ecflow/base/cts/user/BeginCmd.hpp"
#include "ecflow/base/cts/user/ForceCmd.hpp"
#include "ecflow/base/cts/user/RequeueNodeCmd.hpp"
#include "ecflow/core/PrintStyle.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Family.hpp"
#include "ecflow/node/Jobs.hpp"
#include "ecflow/node/JobsParam.hpp"
#include "ecflow/node/Limit.hpp"
#include "ecflow/node/Suite.hpp"
#include "ecflow/node/System.hpp"
#include "ecflow/node/Task.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace std;
using namespace ecf;

BOOST_AUTO_TEST_SUITE(U_Base)

BOOST_AUTO_TEST_SUITE(T_InLimitAndLimit)

BOOST_AUTO_TEST_CASE(test_add_log) {
    // create once for all test below, then remove at the end
    Log::create("test_add_log.log");
    BOOST_CHECK_MESSAGE(true, "stop boost test form complaining");
}

BOOST_AUTO_TEST_CASE(test_add_limit) {
    ECF_NAME_THIS_TEST();

    suite_ptr suite = Suite::create("suite");
    suite->addLimit(Limit("fast", 1)); // " Adding limit first time should be ok");
    BOOST_REQUIRE_THROW(suite->addLimit(Limit("fast", 1)),
                        std::runtime_error); //" Adding same limit second time should fail");

    suite->addInLimit(InLimit("fast", "/suite")); //" Adding in-limit first time should be ok");
    BOOST_REQUIRE_THROW(suite->addInLimit(InLimit("fast", "/suite")),
                        std::runtime_error); // " Adding in-limit second time should fail");

    // adding inlimit with limit node only and submission should fail
    BOOST_REQUIRE_THROW(
        suite->addInLimit(InLimit("limit", "/suite", 1, true, /*limit_this_node_only*/ true /*limit submission*/)),
        std::runtime_error); // " Adding in-limit second time should fail");
}

BOOST_AUTO_TEST_CASE(test_limit_increment) {
    ECF_NAME_THIS_TEST();

    // Test than when a job is submitted multiple times, it should only consume 1 token
    //
    // Create the defs file
    // suite suite
    //     limit fast 10
    //    family f
    //          inlimit /suite:fast
    //          task t1
    //    endfamily
    // endsuite
    Defs defs;
    std::string limitName   = "fast";
    std::string pathToLimit = "/suite";
    std::string suitename   = "suite";
    suite_ptr s             = defs.add_suite(suitename);
    family_ptr f            = s->add_family("f");
    task_ptr t1             = f->add_task("t1");
    {
        f->addInLimit(InLimit(limitName, pathToLimit));
        s->addLimit(Limit(limitName, 10));
    }
    // cerr << defs << "\n";

    {
        // Resolve dependencies:: BEFORE calling beginCmd, should be a NO OP
        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 0,
                            "Expected 0 tasks to submit but found " << jobsParam.submitted().size());
    }

    {
        // Create a request to begin suite
        // make sure chosen suite can begin to resolve dependencies.
        // beginning the suite will:
        //     1/ set all children to the QUEUED state
        //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
        //
        //  Resolve dependencies. Only one task should be submitted due to Limit
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd(suitename)));
        BOOST_CHECK_MESSAGE(s->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s->state()));
        BOOST_CHECK_MESSAGE(f->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f->state()));
        BOOST_CHECK_MESSAGE(t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(t1->state()));
    }

    {
        // Resolve dependencies. No task should submit since we have reached the limit
        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 0,
                            "Expected 0 task to submit but found " << jobsParam.submitted().size());
    }

    {
        // Try resubmitting the an active task, it should still only consume 1 token in the limit
        BOOST_CHECK_MESSAGE(t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(t1->state()));

        // Hack the state, to allow job to re- resubmitted
        t1->setStateOnly(NState::QUEUED);

        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 1,
                            "Expected 1 task to submit but found " << jobsParam.submitted().size());

        // Limit should still have only consumed one token
        limit_ptr limit = s->find_limit(limitName);
        BOOST_CHECK_MESSAGE(limit.get(), "Limit not found");
        BOOST_CHECK_MESSAGE(limit->value() == 1, "Expected limit of value 1, but found " << limit->value());
        BOOST_CHECK_MESSAGE(limit->paths().size() == 1,
                            "Expected Only 1 task path in limit but found " << limit->paths().size());
    }
}

//===============================================================================
// This TEST is used to test limit and inLimit.
// Both examples taken from the documentation
BOOST_AUTO_TEST_CASE(test_limit) {
    ECF_NAME_THIS_TEST();

    ///////////////////////////////////////////////////////////////////////////
    // Create the defs file
    //	suite suite
    //     limit fast 1
    //	   family f
    //          inlimit /suite:fast
    //	   		task t1
    //	   		task t2
    //	   endfamily
    //	endsuite
    Defs defs;
    std::string limitName   = "fast";
    std::string pathToLimit = "/suite";
    std::string suitename   = "suite";
    family_ptr f            = Family::create("f");
    task_ptr t1             = Task::create("t1");
    task_ptr t2             = Task::create("t2");
    suite_ptr s             = Suite::create(suitename);
    {
        f->addTask(t1);
        f->addTask(t2);
        f->addInLimit(InLimit(limitName, pathToLimit));

        s->addFamily(f);
        s->addLimit(Limit(limitName, 1));

        defs.addSuite(s);
    }
    // cerr << defs << "\n";

    //*******************************************************************************
    // Resolve dependencies:: BEFORE calling beginCmd, should be a NO OP
    {
        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 0,
                            "Expected 0 tasks to submit but found " << jobsParam.submitted().size());
    }

    // ***********************************************************************
    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. Only one task should be submitted due to Limit
    //
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd(suitename)));
        BOOST_CHECK_MESSAGE(s->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s->state()));
        BOOST_CHECK_MESSAGE(f->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f->state()));
        BOOST_CHECK_MESSAGE(t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(t1->state()));
        BOOST_CHECK_MESSAGE(t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(t2->state()));
    }

    //*******************************************************************************
    // Resolve dependencies. No task should submit since we have reached the limit
    {
        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 0,
                            "Expected 0 task to submit but found " << jobsParam.submitted().size());
    }

    {
        std::string errorMsg;
        BOOST_CHECK_MESSAGE(defs.checkInvariants(errorMsg), errorMsg);
    }
}

BOOST_AUTO_TEST_CASE(test_limit1) {
    ECF_NAME_THIS_TEST();

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // Create the defs file
    //	suite suite
    //     limit disk 50
    //	   family f
    //          inlimit /suite:disk 20
    //	   		task t1
    //	   		task t2
    //	   		task t3
    //	   endfamily
    //	endsuite
    //
    //  Test that only 2 Tasks can run
    Defs defs;
    std::string limitName   = "disk";
    std::string pathToLimit = "/suite";
    std::string suitename   = "suite";
    family_ptr f            = Family::create("f");
    task_ptr t1             = Task::create("t1");
    task_ptr t2             = Task::create("t2");
    task_ptr t3             = Task::create("t3");
    suite_ptr s             = Suite::create(suitename);
    {
        f->addTask(t1);
        f->addTask(t2);
        f->addTask(t3);
        f->addInLimit(InLimit(limitName, pathToLimit, 20));

        s->addFamily(f);
        s->addLimit(Limit(limitName, 50));

        defs.addSuite(s);
    }
    // cerr << defs << "\n";

    //*******************************************************************************
    // Resolve dependencies:: BEFORE calling beginCmd, should be a NO OP
    {
        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 0,
                            "Expected 0 tasks to submit but found " << jobsParam.submitted().size());
    }

    // ***********************************************************************
    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. Only TWO task should be active due to Limit
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd(suitename)));
        BOOST_CHECK_MESSAGE(s->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s->state()));
        BOOST_CHECK_MESSAGE(f->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f->state()));
        BOOST_CHECK_MESSAGE(t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(t1->state()));
        BOOST_CHECK_MESSAGE(t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(t2->state()));
        BOOST_CHECK_MESSAGE(t3->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(t3->state()));
    }

    //*******************************************************************************
    // Resolve dependencies. No task should submit since we have reached the limit
    {
        JobsParam jobsParam; // create jobs = false, spawn jobs = false
        Jobs jobs(&defs);
        BOOST_CHECK_MESSAGE(jobs.generate(jobsParam), jobsParam.getErrorMsg());
        BOOST_CHECK_MESSAGE(jobsParam.submitted().size() == 0,
                            "Expected 0 task to submit but found " << jobsParam.submitted().size());
        if (jobsParam.submitted().size() != 0) {
            for (Submittable* t : jobsParam.submitted()) {
                cerr << "Submittable " << t->absNodePath() << "\n";
            }
        }
    }

    {
        std::string errorMsg;
        BOOST_CHECK_MESSAGE(defs.checkInvariants(errorMsg), errorMsg);
    }
}

BOOST_AUTO_TEST_CASE(test_limit_references_after_delete) {
    /// In-limit have a reference to a limit. This limit can be on another node. If that node is deleted
    /// The limits are also deleted, hence we need to ensure in-limit reference to limits that are being
    /// deleted are cleared. Currently we use shared_ptr to achieve this
    ECF_NAME_THIS_TEST();

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    //  Create the defs file
    //	suite suiteLimits    # the limit we want delete
    //     limit limit1  1
    //     limit limit2  1
    //     limit limit3  1
    //	endsuite
    //
    //	suite suite
    //	   family f
    //          inlimit /suiteLimits:limit1    # check limits references cleared after suiteLimits is deleted
    //	   		task t1
    //	   endfamily
    //	   family f1
    //          inlimit /suiteLimits:limit2
    //	   		task t1
    //	   endfamily
    //	   family f2
    //          inlimit /suiteLimits:limit3
    //	   		task t1
    //	   endfamily
    //	endsuite
    //
    // In this test case we will delete the suiteLimits, and then test to ensure the the in-limits
    // in suite have been reset.
    Defs defs;
    {
        suite_ptr suite = Suite::create("suiteLimits");
        suite->addLimit(Limit("limit1", 1));
        suite->addLimit(Limit("limit2", 1));
        suite->addLimit(Limit("limit3", 1));
        defs.addSuite(suite);
    }

    InLimit in_limit1("limit1", "/suiteLimits");
    InLimit in_limit2("limit2", "/suiteLimits");
    InLimit in_limit3("limit3", "/suiteLimits");
    {
        suite_ptr suite = Suite::create("suite");

        family_ptr fam = Family::create("f");
        fam->addTask(Task::create("t1"));
        fam->addInLimit(in_limit1);
        suite->addFamily(fam);

        family_ptr fam2 = Family::create("f1");
        fam2->addTask(Task::create("t1"));
        fam2->addInLimit(in_limit2);
        suite->addFamily(fam2);

        family_ptr fam3 = Family::create("f2");
        fam3->addTask(Task::create("t1"));
        fam3->addInLimit(in_limit3);
        suite->addFamily(fam3);

        defs.addSuite(suite);
    }

    // First check that in-limit reference have been setup
    node_ptr f  = defs.findAbsNode("/suite/f");
    node_ptr f1 = defs.findAbsNode("/suite/f1");
    node_ptr f2 = defs.findAbsNode("/suite/f2");
    BOOST_REQUIRE_MESSAGE(f.get() && f1.get() && f2.get(), "Failed to find family than contain the inlimits");

    BOOST_REQUIRE_MESSAGE(f->findInLimitByNameAndPath(in_limit1) && f1->findInLimitByNameAndPath(in_limit2) &&
                              f2->findInLimitByNameAndPath(in_limit3),
                          "Failed to find in-limits on the families" << ecf::as_string(defs, PrintStyle::DEFS));

    // Now check they all reference the limits
    BOOST_REQUIRE_MESSAGE(f->findLimitViaInLimit(in_limit1),
                          "Inlimit does not reference its limit. Post process failed ?");
    BOOST_REQUIRE_MESSAGE(f1->findLimitViaInLimit(in_limit2),
                          "Inlimit does not reference its limit. Post process failed ?");
    BOOST_REQUIRE_MESSAGE(f2->findLimitViaInLimit(in_limit3),
                          "Inlimit does not reference its limit. Post process failed ?");

    // Ok Now delete suiteLimits, as this is where the limits reside
    // *** It is extremely important that shared_ptr for '/suiteLimit' is in its own
    // *** scope, otherwise it will keep the 'suite' live, and NOT delete the limits
    {
        node_ptr suiteLimits = defs.findAbsNode("/suiteLimits");
        BOOST_REQUIRE_MESSAGE(suiteLimits.get(), "Could not find the suite we want to delete?");
        BOOST_REQUIRE_MESSAGE(defs.deleteChild(suiteLimits.get()), "Deletion failed?");
        BOOST_REQUIRE_MESSAGE(!defs.findAbsNode("/suiteLimits").get(), "Deletion failed?");
    }

    BOOST_REQUIRE_MESSAGE(!f->findLimitViaInLimit(in_limit1), "In-limit still references limit that has been deleted?");
    BOOST_REQUIRE_MESSAGE(!f1->findLimitViaInLimit(in_limit2),
                          "In-limit still references limit that has been deleted?");
    BOOST_REQUIRE_MESSAGE(!f2->findLimitViaInLimit(in_limit3),
                          "In-limit still references limit that has been deleted?");
}

BOOST_AUTO_TEST_CASE(test_limits_after_force_cmd) {
    ECF_NAME_THIS_TEST();

    // Create the following defs
    // suite s1
    //   limit A 10
    //   family f1
    //     inlimit A
    //     task t1
    //     task t2
    //   family f2
    //     inlimit A
    //     task t1
    //     task t2

    // When all the tasks are running/submitted state, limit A should have consumed 4 tokens
    // However if we how force family f2 to be complete, then the limit should be reset back to 2 tokens.
    // Repeating for family f1, should result with a Limit with no tokens
    Defs defs;
    suite_ptr s1 = defs.add_suite("s1");
    s1->addLimit(Limit("A", 10));
    family_ptr f1 = s1->add_family("f1");
    f1->addInLimit(InLimit("A", "/s1"));
    task_ptr f1_t1 = f1->add_task("t1");
    task_ptr f1_t2 = f1->add_task("t2");
    family_ptr f2  = s1->add_family("f2");
    f2->addInLimit(InLimit("A", "/s1"));
    task_ptr f2_t1 = f2->add_task("t1");
    task_ptr f2_t2 = f2->add_task("t2");
    // cout << defs;

    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. All tasks are within the limit and hence should start
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd("/s1")));
        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t2->state()));

        limit_ptr the_limit = s1->find_limit("A");
        BOOST_CHECK_MESSAGE(the_limit, "Could not find limit");
        BOOST_CHECK_MESSAGE(the_limit->value() == 4, "Expected limit value to be 4 but found " << the_limit->value());
    }

    {
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(
                f2->absNodePath(), "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t2->state()));

        limit_ptr the_limit = s1->find_limit("A");
        BOOST_CHECK_MESSAGE(the_limit, "Could not find limit");
        BOOST_CHECK_MESSAGE(the_limit->value() == 2, "Expected limit value to be 2 but found " << the_limit->value());
    }

    {
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(
                f1->absNodePath(), "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(s1->state() == NState::COMPLETE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::COMPLETE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::COMPLETE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::COMPLETE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t2->state()));

        limit_ptr the_limit = s1->find_limit("A");
        BOOST_CHECK_MESSAGE(the_limit, "Could not find limit");
        BOOST_CHECK_MESSAGE(the_limit->value() == 0, "Expected limit value to be 0 but found " << the_limit->value());
    }
}

BOOST_AUTO_TEST_CASE(test_limits_after_requeue_family_ECFLOW_196) {
    ECF_NAME_THIS_TEST();

    // This test is used to ensure that, requeue causes node to release tokens held by the Limits

    // Create the following defs
    // suite s1
    //   limit A 10
    //   family f1
    //     inlimit A
    //     task t1
    //     task t2
    //   family f2
    //     inlimit A
    //     task t1
    //     task t2

    // When all the tasks are running/submitted state, limit A should have consumed 4 tokens
    // However if we how force family f2 to be requeud, then the limit should be reset back to 2 tokens.
    Defs defs;
    suite_ptr s1 = defs.add_suite("s1");
    s1->addLimit(Limit("A", 10));
    family_ptr f1 = s1->add_family("f1");
    f1->addInLimit(InLimit("A", "/s1"));
    task_ptr f1_t1 = f1->add_task("t1");
    task_ptr f1_t2 = f1->add_task("t2");
    family_ptr f2  = s1->add_family("f2");
    f2->addInLimit(InLimit("A", "/s1"));
    task_ptr f2_t1 = f2->add_task("t1");
    task_ptr f2_t2 = f2->add_task("t2");
    // cout << defs;

    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. All tasks are within the limit and hence should start
    int expected_limit_value = 4;
    limit_ptr the_limit      = s1->find_limit("A");
    BOOST_CHECK_MESSAGE(the_limit, "Could not find limit");
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd("/s1")));
        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t2->state()));

        BOOST_CHECK_MESSAGE(the_limit->value() == expected_limit_value,
                            "Expected limit value to be 4 but found " << the_limit->value());
    }

    {
        expected_limit_value = 2;
        // We now want to re-queue node f2, and check to see that limit tokens are released after the re-queue
        // However, the Mock server, will call resolve dependencies after re-queue command , and place nodes *back* into
        // active state, to *STOP* this, we will add a trigger, so that the dependencies will not resolve, and hence
        // nodes stay queued
        f2->add_trigger("1 == 0");
        TestHelper::invokeRequest(&defs, Cmd_ptr(new RequeueNodeCmd(f2->absNodePath(), RequeueNodeCmd::FORCE)));

        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t2->state()));

        BOOST_CHECK_MESSAGE(the_limit->value() == expected_limit_value,
                            "Expected limit value to be 2 but found " << the_limit->value());
    }

    {
        expected_limit_value = 0;
        // We now want to re-queue node f1, and check to see that limit tokens are released after the re-queue
        // However, the Mock server, will call resolve dependencies after re-queue command , and place nodes *back* into
        // active state, to *STOP* this, we will add a trigger, so that the dependencies will not resolve, and hence
        // nodes stay queued
        f1->add_trigger("1 == 0");
        TestHelper::invokeRequest(&defs, Cmd_ptr(new RequeueNodeCmd(f1->absNodePath(), RequeueNodeCmd::FORCE)));

        BOOST_CHECK_MESSAGE(s1->state() == NState::QUEUED,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::QUEUED,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::QUEUED,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::QUEUED,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t2->state()));

        BOOST_CHECK_MESSAGE(the_limit->value() == expected_limit_value,
                            "Expected limit value to be 0 but found " << the_limit->value());
    }
}

BOOST_AUTO_TEST_CASE(test_limits_after_requeue_task_ECFLOW_196) {
    ECF_NAME_THIS_TEST();

    // This test is used to ensure that, requeue causes node to release tokens held by the Limits

    // Create the following def, i.e using internal and external limits
    // suite s0
    //   limit X 5
    // suite s1
    //   limit A 10
    //   limit B 5
    //   family f1
    //     inlimit A
    //     inlimit B
    //     inlimit X /s0/
    //     task t1
    //     task t2
    //   family f2
    //     inlimit A
    //     inlimit B
    //     inlimit X /s0/
    //     task t1
    //     task t2

    // When all the tasks are running/submitted state, limit A should have consumed 4 tokens
    // However if we how force task to reque, then the token should be released
    Defs defs;
    suite_ptr s0 = defs.add_suite("s0");
    s0->addLimit(Limit("X", 10));

    suite_ptr s1 = defs.add_suite("s1");
    s1->addLimit(Limit("A", 10));
    s1->addLimit(Limit("B", 5));
    family_ptr f1 = s1->add_family("f1");
    f1->addInLimit(InLimit("A", "/s1"));
    f1->addInLimit(InLimit("B", "/s1"));
    f1->addInLimit(InLimit("X", "/s0"));
    task_ptr f1_t1 = f1->add_task("t1");
    task_ptr f1_t2 = f1->add_task("t2");
    family_ptr f2  = s1->add_family("f2");
    f2->addInLimit(InLimit("A", "/s1"));
    f2->addInLimit(InLimit("B", "/s1"));
    f2->addInLimit(InLimit("X", "/s0"));
    task_ptr f2_t1 = f2->add_task("t1");
    task_ptr f2_t2 = f2->add_task("t2");

    std::vector<task_ptr> tasks;
    tasks.push_back(f1_t1);
    tasks.push_back(f1_t2);
    tasks.push_back(f2_t1);
    tasks.push_back(f2_t2);
    // cout << defs;

    limit_ptr the_A_limit = s1->find_limit("A");
    limit_ptr the_B_limit = s1->find_limit("B");
    limit_ptr the_X_limit = s0->find_limit("X");
    BOOST_CHECK_MESSAGE(the_A_limit && the_B_limit && the_X_limit, "Could not find limits");

    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. All tasks are within the limit and hence should start
    int expected_limit_value = 4;
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd("/s1")));
        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t2->state()));

        BOOST_CHECK_MESSAGE(the_A_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_A_limit->value());
        BOOST_CHECK_MESSAGE(the_B_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_B_limit->value());
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());
    }

    {
        // We now want to re-queue node tasks, and check to see that limit tokens are released after the re-queue
        // However, the Mock server, will call resolve dependencies after re-queue command , and place nodes *back* into
        // active state, to *STOP* this, we will add a trigger, so that the dependencies will not resolve, and hence
        // nodes stay queued
        f1->add_trigger("1 == 0");
        f2->add_trigger("1 == 0");

        for (auto& task : tasks) {
            TestHelper::invokeRequest(&defs, Cmd_ptr(new RequeueNodeCmd(task->absNodePath(), RequeueNodeCmd::FORCE)));
            expected_limit_value--;
            BOOST_CHECK_MESSAGE(the_A_limit->value() == expected_limit_value,
                                "Expected limit value " << expected_limit_value << " but found "
                                                        << the_A_limit->value());
            BOOST_CHECK_MESSAGE(the_B_limit->value() == expected_limit_value,
                                "Expected limit value " << expected_limit_value << " but found "
                                                        << the_B_limit->value());
            BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                                "Expected limit value " << expected_limit_value << " but found "
                                                        << the_X_limit->value());
        }
    }
}

BOOST_AUTO_TEST_CASE(test_inlimit_with_family_ECFLOW_878) {
    ECF_NAME_THIS_TEST();

    // This test places a limit on the families. Should ignore the tasks
    // With this test only 1 family can start at a time
    //
    // Create the following def. with inlimit -n, we limit node only
    // suite s0
    //   limit X 1   # Only allow one family at a time to start
    // suite s1
    //   family f1
    //     inlimit -n X
    //     task t1
    //     task t2
    //   family f2
    //     inlimit -n X
    //     task t1
    //     task t2
    //   family f3
    //     inlimit -n X
    //     task t1
    //     task t2

    Defs defs;
    suite_ptr s0 = defs.add_suite("s0");
    s0->addLimit(Limit("X", 1));

    suite_ptr s1  = defs.add_suite("s1");
    family_ptr f1 = s1->add_family("f1");
    f1->addInLimit(InLimit("X", "/s0", 1, true));
    task_ptr f1_t1 = f1->add_task("t1");
    task_ptr f1_t2 = f1->add_task("t2");
    family_ptr f2  = s1->add_family("f2");
    f2->addInLimit(InLimit("X", "/s0", 1, true));
    task_ptr f2_t1 = f2->add_task("t1");
    task_ptr f2_t2 = f2->add_task("t2");
    family_ptr f3  = s1->add_family("f3");
    f3->addInLimit(InLimit("X", "/s0", 1, true));
    task_ptr f3_t1 = f3->add_task("t1");
    task_ptr f3_t2 = f3->add_task("t2");

    // cout << defs;
    limit_ptr the_X_limit = s0->find_limit("X");
    BOOST_CHECK_MESSAGE(the_X_limit, "Could not find limits");

    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. Only 2 task should start
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd("/s1")));

        //      PrintStyle::setStyle(PrintStyle::MIGRATE);
        //      cout << defs;

        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 1;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // should only have 1 path, and it should be path to family f1
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(x_limit_paths.size() == 1, "Expected one path in limit but found " << x_limit_paths.size());
        BOOST_CHECK_MESSAGE(x_limit_paths.find(f1->absNodePath()) != x_limit_paths.end(),
                            "Expected to find path " << f1->absNodePath());

        //      // why f2_t1 is queued
        //      cout << "why ============================================";
        //      std::vector<std::string> vec;
        //      f2_t1->bottom_up_why(vec);
        //      for(size_t i =0; i < vec.size(); i++) {
        //         cout << "why:" << i << " " << vec[i] << "\n";
        //      }
    }

    // Now set tasks f1_t1, f1_t2 to complete, this should release the limit, so that next family can run
    {
        std::vector<std::string> task_paths;
        task_paths.push_back(f1_t1->absNodePath());
        task_paths.push_back(f1_t2->absNodePath());
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(task_paths, "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(f1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 1;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // Paths should be empty
        // should only have 1 path, and it should be path to family f1
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(x_limit_paths.size() == 1, "Expected one path in limit but found " << x_limit_paths.size());
        BOOST_CHECK_MESSAGE(x_limit_paths.find(f2->absNodePath()) != x_limit_paths.end(),
                            "Expected to find path " << f2->absNodePath());
    }

    // Now set tasks f2_t1, f2_t2 to complete, this should release the limit, so that next family can run
    {
        std::vector<std::string> task_paths;
        task_paths.push_back(f2_t1->absNodePath());
        task_paths.push_back(f2_t2->absNodePath());
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(task_paths, "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(f1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 1;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // should only have 1 path, and it should be path to family f3
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(x_limit_paths.size() == 1, "Expected one path in limit but found " << x_limit_paths.size());
        BOOST_CHECK_MESSAGE(x_limit_paths.find(f3->absNodePath()) != x_limit_paths.end(),
                            "Expected to find path " << f3->absNodePath());
    }

    // Now set tasks f3_t1, f3_t2 to complete, this should release the limit
    {
        std::vector<std::string> task_paths;
        task_paths.push_back(f3_t1->absNodePath());
        task_paths.push_back(f3_t2->absNodePath());
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(task_paths, "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(f1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 0;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // Paths should be empty
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(the_X_limit->paths().empty(), "Expected no paths, but found" << x_limit_paths.size());
    }

    //   PrintStyle::setStyle(PrintStyle::MIGRATE);
    //   cout << defs;
}

BOOST_AUTO_TEST_CASE(test_inlimit_ECFLOW_878) {
    ECF_NAME_THIS_TEST();

    // This test places a limit on the families. Should ignore the tasks
    // With this test only 1 family can start at a time.
    // However we *ALSO* want to constrain the tasks, to only 1 task at a time
    //
    // Create the following def. with inlimit -n, we limit node only
    // suite s0
    //   limit X 1   # Only allow one family at a time to start
    //   limit T 1   # Only allow one task at a time to start
    // suite s1
    //   family f1
    //     inlimit -n X
    //     inlimit T
    //     task t1
    //     task t2
    //   family f2
    //     inlimit -n X
    //     inlimit T
    //     task t1
    //     task t2
    //   family f3
    //     inlimit -n X
    //     inlimit T
    //     task t1
    //     task t2

    Defs defs;
    suite_ptr s0 = defs.add_suite("s0");
    s0->addLimit(Limit("X", 1));
    s0->addLimit(Limit("T", 1));

    suite_ptr s1  = defs.add_suite("s1");
    family_ptr f1 = s1->add_family("f1");
    f1->addInLimit(InLimit("X", "/s0", 1, true));
    f1->addInLimit(InLimit("T", "/s0", 1));
    task_ptr f1_t1 = f1->add_task("t1");
    task_ptr f1_t2 = f1->add_task("t2");
    family_ptr f2  = s1->add_family("f2");
    f2->addInLimit(InLimit("X", "/s0", 1, true));
    f2->addInLimit(InLimit("T", "/s0", 1));
    task_ptr f2_t1 = f2->add_task("t1");
    task_ptr f2_t2 = f2->add_task("t2");
    family_ptr f3  = s1->add_family("f3");
    f3->addInLimit(InLimit("X", "/s0", 1, true));
    f3->addInLimit(InLimit("T", "/s0", 1));
    task_ptr f3_t1 = f3->add_task("t1");
    task_ptr f3_t2 = f3->add_task("t2");

    // cout << defs;
    limit_ptr the_X_limit = s0->find_limit("X");
    limit_ptr the_T_limit = s0->find_limit("T");
    BOOST_CHECK_MESSAGE(the_X_limit && the_T_limit, "Could not find limits");

    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state to ACTIVE for submitted jobs
    //
    //  Resolve dependencies. Only 1 task should start
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd("/s1")));

        // PrintStyle::setStyle(PrintStyle::MIGRATE);
        // cout << defs;

        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 1;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // should only have 1 path, and it should be path to family f1
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(x_limit_paths.size() == 1, "Expected one path in limit but found " << x_limit_paths.size());
        BOOST_CHECK_MESSAGE(x_limit_paths.find(f1->absNodePath()) != x_limit_paths.end(),
                            "Expected to find path " << f1->absNodePath());

        const std::set<std::string>& t_limit_paths = the_T_limit->paths();
        BOOST_CHECK_MESSAGE(t_limit_paths.size() == 1, "Expected one path in limit but found " << t_limit_paths.size());

        //      // why f2_t1 is queued
        //      cout << "why ============================================";
        //      std::vector<std::string> vec;
        //      f2_t1->bottom_up_why(vec);
        //      for(size_t i =0; i < vec.size(); i++) {
        //         cout << "why:" << i << " " << vec[i] << "\n";
        //      }
    }

    // Now set tasks f1_t1 complete, this should release  t1_t2
    {
        std::vector<std::string> task_paths;
        task_paths.push_back(f1_t1->absNodePath());
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(task_paths, "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 1;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // should only have 1 path, and it should be path to family f1
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(x_limit_paths.size() == 1, "Expected one path in limit but found " << x_limit_paths.size());
        BOOST_CHECK_MESSAGE(x_limit_paths.find(f1->absNodePath()) != x_limit_paths.end(),
                            "Expected to find path " << f1->absNodePath());

        const std::set<std::string>& t_limit_paths = the_T_limit->paths();
        BOOST_CHECK_MESSAGE(t_limit_paths.size() == 1, "Expected one path in limit but found " << t_limit_paths.size());
    }

    // Now set tasks f1_t2 complete, this should release  f2_1
    {
        std::vector<std::string> task_paths;
        task_paths.push_back(f1_t2->absNodePath());
        TestHelper::invokeRequest(
            &defs,
            Cmd_ptr(new ForceCmd(task_paths, "complete", true /*recursive */, false /* set Repeat to last value */)));

        BOOST_CHECK_MESSAGE(f1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::COMPLETE,
                            "expected state NState::COMPLETE, but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::QUEUED,
                            "expected state NState::QUEUED, but found to be " << NState::toString(f3_t2->state()));

        int expected_limit_value = 1;
        BOOST_CHECK_MESSAGE(the_X_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_X_limit->value());

        // should only have 1 path, and it should be path to family f2
        const std::set<std::string>& x_limit_paths = the_X_limit->paths();
        BOOST_CHECK_MESSAGE(x_limit_paths.size() == 1, "Expected one path in limit but found " << x_limit_paths.size());
        BOOST_CHECK_MESSAGE(x_limit_paths.find(f2->absNodePath()) != x_limit_paths.end(),
                            "Expected to find path " << f2->absNodePath());

        const std::set<std::string>& t_limit_paths = the_T_limit->paths();
        BOOST_CHECK_MESSAGE(t_limit_paths.size() == 1, "Expected one path in limit but found " << t_limit_paths.size());
    }

    // PrintStyle::setStyle(PrintStyle::MIGRATE);
    // cout << defs;
}

BOOST_AUTO_TEST_CASE(test_inlimit_submission_only) {
    ECF_NAME_THIS_TEST();

    // Create the following def. with inlimit -s, we limit submission
    // suite s0
    //   limit T 1   # Only allow one task at a time to start
    // suite s1
    //   family f1
    //     inlimit -s T
    //     task t1
    //     task t2
    //   family f2
    //     inlimit -s T
    //     task t1
    //     task t2
    //   family f3
    //     inlimit -s T
    //     task t1
    //     task t2

    Defs defs;
    suite_ptr s0 = defs.add_suite("s0");
    s0->addLimit(Limit("T", 1));

    suite_ptr s1  = defs.add_suite("s1");
    family_ptr f1 = s1->add_family("f1");
    f1->addInLimit(InLimit("T", "/s0", 1, false, true));
    task_ptr f1_t1 = f1->add_task("t1");
    task_ptr f1_t2 = f1->add_task("t2");
    family_ptr f2  = s1->add_family("f2");
    f2->addInLimit(InLimit("T", "/s0", 1, false, true));
    task_ptr f2_t1 = f2->add_task("t1");
    task_ptr f2_t2 = f2->add_task("t2");
    family_ptr f3  = s1->add_family("f3");
    f3->addInLimit(InLimit("T", "/s0", 1, false, true));
    task_ptr f3_t1 = f3->add_task("t1");
    task_ptr f3_t2 = f3->add_task("t2");

    // cout << defs;
    limit_ptr the_T_limit = s0->find_limit("T");
    BOOST_CHECK_MESSAGE(the_T_limit, "Could not find limits");

    // Create a request to begin suite
    // make sure chosen suite can begin to resolve dependencies.
    // beginning the suite will:
    //     1/ set all children to the QUEUED state
    //     2/ Begin job submission, and hence changes state  queued->submitted->active
    //        Hence under test scenario there is no delay between submitted->active , hence limit is incremented and
    //        decrement
    {
        TestHelper::invokeRequest(&defs, Cmd_ptr(new BeginCmd("/s1")));

        // PrintStyle::setStyle(PrintStyle::MIGRATE);
        // cout << defs;

        BOOST_CHECK_MESSAGE(s1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(s1->state()));
        BOOST_CHECK_MESSAGE(f1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1->state()));
        BOOST_CHECK_MESSAGE(f1_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE, but found to be " << NState::toString(f1_t1->state()));
        BOOST_CHECK_MESSAGE(f1_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f1_t2->state()));
        BOOST_CHECK_MESSAGE(f2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f2->state()));
        BOOST_CHECK_MESSAGE(f2_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f2_t1->state()));
        BOOST_CHECK_MESSAGE(f2_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f2_t2->state()));
        BOOST_CHECK_MESSAGE(f3->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f3->state()));
        BOOST_CHECK_MESSAGE(f3_t1->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f3_t1->state()));
        BOOST_CHECK_MESSAGE(f3_t2->state() == NState::ACTIVE,
                            "expected state NState::ACTIVE , but found to be " << NState::toString(f3_t2->state()));

        // The active state *decrements* the inlimit/limit value
        int expected_limit_value = 0;
        BOOST_CHECK_MESSAGE(the_T_limit->value() == expected_limit_value,
                            "Expected limit value to be " << expected_limit_value << " but found "
                                                          << the_T_limit->value());

        //      // why f2_t1 is queued
        //      cout << "why ============================================";
        //      std::vector<std::string> vec;
        //      f2_t1->bottom_up_why(vec);
        //      for(size_t i =0; i < vec.size(); i++) {
        //         cout << "why:" << i << " " << vec[i] << "\n";
        //      }
    }

    // PrintStyle::setStyle(PrintStyle::MIGRATE);
    // cout << defs;

    /// Destroy System singleton to avoid valgrind from complaining
    System::destroy();
}

BOOST_AUTO_TEST_CASE(test_destroy_log) {
    Log::destroy();
    fs::remove("test_add_log.log");
    BOOST_CHECK_MESSAGE(true, "stop boost test form complaining");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()