diff --git a/Source/Reader.cs b/Source/Reader.cs index d7fe656..f687e56 100644 --- a/Source/Reader.cs +++ b/Source/Reader.cs @@ -20,7 +20,7 @@ public Reader(ILogger logger) public async Task ReadNodesForever(ISession connection, IEnumerable<(NodeId node, TimeSpan readInterval)> nodes, Func handleValue, CancellationToken cancellationToken) { - _logger.Information("Starting reading nodes..."); + _logger.Information("Start reading nodes..."); using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); var tasks = nodes diff --git a/Specifications/for_Reader/given/a_reader.cs b/Specifications/for_Reader/given/a_reader.cs new file mode 100644 index 0000000..9eaa1ab --- /dev/null +++ b/Specifications/for_Reader/given/a_reader.cs @@ -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 connection; + protected static CancellationTokenSource cts; + protected static List handled_values; + protected static Func handler; + + Establish context = () => + { + connection = new(); + reader = new(Mock.Of()); + cts = new(); + + var values = handled_values = []; + + handler = _ => + { + handled_values.Add(_); + return Task.CompletedTask; + }; + }; + + Cleanup after = () => + { + cts.Dispose(); + }; +} \ No newline at end of file diff --git a/Specifications/for_Reader/when_reading_nodes_forever/and_datavalue_found.cs b/Specifications/for_Reader/when_reading_nodes_forever/and_datavalue_found.cs new file mode 100644 index 0000000..fe27974 --- /dev/null +++ b/Specifications/for_Reader/when_reading_nodes_forever/and_datavalue_found.cs @@ -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"))); +} \ No newline at end of file