chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
 
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenTap.Plugins.BasicSteps;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class TestPlanBreakRequestedTests 
    {
 
        [AllowAnyChild]
        public class WithEvents : TestStep
        {
            public override void Run()
            {
                OfferBreak();
                RunChildSteps();
            }
        }
 
 
        private int _pausedDetectedCount;
        private int _startingDetectedCount;
 
        [Test]
        public void RunFlatTestplan()
        {
            TestPlan testplan = new TestPlan();
            WithEvents step1 = new WithEvents();
            WithEvents step2 = new WithEvents();
            WithEvents step3 = new WithEvents();
            WithEvents step4 = new WithEvents();
 
            testplan.Steps.Add(step1);
            testplan.Steps.Add(step2);
            testplan.Steps.Add(step3);
            testplan.Steps.Add(step4);
 
            testplan.BreakOffered += _testplan_TestStepPaused;
 
            _pausedDetectedCount = 0;
            _startingDetectedCount = 0;
 
            testplan.Execute();
            //Since the plan is flat, the starting and paused count is the same.
            Assert.IsTrue(_startingDetectedCount == TotalEnabledStepsInTestPlan(testplan));
            Assert.IsTrue(_pausedDetectedCount == TotalEnabledStepsInTestPlan(testplan));
        }
 
 
        private TestPlan testplanJumpToStep;
        private bool firstBreak = true;
        [Test]
        public void BreakJumpToStepRunNull()
        {
            testplanJumpToStep = new TestPlan();
            SequenceStep step1 = new SequenceStep();
            SequenceStep step2 = new SequenceStep();
            SequenceStep step3 = new SequenceStep();
 
            testplanJumpToStep.Steps.Add(step1);
            testplanJumpToStep.Steps.Add(step2);
            testplanJumpToStep.Steps.Add(step3);
 
            testplanJumpToStep.BreakOffered += Testplan_BreakOffered;
 
            firstBreak = true;
            testplanJumpToStep.Execute();
 
            foreach (var step in testplanJumpToStep.Steps)
                Assert.IsNull(step.StepRun);
        }
 
        private void Testplan_BreakOffered(object sender, BreakOfferedEventArgs e)
        {
            if (firstBreak)
            {
                e.JumpToStep = testplanJumpToStep.Steps.Last().Id;
                firstBreak = false;
            }
        }
 
        [Test]
        [Repeat(2)]
        public void BreakAbortStepRunNull()
        {
            TestPlan testPlan = new TestPlan();
            SequenceStep step1 = new SequenceStep();
            SequenceStep step2 = new SequenceStep();
            SequenceStep step3 = new SequenceStep();
 
            testPlan.Steps.Add(step1);
            testPlan.Steps.Add(step2);
            testPlan.Steps.Add(step3);
 
            TapThread.WithNewContext(() =>
            {
                var planThread = TapThread.Current;
                testPlan.BreakOffered += (s, e) => planThread.Abort();
                testPlan.Execute();
                Assert.IsTrue(TapThread.Current.AbortToken.IsCancellationRequested);
            });
            Assert.IsFalse(TapThread.Current.AbortToken.IsCancellationRequested);
 
            foreach (var step in testPlan.Steps)
                Assert.IsNull(step.StepRun);
        }
 
        static ThreadHierarchyLocal<object> tapThreadLocalObject = new ThreadHierarchyLocal<object>();
        [Test]
        public void HierarchyLocalWithThreads()
        {
            // This test verifies that ThreadHierarchyLocal work with different thread contexts.
            //
            tapThreadLocalObject.LocalValue = (int)5;
            // At child thread level it should be 5.
            TapThread.WithNewContext(() => Assert.AreEqual(5, (int)tapThreadLocalObject.LocalValue));
            TapThread.WithNewContext(() =>
            {
                tapThreadLocalObject.LocalValue = (int)6;
                // At child-child thread level it should be 6.    
                TapThread.WithNewContext(() => Assert.AreEqual(6, (int)tapThreadLocalObject.LocalValue));
            });
            
            // At this level it should still be 5.
            TapThread.WithNewContext(() => Assert.AreEqual(5, (int)tapThreadLocalObject.LocalValue));
            
            // for no parent it should not have been set.
            TapThread.WithNewContext(() => Assert.IsNull(tapThreadLocalObject.LocalValue), parent: null);
        }
 
        [Test]
        public void RunHierarchical()
        {
            TestPlan testplan = new TestPlan();
 
            var grandParent = new WithEvents();
            testplan.Steps.Add(grandParent);
 
            var parent = new WithEvents();
            grandParent.ChildTestSteps.Add(parent);
 
            var child1 = new WithEvents();
            parent.ChildTestSteps.Add(child1);
 
            var child2 = new WithEvents();
            parent.ChildTestSteps.Add(child2);
 
            testplan.BreakOffered += _testplan_TestStepPaused;
 
            _pausedDetectedCount = 0;
            _startingDetectedCount = 0;
 
            testplan.Execute();
 
            // Since ALL the steps are of type WithEvents, we continue to get 
            // a pausedcount = startingCount = totalcount.
            Assert.IsTrue(_startingDetectedCount == TotalEnabledStepsInTestPlan(testplan));
            Assert.IsTrue(_pausedDetectedCount == TotalEnabledStepsInTestPlan(testplan));
        }
 
        [Test]
        public void RunHierarchical2()
        {
            TestPlan testplan = new TestPlan();
 
            var grandParent = new SequenceStep();
            testplan.Steps.Add(grandParent);
 
            var parent = new SequenceStep();
            grandParent.ChildTestSteps.Add(parent);
 
            var child1 = new WithEvents();
            parent.ChildTestSteps.Add(child1);
 
            var child2 = new WithEvents();
            parent.ChildTestSteps.Add(child2);
 
            testplan.BreakOffered += _testplan_TestStepPaused;
 
            _pausedDetectedCount = 0;
            _startingDetectedCount = 0;
 
            testplan.Execute();
 
            // Since only two of the steps are with events, we get
            // _starting = total
            // _pausedCount = 2; 
            var test = TotalEnabledStepsInTestPlan(testplan);
            Assert.IsTrue(_startingDetectedCount == TotalEnabledStepsInTestPlan(testplan));
            Assert.IsTrue(_pausedDetectedCount == 2);
        }
 
 
        [Test]
        public void RunLoop()
        {
            TestPlan testplan = new TestPlan();
 
            var grandParent = new RepeatStep();
            uint loopCount = 7;
            grandParent.Count = loopCount;
            grandParent.Action = RepeatStep.RepeatStepAction.Fixed_Count;
            testplan.Steps.Add(grandParent);
 
            var parent = new WithEvents();
            grandParent.ChildTestSteps.Add(parent);
 
            testplan.BreakOffered += _testplan_TestStepPaused;
 
            _pausedDetectedCount = 0;
            _startingDetectedCount = 0;
 
            testplan.Execute();
 
            //We will actually have loopCount + 1 starts.
            // We get a start for each of the loops around "withevents steps", plus one for the outer loop "FixedCountLoop" step
            Assert.AreEqual(loopCount + 1, _startingDetectedCount, "StartingDetectedCount");
 
            //Since fixed count loop does NOT have a pause, it will not count in the pause count.
            Assert.AreEqual(loopCount, _pausedDetectedCount, "PausedDetectedCount");
        }
 
        void _testplan_TestStepPaused(object sender, BreakOfferedEventArgs e)
        {
 
            if (e.IsTestStepStarting)
                _startingDetectedCount++;
            else
                _pausedDetectedCount++;
        }
 
        int TotalEnabledStepsInTestPlan(TestPlan testplan)
        {
            int total = testplan.EnabledSteps.Count;
            foreach (ITestStep testStep in testplan.Steps)
            {
                total += testStep.RecursivelyGetChildSteps(TestStepSearch.EnabledOnly).Count();
            }
            return total;
        }
 
        [Test]
        public void ParmsCheck()
        {
            TestPlan testplan = new TestPlan();
            DelayStep step1 = new DelayStep();
            string myName = "fred";
            step1.Name = myName;
            testplan.Steps.Add(step1);
 
            testplan.BreakOffered += (s, e) =>
            {
                Assert.IsTrue(e.TestStepRun.Verdict == Verdict.NotSet);
                //Assert.IsTrue(e.TestStepRun.Step.GetStepPath().Contains(myName));
            };
            testplan.Execute();
 
        }
 
        public class SetVerdictStep : TestStep
        {
            public Verdict SetVerdict { get; set; } = Verdict.NotSet;
 
            public override void Run()
            {
                UpgradeVerdict(SetVerdict);
            }
        }
 
        [TestCase(Verdict.Pass, Verdict.Pass)]
        [TestCase(Verdict.Fail, Verdict.Fail)]
        [TestCase(Verdict.Error, Verdict.Error)]
        public void TestDeadlockInReferencedPlan(Verdict inputVerdict, Verdict expectedVerdict)
        {
            bool wrap = false;
            // We construct this testplan:
            // Top
            // -- Outer Sequence
            // ---- Inner Sequence 
            // ------ SetVerdictStep
            // -- If Verdict(Inner Sequence)
        
            var childPlan = new TestPlan();
            var outerSequence2 = new SequenceStep();
            childPlan.ChildTestSteps.Add(outerSequence2);
 
            ITestStepParent outerSequence = wrap ? outerSequence2 : childPlan;
 
            var innerSequence = new SequenceStep();
            var setVerdictStep = new SetVerdictStep() { SetVerdict = inputVerdict };
            outerSequence.ChildTestSteps.Add(innerSequence);
            innerSequence.ChildTestSteps.Add(setVerdictStep);
 
            BreakConditionProperty.SetBreakCondition(setVerdictStep, BreakCondition.BreakOnFail | BreakCondition.BreakOnError);
 
            var ifStep = new IfStep
            {
                InputVerdict =
                {
                    Step = innerSequence,
                    Property = TypeData.GetTypeData(innerSequence).GetMember(nameof(innerSequence.Verdict))
                },
                Action = IfStep.IfStepAction.RunChildren,
                TargetVerdict = Verdict.Fail
            };
            outerSequence.ChildTestSteps.Add(ifStep);
 
            var run = childPlan.ExecuteAsync();
            Task.WaitAny(run, Task.Delay(TimeSpan.FromSeconds(1000)));
            Assert.That(run.Wait(0), Is.True, "Testplan timed out");
            Assert.That(run.Result.Verdict, Is.EqualTo(expectedVerdict));
        }
    }
}