-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestSegmentStateMachine.cs
322 lines (284 loc) · 13.7 KB
/
TestSegmentStateMachine.cs
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 (C) 2020, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/OPC-UA-OOI
//___________________________________________________________________________________
#pragma warning disable 1591
using CAS.CommServer.ProtocolHub.Communication;
using CAS.CommServer.ProtocolHub.Communication.BaseStation;
using CAS.CommServer.ProtocolHub.CommunicationUnitTests.Instrumentation;
using CAS.CommServer.ProtocolHub.ConfigurationEditor.Components;
using CAS.CommServer.ProtocolHub.MonitorInterface;
using CAS.Lib.CommonBus.ApplicationLayer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UAOOI.ProcessObserver.Configuration;
using UAOOI.ProcessObserver.RealTime.Processes;
using DiagnosticSegment = CAS.CommServer.ProtocolHub.Communication.Diagnostic.Segment;
namespace CAS.CommServer.ProtocolHub.CommunicationUnitTests
{
[TestClass]
[DeploymentItem("DefaultConfig.xml")]
public class TestSegmentStateMachine
{
[ClassInitialize]
public static void OnceExecutedSetUp(TestContext context)
{
Assert.IsFalse(m_OnceExecutedSetUpFinished);
FileInfo _configuration = new FileInfo("DefaultConfig.xml");
Assert.IsTrue(_configuration.Exists);
m_ConfigurationMain.ReadConfiguration(_configuration.FullName);
Assert.IsNotNull(myConfig, "Problem with configuration file: null configuration");
Assert.IsTrue(myConfig.Channels.Count > 0, "Problem with configuration file: 0 channels");
int volumeConstrain = int.MaxValue;
Assert.Inconclusive();
Station.InitStations(myConfig.Station, ref volumeConstrain);
SetUp();
m_OnceExecutedSetUpFinished = true;
}
[ClassCleanup]
public static void OnceExecutedTearDown()
{
Assert.IsTrue(m_OnceExecutedSetUpFinished);
myMaster.CheckConsistency();
myConfig.Dispose();
}
private static void SetUp()
{
FacadePipe myPipe = new FacadePipe(myConfig.Station[0]);
myMaster = new FacadeApplicationLayerMaster();
myFacadeISegmentStatistics = new FacadeISegmentStatistics();
SegmentTiming();
myMachine = new SegmentStateMachine(myMaster, parameters, false, myFacadeISegmentStatistics, myTimeList);
myMachine.ResetCounter();
myMachine.DisconnectedAfterFailureEntered += new EventHandler(myMachine_DisconnectedAfterFailureEntered);
FacadeSegment myFacadeSegment = new FacadeSegment();
int myMaxNumberOfTags = int.MaxValue;
Statistics.ChannelStatistics myChanel = new Statistics.ChannelStatistics(myConfig.Channels[0]);
DiagnosticSegment mySegment = new DiagnosticSegment(myConfig.Segments[0], myChanel);
myInterface = (new FacadeSegment.FacadePipeInterface(new InterfaceParameters(myConfig.Interfaces[0]), myPipe, mySegment));
myInterface.ResetCounter();
FacadeSegment.FacadeDataDescription myDataDescription = new FacadeSegment.FacadeDataDescription(myConfig.DataBlocks[0], ref myMaxNumberOfTags);
myDataDescription.ResetCounter();
myPipeDataBlock = new FacadeSegment.FacadePipeInterface.FacadePipeDataBlock(myFacadeSegment, myDataDescription, myInterface);
myPipeDataBlock.ResetCounter();
}
#region private
private class FacadeWaitTimeList : WaitTimeList<SegmentStateMachine>
{
internal FacadeWaitTimeList() : base("TestSegmentStateMachine")
{
}
}
private static bool m_OnceExecutedSetUpFinished = false;
private static CommServerConfigurationMain m_ConfigurationMain = new CommServerConfigurationMain();
private static ComunicationNet myConfig => m_ConfigurationMain.Configuartion;
private static readonly FacadeWaitTimeList myTimeList = new FacadeWaitTimeList();
private static SegmentStateMachine myMachine;
private static FacadeSegment.FacadePipeInterface.FacadePipeDataBlock myPipeDataBlock;
private static FacadeApplicationLayerMaster myMaster;
private static FacadeSegment.FacadePipeInterface myInterface;
private static FacadeISegmentStatistics myFacadeISegmentStatistics;
private static SegmentParameters parameters;
private static bool myMachine_DisconnectedAfterFailureEnteredExecuted = false;
private readonly IBlockDescription myBlockDescription = new FacadeBlockDescription(int.MaxValue, int.MaxValue, 0);
private TimeSpan FiveSeconds = new TimeSpan(0, 0, 0, 5, 0);
private int myNumberOfThreads = 0;
private void MakeConnection()
{
myMachine.ConnectRequest();
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
TestRead();
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
TestWriteData();
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
}
private static void myMachine_DisconnectedAfterFailureEntered(object sender, EventArgs e)
{
myMachine_DisconnectedAfterFailureEnteredExecuted = true;
}
private void TestRead()
{
//myMachine.ReadData( myPipeDataBlock );
IBlockDescription dataAddress = new FacadeBlockDescription(int.MaxValue, int.MaxValue, short.MaxValue);
myMachine.ReadData(out object data, dataAddress, myInterface);
}
private void TestWriteData()
{
myMachine.WriteData(0, myBlockDescription, myInterface);
}
private void AssertConnected()
{
Assert.AreEqual(SegmentStateMachine.State.Connected, myMachine.CurrentState);
}
private void AssertDisconnected()
{
Assert.AreEqual(SegmentStateMachine.State.Disconnected, myMachine.CurrentState);
}
private void AssertKeepConnection()
{
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
}
private void WaitCallbackHandle(object state)
{
System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
TestSegmentStateMachine myParent = (TestSegmentStateMachine)state;
myStopwatch.Reset();
myStopwatch.Start();
while (myStopwatch.Elapsed < FiveSeconds)
{
TestRead();
TestWriteData();
}
System.Threading.Interlocked.Decrement(ref myParent.myNumberOfThreads);
}
private static void SegmentTiming()
{
ComunicationNet.SegmentsRow segmentRow = myConfig.Segments[0];
segmentRow.KeepConnect = false;
segmentRow.TimeIdleKeepConn = 100;
segmentRow.timeKeepConn = 5000;
segmentRow.TimeReconnect = 10000;
segmentRow.TimeScan = 10000;
parameters = new SegmentParameters(segmentRow);
}
#endregion private
[TestMethod]
public void TestSuccess()
{
try
{
string KeepConnectExpectedTimeTemplate = "Expected KeepConnect time ={0}";
string KeepConnectActualTimeTemplate = "KeepConnect time ={0}";
string ConnectExpectedTimeTemplate = "Expected connect time ={0}";
string CoccectAcctualTimeTemplate = "Connect time ={0}";
string IdleKeepConnectExpectedTimeTemplate = "Expected IdleKeepConnect time ={0}";
string IdleKeepConnectActualTimeTemplate = "IdleKeepConnect time ={0}";
MakeConnection();
AssertKeepConnection();
System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
myStopwatch.Start();
TimeSpan maxValue = parameters.TimeKeepConnrction + new TimeSpan(0, 0, 0, 0, 100);
Console.WriteLine(KeepConnectExpectedTimeTemplate, parameters.TimeKeepConnrction.ToString());
while (myMachine.CurrentState == SegmentStateMachine.State.KeepConnection)
{
Assert.IsTrue(myStopwatch.Elapsed < maxValue, "Timing error - too log keep connect state");
Assert.IsFalse(myMachine.NeedsChannelAccess, "inconsistency of the myMachine.NeedsChannelAccess");
TestRead();
TestWriteData();
Thread.Sleep(1);
}
AssertConnected();
Assert.IsTrue(myStopwatch.Elapsed >= parameters.TimeKeepConnrction, $"Timing error - too short keep connect state time {myStopwatch.Elapsed } expected {parameters.TimeKeepConnrction}");
Console.WriteLine(KeepConnectActualTimeTemplate, myStopwatch.Elapsed.ToString());
myStopwatch.Reset();
myStopwatch.Start();
Console.WriteLine(ConnectExpectedTimeTemplate, FiveSeconds.ToString());
while (myStopwatch.Elapsed < FiveSeconds)
{
TestRead();
TestWriteData();
Thread.Sleep(1);
AssertConnected();
}
Console.WriteLine(CoccectAcctualTimeTemplate, myStopwatch.Elapsed.ToString());
myStopwatch.Reset();
myStopwatch.Start();
Console.WriteLine(IdleKeepConnectExpectedTimeTemplate, parameters.TimeIdleKeepConnection.ToString());
maxValue = parameters.TimeIdleKeepConnection + new TimeSpan(0, 0, 0, 0, 100);
while (myMachine.CurrentState == SegmentStateMachine.State.Connected)
{
Assert.IsTrue(myStopwatch.Elapsed < maxValue, "Timing error - to log idle keep connect state");
Thread.Sleep(1);
}
Assert.IsTrue(myStopwatch.Elapsed >= parameters.TimeIdleKeepConnection, "Timing error - to short idle keep connect state");
AssertDisconnected();
Console.WriteLine(IdleKeepConnectActualTimeTemplate, myStopwatch.Elapsed.ToString());
MakeConnection();
myMachine.NotifyKeepConnectTimeElapsed();
}
finally
{
myMachine.DisconnectRequest();
AssertDisconnected();
Debug.WriteLine("Test failed - machine has been disconnected.");
}
}
[TestMethod]
public void TestAsynchronousDisconnect()
{
Assert.Inconclusive();
System.Threading.ThreadPool.GetAvailableThreads(out int workerThreads, out int completionPortThreads);
Console.WriteLine("number of available threads worker= {0}; CompletionPort= {1}", workerThreads, completionPortThreads);
Assert.IsFalse(myMachine.NeedsChannelAccess, "inconsistency of the myMachine.NeedsChannelAccess");
myNumberOfThreads = workerThreads;
for (int index = 0; index < myNumberOfThreads; index++)
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(WaitCallbackHandle), this);
//
System.Threading.ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("number of available threads worker= {0}; CompletionPort= {1}", workerThreads, completionPortThreads);
Thread.Sleep(1000);
System.Threading.ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("number of available threads worker= {0}; CompletionPort= {1}", workerThreads, completionPortThreads);
//
while (!Equals(myNumberOfThreads, 0))
{
MakeConnection();
AssertKeepConnection();
myMachine.DisconnectRequest();
AssertDisconnected();
}
Console.WriteLine(myMaster.ToString());
AssertDisconnected();
Assert.IsFalse(myMachine.NeedsChannelAccess, "inconsistency of the myMachine.NeedsChannelAccess");
System.Threading.ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("number of available threads worker= {0}; CompletionPort= {1}", workerThreads, completionPortThreads);
}
[TestMethod]
public void TestRWFailure()
{
myMachine.ConnectRequest();
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
myMaster.MakeError();
TestRead();
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
myMaster.MakeError();
TestWriteData();
Assert.AreEqual(SegmentStateMachine.State.KeepConnection, myMachine.CurrentState);
myMachine.NotifyKeepConnectTimeElapsed();
Assert.AreEqual(SegmentStateMachine.State.Connected, myMachine.CurrentState);
myMaster.MakeError();
TestRead();
Assert.AreEqual(SegmentStateMachine.State.Connected, myMachine.CurrentState);
myMaster.MakeError();
TestWriteData();
Assert.AreEqual(SegmentStateMachine.State.Connected, myMachine.CurrentState);
myMachine.DisconnectRequest();
Assert.AreEqual(SegmentStateMachine.State.Disconnected, myMachine.CurrentState);
}
[TestMethod]
public void TestConnectionAbort()
{
myMaster.BreakConnection();
myMachine.ConnectRequest();
Assert.AreEqual(SegmentStateMachine.State.DisconnectedAfterFailure, myMachine.CurrentState);
MakeConnection();
myMaster.BreakConnection();
TestRead();
Assert.AreEqual(SegmentStateMachine.State.DisconnectedAfterFailure, myMachine.CurrentState);
Assert.IsTrue(myMachine_DisconnectedAfterFailureEnteredExecuted, "Disconnected After Failure Entered Executed not executed");
myMachine_DisconnectedAfterFailureEnteredExecuted = false;
MakeConnection();
myMaster.BreakConnection();
TestWriteData();
Assert.AreEqual(SegmentStateMachine.State.DisconnectedAfterFailure, myMachine.CurrentState);
Assert.IsTrue(myMachine_DisconnectedAfterFailureEnteredExecuted, "Disconnected After Failure Entered Executed not executed");
myMachine_DisconnectedAfterFailureEnteredExecuted = false;
Assert.IsFalse(myMachine.NeedsChannelAccess, "inconsistency of the myMachine.NeedsChannelAccess");
}
}
}