Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/simplify-n-refactor' into simpli…
Browse files Browse the repository at this point in the history
…fy-n-refactor
  • Loading branch information
jakhog committed Jun 17, 2024
2 parents e292761 + c569816 commit d772900
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Source/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Reader(ILogger logger)

public async Task ReadNodesForever(ISession connection, IEnumerable<(NodeId node, TimeSpan readInterval)> nodes, Func<NodeValue, Task> handleValue, CancellationToken cancellationToken)
{
_logger.Information("Starting reading nodes...");
_logger.Information("Start reading nodes...");
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

var tasks = nodes
Expand Down
39 changes: 39 additions & 0 deletions Specifications/for_Reader/given/a_reader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Machine.Specifications;
using Moq;
using Serilog;
using ISession = Opc.Ua.Client.ISession;

namespace RaaLabs.Edge.Connectors.OPCUA.for_Reader.given;

public class a_reader
{
protected static Reader reader;
protected static Mock<ISession> connection;
protected static CancellationTokenSource cts;
protected static List<NodeValue> handled_values;
protected static Func<NodeValue, Task> handler;

Establish context = () =>
{
connection = new();
reader = new(Mock.Of<ILogger>());
cts = new();

var values = handled_values = [];

handler = _ =>
{
handled_values.Add(_);
return Task.CompletedTask;
};
};

Cleanup after = () =>
{
cts.Dispose();
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Machine.Specifications;
using Opc.Ua;

namespace RaaLabs.Edge.Connectors.OPCUA.for_Reader.when_reading_nodes_forever;

public class and_datavalue_found : given.a_reader
{
static IEnumerable<(NodeId node, TimeSpan readInterval)> nodes;
static Task forever_reading_task;

Establish context = () =>
{
nodes = new[]
{
(new NodeId(1), TimeSpan.FromSeconds(1))
};
connection
.Setup(_ => _.ReadValueAsync(new NodeId(1), cts.Token))
.Returns(Task.FromResult(new DataValue(("reading value"))));
};

Because of = () =>
{
forever_reading_task = reader.ReadNodesForever(connection.Object, nodes, handler, cts.Token);
};

It should_have_read_the_values = () => handled_values.ShouldContainOnly(new NodeValue(new (1), new ("reading value")));
}

0 comments on commit d772900

Please sign in to comment.