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
//            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.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
 
namespace OpenTap
{
    /// <summary>
    /// Object used to indicate that a TestStep is interested in results from another TestStep. 
    /// A public property of this type should exist on the interested TestStep.
    /// Methods defined in this interface are called on all instances found as properties on TestSteps in the TestPlan.
    /// </summary>
    public interface IResultSink
    {
        /// <summary>
        /// Called when a TestStep publishes results. This is happening in a background thread.
        /// </summary>
        void OnResultPublished(TestStepRun run, ResultTable table);
 
        /// <summary>
        /// Called when a TestStep is starting. This is happening in a background thread.
        /// </summary>
        void OnTestStepRunStart(TestStepRun stepRun);
 
        /// <summary>
        /// Called when a TestStep is completed. This is happening in a background thread. ResultPublished will not be called anymore with this TestStepRun after this.
        /// </summary>
        void OnTestStepRunCompleted(TestStepRun stepRun);
 
        /// <summary>
        /// Called when a TestPlan starts running. This is happening in a background thread.
        /// </summary>
        void OnTestPlanRunStart(TestPlanRun stepRun);
 
        /// <summary>
        /// Called when a TestPlan run is completed. This is happening in a background thread. 
        /// </summary>
        void OnTestPlanRunCompleted(TestPlanRun stepRun);
    }
 
    [Browsable(false)]
    internal class ResultSinkListener : ResultListener
    {
        private IEnumerable<IResultSink> currentSinks;
        Dictionary<Guid, TestStepRun> currentStepRuns = new Dictionary<Guid, TestStepRun>();
 
        public ResultSinkListener(IEnumerable<IResultSink> sinks)
        {
            currentSinks = sinks;
        }
 
        public override void OnTestPlanRunStart(TestPlanRun planRun)
        {
            foreach (IResultSink sink in currentSinks)
            {
                try
                {
                    sink.OnTestPlanRunStart(planRun);
                }
                catch(Exception ex)
                {
                    Log.Error($"{TypeData.GetTypeData(sink).Name} caused an error.");
                    Log.Debug(ex);
                }
            }
        }
 
        public override void OnTestPlanRunCompleted(TestPlanRun planRun, Stream log)
        {
            foreach (IResultSink sink in currentSinks)
            {
                try
                {
                    sink.OnTestPlanRunCompleted(planRun);
                }
                catch (Exception ex)
                {
                    Log.Error($"{TypeData.GetTypeData(sink).Name} caused an error.");
                    Log.Debug(ex);
                }
            }
        }
 
        public override void OnTestStepRunStart(TestStepRun stepRun)
        {
            currentStepRuns.Add(stepRun.Id, stepRun);
            foreach (IResultSink sink in currentSinks)
            {
                try
                {
                    sink.OnTestStepRunStart(stepRun);
                }
                catch (Exception ex)
                {
                    Log.Error($"{TypeData.GetTypeData(sink).Name} caused an error.");
                    Log.Debug(ex);
                }
            }
        }
 
        public override void OnTestStepRunCompleted(TestStepRun stepRun)
        {
            currentStepRuns.Remove(stepRun.Id);
            foreach (IResultSink sink in currentSinks)
            {
                try
                {
                    sink.OnTestStepRunCompleted(stepRun);
                }
                catch (Exception ex)
                {
                    Log.Error($"{TypeData.GetTypeData(sink).Name} caused an error.");
                    Log.Debug(ex);
                }
            }
        }
 
        public override void OnResultPublished(Guid stepRunId, ResultTable result)
        {
            foreach (IResultSink sink in currentSinks)
            {
                try
                {
                    sink.OnResultPublished(currentStepRuns[stepRunId], result);
                }
                catch (Exception ex)
                {
                    Log.Error($"{TypeData.GetTypeData(sink).Name} caused an error.");
                    Log.Debug(ex);
                }
            }
        }
    }
 
    /// <summary>
    /// ResultSink that will provide the first result from a given result column published by a given TestStep.
    /// When SourceTestStep is inside a loop step, only results from the last iteration of SourceTestStep is accessible.
    /// </summary>
    public class ScalarResultSink<T> : IResultSink where T : IConvertible
    {
        private Queue<T> Result;
        private ManualResetEvent ItemsInQueue;
 
        /// <summary>
        /// The ID of the source TestStep that we are interested in results from.
        /// </summary>
        public ITestStep SourceTestStep { get; set; }
 
        /// <summary>
        /// The name of the result column to get the result from.
        /// </summary>
        public string ResultColumnName { get; set; }
 
        private ITestStep ListeningStep;
 
        /// <summary>
        /// Creates an instance. This should probably be called from the constructor of the TestStep.
        /// </summary>
        /// <example>
        /// <code>
        ///public class ListeningStepExample : TestStep
        ///{
        ///    public ITestStep SourceStep { get => Sink.SourceTestStep; set => Sink.SourceTestStep = value; }
        ///    public ScalarResultSink&lt;double&gt; Sink { get; set; }
        ///
        ///    public ListeningStepExample()
        ///    {
        ///        Sink = new ScalarResultSink&lt;double&gt;(this);
        ///    }
        ///
        ///    public override void Run()
        ///    {
        ///        log.Debug("Result was: {0}", Sink.GetResult(TapThread.Current.AbortToken));
        ///    }
        ///}
        /// </code>
        /// </example>
        /// <param name="listeningStep"></param>
        public ScalarResultSink(ITestStep listeningStep)
        {
            Result = new Queue<T>();
            ListeningStep = listeningStep;
        }
 
        /// <summary>
        /// Called by TestSteps when the result is needed. Blocks until a result is available.
        /// </summary>
        public T GetResult(CancellationToken ct)
        {
            WaitHandle.WaitAny(new [] { ItemsInQueue, ct.WaitHandle });
            ct.ThrowIfCancellationRequested();
            lock (Result)
            {
                var res = Result.Dequeue();
                if (!Result.Any())
                    ItemsInQueue.Reset();
                return res;
            }
        }
 
        /// <summary>
        /// Called by TestSteps when the result is needed. Returns true if a result is available.
        /// </summary>
        public bool TryGetResult(out T result)
        {
            lock (Result)
            {
                if (Result.Any())
                {
                    result = Result.Dequeue();
                    if (!Result.Any())
                        ItemsInQueue.Reset();
                    return true;
                }
            }
            result = default(T);
            return false;
        }
 
        /// <summary>
        /// Resets result collection when a new run of the <see cref="SourceTestStep"/> is started.
        /// </summary>
        public void OnTestStepRunStart(TestStepRun run)
        {
            if (run.TestStepId == SourceTestStep?.Id)
            {
                lock (Result)
                {
                    ItemsInQueue.Reset();
                    Result.Clear();
                }
            }
        }
 
        /// <summary>
        /// Called when a TestStep is completed.
        /// </summary>
        public void OnTestStepRunCompleted(TestStepRun run) { }
 
        /// <summary>
        /// Called by OpenTAP when the source TestStep publishes results. This is happening in a background thread.
        /// </summary>
        public void OnResultPublished(TestStepRun stepRun, ResultTable result)
        {
            if (stepRun.TestStepId == SourceTestStep?.Id)
            {
                ResultColumn column = result.Columns.FirstOrDefault(col => col.Name == ResultColumnName);
                if (column != null)
                {
                    lock (Result)
                    {
                        Result.Enqueue(column.GetValue<T>(0));
                        ItemsInQueue.Set();
                    }
                }
            }
        }
 
        /// <summary> Initializes this instance. </summary>
        public void OnTestPlanRunStart(TestPlanRun run)
        {
            ItemsInQueue = new ManualResetEvent(false);
        }
        
        /// <summary> Cleans up after this instance </summary>
        public void OnTestPlanRunCompleted(TestPlanRun run)
        {
            ItemsInQueue.Dispose();
        }
    }
}