Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions TitaniumAS.Opc.Client.Tests/BasicUsageTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -153,10 +155,12 @@ public async Task ReadValiesAsynchronously()
server.Connect();

// Create a group with items.
OpcDaGroup group = CreateGroupWithItems(server);
using OpcDaGroup group = CreateGroupWithItems(server);

Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
// Read values of items from device asynchronously.
OpcDaItemValue[] values = await group.ReadAsync(group.Items);
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);

// Output values
foreach (OpcDaItemValue value in values)
Expand Down Expand Up @@ -261,7 +265,6 @@ public async Task GetAValueOfAnItemBySubscription()

// Create a group with items.
OpcDaGroup group = CreateGroupWithItems(server);

// Configure subscription.
group.ValuesChanged += OnGroupValuesChanged;
group.UpdateRate = TimeSpan.FromMilliseconds(100); // ValuesChanged won't be triggered if zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Test_Enumerate_OpcDa_Servers()
serverDescriptions.Should().ContainSingle(s => s.Uri.Segments[1] == "Matrikon.OPC.Simulation.1");
}

[TestMethod]
[TestMethod, Ignore]
public void Test_Enumerate_Hosts()
{
var enumerator = new OpcServerEnumeratorAuto();
Expand Down
96 changes: 48 additions & 48 deletions TitaniumAS.Opc.Client.Tests/Da/Browsing/OpcDaBrowser3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace TitaniumAS.Opc.Client.Tests.Da.Browsing
[TestClass]
public class OpcDaBrowser3Tests
{
private OpcDaServer _graybox;
private OpcDaBrowser3 _grayboxBrowser;
//private OpcDaServer _graybox;
//private OpcDaBrowser3 _grayboxBrowser;
private OpcDaServer _matrikon;
private OpcDaBrowser3 _matrikonBrowser;

Expand All @@ -19,16 +19,16 @@ public void TestInitialize()
{
_matrikon = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));
_matrikon.Connect();
_graybox = new OpcDaServer(UrlBuilder.Build("Graybox.Simulator.1"));
_graybox.Connect();
//_graybox = new OpcDaServer(UrlBuilder.Build("Graybox.Simulator.1"));
//_graybox.Connect();
_matrikonBrowser = new OpcDaBrowser3(_matrikon);
_grayboxBrowser = new OpcDaBrowser3(_graybox);
//_grayboxBrowser = new OpcDaBrowser3(_graybox);
}

[TestCleanup]
public void TestCleanup()
{
_graybox.Dispose();
//_graybox.Dispose();
_matrikon.Dispose();
}

Expand Down Expand Up @@ -84,51 +84,51 @@ public void Test_Browser_GetProperties()
}
}

[TestMethod]
public void Test_Browser_GetProperties_All()
{
var properties = _grayboxBrowser.GetProperties(
new[] {"numeric.random.int32", "numeric.random.int16"},
new OpcDaPropertiesQuery(true));
foreach (var itemProperties in properties)
{
itemProperties.ErrorId.Should().Match<HRESULT>(e => e.Succeeded);
itemProperties.Properties.Should().NotBeEmpty().And.OnlyContain(p => p.Value != null);
}
}
//[TestMethod]
//public void Test_Browser_GetProperties_All()
//{
// var properties = _grayboxBrowser.GetProperties(
// new[] {"numeric.random.int32", "numeric.random.int16"},
// new OpcDaPropertiesQuery(true));
// foreach (var itemProperties in properties)
// {
// itemProperties.ErrorId.Should().Match<HRESULT>(e => e.Succeeded);
// itemProperties.Properties.Should().NotBeEmpty().And.OnlyContain(p => p.Value != null);
// }
//}

[TestMethod]
public void Test_Browser_GetProperties_NoValue()
{
var properties = _grayboxBrowser.GetProperties(new[] {"numeric.random.int32", "numeric.random.int16"},
new OpcDaPropertiesQuery(false));
foreach (var itemProperties in properties)
{
itemProperties.Properties.Should().NotBeEmpty().And.OnlyContain(p => p.Value == null);
}
}
//[TestMethod]
//public void Test_Browser_GetProperties_NoValue()
//{
// var properties = _grayboxBrowser.GetProperties(new[] {"numeric.random.int32", "numeric.random.int16"},
// new OpcDaPropertiesQuery(false));
// foreach (var itemProperties in properties)
// {
// itemProperties.Properties.Should().NotBeEmpty().And.OnlyContain(p => p.Value == null);
// }
//}

[TestMethod]
public void Test_Browser_GetElements_WithProperties()
{
var elements = _grayboxBrowser.GetElements("numeric.random", null, new OpcDaPropertiesQuery());
foreach (var element in elements)
{
element.ItemProperties.Should().Match<OpcDaItemProperties>(p => p.Properties.Length > 0);
}
}
//[TestMethod]
//public void Test_Browser_GetElements_WithProperties()
//{
// var elements = _grayboxBrowser.GetElements("numeric.random", null, new OpcDaPropertiesQuery());
// foreach (var element in elements)
// {
// element.ItemProperties.Should().Match<OpcDaItemProperties>(p => p.Properties.Length > 0);
// }
//}

[TestMethod]
public void Test_Browser_TraverseTree_Graybox()
{
BrowseHelpers.BrowseChildren("", _grayboxBrowser);
}
//[TestMethod]
//public void Test_Browser_TraverseTree_Graybox()
//{
// BrowseHelpers.BrowseChildren("", _grayboxBrowser);
//}

[TestMethod]
public void Test_GetProperties()
{
var properties = _grayboxBrowser.GetProperties(new[] {"numeric.random.int32"});
properties[0].Properties.Should().NotBeEmpty();
}
//[TestMethod]
//public void Test_GetProperties()
//{
// var properties = _grayboxBrowser.GetProperties(new[] {"numeric.random.int32"});
// properties[0].Properties.Should().NotBeEmpty();
//}
}
}
57 changes: 29 additions & 28 deletions TitaniumAS.Opc.Client.Tests/Da/OpcDaGroupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void Test_Clone()
public void Test_Clone_With_Items()
{
var g1 = _server.AddGroup("g1");
g1.AddItems(new[] {new OpcDaItemDefinition {ItemId = "Random.Int2"}});
g1.AddItems(new[] { new OpcDaItemDefinition { ItemId = "Random.Int2" } });

var g2 = g1.Clone("g2");
g2.Items.Should().HaveCount(1);
Expand Down Expand Up @@ -321,7 +321,7 @@ public void Test_Write()
var itemResult = g1.AddItems(items);
g1.IsActive = true;

var errors = g1.Write(g1.Items, new object[] {1, 2.0});
var errors = g1.Write(g1.Items, new object[] { 1, 2.0 });
errors.Should().HaveCount(2);
errors.Should().OnlyContain(e => e.Succeeded);
}
Expand Down Expand Up @@ -351,7 +351,7 @@ public void Test_ReadMaxAge()
};
var itemResult = g1.AddItems(items);
g1.IsActive = true;
var values = g1.ReadMaxAge(g1.Items, new[] {TimeSpan.Zero, TimeSpan.MaxValue, TimeSpan.FromSeconds(5)});
var values = g1.ReadMaxAge(g1.Items, new[] { TimeSpan.Zero, TimeSpan.MaxValue, TimeSpan.FromSeconds(5) });
values.Should().HaveCount(3);
values.Should().OnlyContain(v => v.Error.Succeeded);
}
Expand Down Expand Up @@ -446,7 +446,7 @@ public void Test_ReadAsync_Cancellation()
var task = g1.ReadAsync(g1.Items, cts.Token);

Action act = () => task.Wait();
act.ShouldThrow<AggregateException>();
act.Should().Throw<AggregateException>();
}

[TestMethod]
Expand Down Expand Up @@ -499,7 +499,7 @@ public void Test_RefreshAsync_Cancellation()
var task = g1.RefreshAsync(OpcDaDataSource.Device, cts.Token);

Action act = () => task.Wait();
act.ShouldThrow<AggregateException>();
act.Should().Throw<AggregateException>();
}

[TestMethod]
Expand All @@ -524,7 +524,7 @@ public void Test_WriteAsync()
g1.IsActive = true;

var cts = new CancellationTokenSource();
var task = g1.WriteAsync(g1.Items, new object[] {1, 2.0}, cts.Token);
var task = g1.WriteAsync(g1.Items, new object[] { 1, 2.0 }, cts.Token);
task.Wait();
var errors = task.Result;
errors.Should().HaveCount(2);
Expand All @@ -547,9 +547,9 @@ public void Test_WriteAsync_Cancellation()
g1.IsActive = true;

var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
var task = g1.WriteAsync(g1.Items, new object[] {1, 2.0}, cts.Token);
var task = g1.WriteAsync(g1.Items, new object[] { 1, 2.0 }, cts.Token);
Action act = () => task.Wait();
act.ShouldThrow<AggregateException>();
act.Should().Throw<AggregateException>();
}

[TestMethod]
Expand All @@ -574,7 +574,7 @@ public void Test_ReadMaxAgeAsync()
g1.IsActive = true;

var cts = new CancellationTokenSource();
var task = g1.ReadMaxAgeAsync(g1.Items, new[] {TimeSpan.Zero, TimeSpan.Zero}, cts.Token);
var task = g1.ReadMaxAgeAsync(g1.Items, new[] { TimeSpan.Zero, TimeSpan.Zero }, cts.Token);
task.Wait();
var values = task.Result;
values.Should().HaveCount(2);
Expand All @@ -599,7 +599,7 @@ public void Test_ReadMaxAgeAsync_Cancellation()
cts.Cancel();
var task = g1.ReadMaxAgeAsync(g1.Items, null, cts.Token);
Action act = task.Wait;
act.ShouldThrow<AggregateException>();
act.Should().Throw<AggregateException>();
}

[TestMethod]
Expand Down Expand Up @@ -632,7 +632,7 @@ public void Test_RefreshMaxAgeAsync()
values.Should().OnlyContain(v => v.Quality.Master == OpcDaQualityMaster.Good);
}

[TestMethod , Ignore]
[TestMethod, Ignore]
public void Test_RefreshMaxAgeAsync_Cancellation()
{
var g1 = _server.AddGroup("g1");
Expand All @@ -652,7 +652,7 @@ public void Test_RefreshMaxAgeAsync_Cancellation()
var task = g1.RefreshMaxAgeAsync(TimeSpan.Zero, cts.Token);

Action act = task.Wait;
act.ShouldThrow<AggregateException>();
act.Should().Throw<AggregateException>();
}

[TestMethod]
Expand Down Expand Up @@ -727,7 +727,7 @@ public void Test_WriteVQTAsync_Cancellation()
cts.Cancel();
var task = g1.WriteVQTAsync(g1.Items, opcItemValues, cts.Token);
Action act = task.Wait;
act.ShouldThrow<AggregateException>();
act.Should().Throw<AggregateException>();
}

[TestMethod]
Expand All @@ -749,10 +749,10 @@ public void Test_IsSubscribed()
};
g1.UpdateRate = TimeSpan.FromMilliseconds(500);
g1.IsSubscribed = true;

var values = new List<OpcDaItemValue>();
AutoResetEvent callbackCalled = new AutoResetEvent(false);
g1.ValuesChanged += delegate(object sender, OpcDaItemValuesChangedEventArgs args)
g1.ValuesChanged += delegate (object sender, OpcDaItemValuesChangedEventArgs args)
{
values.InsertRange(values.Count, args.Values);
callbackCalled.Set();
Expand All @@ -763,16 +763,16 @@ public void Test_IsSubscribed()
var v2 = g1.Items[1];

// 1st active
g1.SetActiveItems(new []{v1});
g1.SetActiveItems(new[] { v1 });
g1.IsActive = true;

callbackCalled.WaitOne(TimeSpan.FromSeconds(5));
g1.IsActive = false;
values.Should().OnlyContain(v => v.Item == v1).And.NotBeEmpty();

// 2nd active
g1.SetActiveItems(new []{v1}, false);
g1.SetActiveItems(new []{v2}, true);
g1.SetActiveItems(new[] { v1 }, false);
g1.SetActiveItems(new[] { v2 }, true);
values.Clear();
g1.IsActive = true;

Expand Down Expand Up @@ -816,7 +816,7 @@ public void Test_PendingRequests()
group.UpdateRate = TimeSpan.FromMilliseconds(10);
group.IsSubscribed = true;
CancellationTokenSource cts = new CancellationTokenSource();

List<Task> tasks = new List<Task>();
for (var i = 0; i < 100; i++)
{
Expand Down Expand Up @@ -871,24 +871,25 @@ public void Test_MemoryLeaks()
group.ReadMaxAge(group.Items, TimeSpan.Zero);

// async read
Task.WaitAll(group.ReadAsync(group.Items, cts.Token), group.ReadMaxAgeAsync(group.Items,
TimeSpan.Zero, cts.Token),
group.RefreshAsync(OpcDaDataSource.Cache, cts.Token)
);
Task.WaitAll(
group.ReadAsync(group.Items, cts.Token),
group.ReadMaxAgeAsync(group.Items, TimeSpan.Zero, cts.Token),
group.RefreshAsync(OpcDaDataSource.Cache, cts.Token));

group.RemoveItems(group.Items.Take(group.Items.Count/2).ToArray());
group.RemoveItems(group.Items.Take(group.Items.Count / 2).ToArray());

// sync read
group.Read(group.Items);
group.ReadMaxAge(group.Items, TimeSpan.Zero);

// async read
Task.WaitAll(group.ReadAsync(group.Items, cts.Token), group.ReadMaxAgeAsync(group.Items,
TimeSpan.Zero, cts.Token),
group.RefreshAsync(OpcDaDataSource.Cache, cts.Token)
);
Task.WaitAll(
group.ReadAsync(group.Items, cts.Token),
group.ReadMaxAgeAsync(group.Items, TimeSpan.Zero, cts.Token),
group.RefreshAsync(OpcDaDataSource.Cache, cts.Token));
}
}

GC.Collect();
}
}
Expand Down
15 changes: 8 additions & 7 deletions TitaniumAS.Opc.Client.Tests/Da/OpcDaServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,32 @@ public void Test_WriteVQT()
{
Value = 0,
Quality = (short) OPC_QUALITY_STATUS.BAD
},
},
new OpcDaItemValue()
{
Value = 0,
Timestamp = DateTimeOffset.Now-TimeSpan.FromHours(1)
},
},
};
HRESULT[] errors = _server.WriteVQT(itemIds, values);
errors.Should().OnlyContain(hres => hres.Succeeded);
}

[TestMethod, Ignore]
[Ignore("Нужно запускать от администратора, ломает другие тесты, спользующие Matrikon и работающие параллельно.")]
[TestMethod]
public void Test_Shutdown()
{
var g1 = _server.AddGroup("g1");
g1.IsSubscribed = true;
g1.IsActive = true;

AutoResetEvent evnt = new AutoResetEvent(false);
AutoResetEvent evnt = new(false);
_server.Shutdown += (sender, args) => evnt.Set();
ServiceController sc = new ServiceController("MatrikonOPC Server for Simulation and Testing");

using ServiceController sc = new("MatrikonOPC Server for Simulation and Testing");
sc.Stop();
evnt.WaitOne();

_server.IsConnected.Should().BeFalse();
_server.Groups.Should().BeEmpty();
}
Expand Down
Loading