chr
2026-04-08 53e656200368a983e563550e2cc1acbc6d86b729
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
//            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.ComponentModel;
using System.Threading;
 
namespace OpenTap.Engine.UnitTests
{
    [DisplayName("Test\\Validator")]
    [Description("Validates that result listeners behave expectedly.")]
    public class ResultListenerValidator : ResultListener
    {
        #region Private members
        private bool IsOpen = false;
 
        private bool IsTestPlanRunning = false;
        #endregion
        public Exception Exception;
 
        public ResultListenerValidator()
        {
            Name = "Validator";
        }
 
        private void NeedsOpen(string Message)
        {
            if (!IsOpen) throw (Exception = new Exception("Result listener is not opened: " + Message));
        }
 
        private void EnterLocked(ref SpinLock Lock, string Procedure)
        {
            bool lockTaken = false;
            Lock.TryEnter(0, ref lockTaken);
            if (!lockTaken)
            {
                throw (Exception = new Exception(string.Format("Tried to enter {0} while it is locked by another thread", Procedure)));
            }
        }
 
        private SpinLock OpenLock = new SpinLock();
        public override void Open()
        {
            EnterLocked(ref OpenLock, "Open");
            try
            {
                if (IsOpen) throw new Exception("Result listener is already open");
                IsOpen = true;
                base.Open();
            }
            finally
            {
                OpenLock.Exit(false);
            }
        }
 
        private SpinLock CloseLock = new SpinLock();
        public override void Close()
        {
            EnterLocked(ref CloseLock, "Close");
            try
            {
                NeedsOpen("Close");
                IsOpen = false;
                base.Close();
            }
            finally
            {
                CloseLock.Exit(false);
            }
        }
 
        private SpinLock AddResultLock = new SpinLock();
        public override void OnResultPublished(Guid stepRunId, ResultTable result)
        {
            EnterLocked(ref AddResultLock, "AddResult");
            try
            {
                NeedsOpen("AddResult");
                base.OnResultPublished(stepRunId, result);
            }
            finally
            {
                AddResultLock.Exit(false);
            }
        }
 
        private SpinLock OnTestPlanRunStartLock = new SpinLock();
        public override void OnTestPlanRunStart(TestPlanRun planRun)
        {
            EnterLocked(ref OnTestPlanRunStartLock, "OnTestPlanRunStart");
            try
            {
                NeedsOpen("OnTestPlanRunStart");
                IsTestPlanRunning = true;
                base.OnTestPlanRunStart(planRun);
            }
            finally
            {
                OnTestPlanRunStartLock.Exit(false);
            }
        }
 
        private SpinLock OnTestPlanRunCompletedLock = new SpinLock();
        public override void OnTestPlanRunCompleted(TestPlanRun planRun, System.IO.Stream logStream)
        {
            EnterLocked(ref OnTestPlanRunCompletedLock, "OnTestPlanRunCompleted");
            try
            {
                NeedsOpen("OnTestPlanRunCompleted");
                if (!IsTestPlanRunning) throw new Exception("TestPlan is not running");
                IsTestPlanRunning = false;
                base.OnTestPlanRunCompleted(planRun, logStream);
            }
            finally
            {
                OnTestPlanRunCompletedLock.Exit(false);
            }
        }
 
        private SpinLock OnTestStepRunStartLock = new SpinLock();
        public override void OnTestStepRunStart(TestStepRun stepRun)
        {
            EnterLocked(ref OnTestStepRunStartLock, "OnTestStepRunStart");
            try
            {
                NeedsOpen("OnTestStepRunStart");
                base.OnTestStepRunStart(stepRun);
            }
            finally
            {
                OnTestStepRunStartLock.Exit(false);
            }
        }
 
        private SpinLock OnTestStepRunCompletedLock = new SpinLock();
        public override void OnTestStepRunCompleted(TestStepRun stepRun)
        {
            EnterLocked(ref OnTestStepRunCompletedLock, "OnTestStepRunCompleted");
            try
            {
                NeedsOpen("OnTestStepRunCompleted");
                base.OnTestStepRunCompleted(stepRun);
            }
            finally
            {
                OnTestStepRunCompletedLock.Exit(false);
            }
        }
    }
}