Skip to content

Commit

Permalink
Update packages, fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Jan 1, 2024
1 parent 93aa3e7 commit 5efd9ef
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 58 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

<ItemGroup Condition="'$(IsTestProject)' == 'true'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
6 changes: 4 additions & 2 deletions src/ModbusRx.IntegrationTests/ModbusRxUdpSlaveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using CP.IO.Ports;
using ModbusRx.Data;
using ModbusRx.Device;
Expand Down Expand Up @@ -43,12 +44,13 @@ public void ModbusUdpSlave_EnsureTheSlaveShutsDownCleanly()
/// <summary>
/// Modbuses the UDP slave not bound.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ModbusUdpSlave_NotBound()
public async Task ModbusUdpSlave_NotBound()
{
var client = new UdpClientRx();
ModbusSlave slave = ModbusUdpSlave.CreateUdp(1, client);
Assert.ThrowsAsync<InvalidOperationException>(async () => await slave.ListenAsync());
await Assert.ThrowsAsync<InvalidOperationException>(async () => await slave.ListenAsync());
}

/// <summary>
Expand Down
60 changes: 34 additions & 26 deletions src/ModbusRx.UnitTests/Device/ModbusMasterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq;
using System.Threading.Tasks;
using ModbusRx.Device;
using ModbusRx.IO;
using Moq;
Expand Down Expand Up @@ -34,78 +35,85 @@ public class ModbusMasterFixture
/// <summary>
/// Reads the coils.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadCoils()
public async Task ReadCoils()
{
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadCoilsAsync(1, 1, 0));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadCoilsAsync(1, 1, 2001));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadCoilsAsync(1, 1, 0));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadCoilsAsync(1, 1, 2001));
}

/// <summary>
/// Reads the inputs.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadInputs()
public async Task ReadInputs()
{
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputsAsync(1, 1, 0));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputsAsync(1, 1, 2001));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputsAsync(1, 1, 0));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputsAsync(1, 1, 2001));
}

/// <summary>
/// Reads the holding registers.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadHoldingRegisters()
public async Task ReadHoldingRegistersAsync()
{
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadHoldingRegistersAsync(1, 1, 0));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadHoldingRegistersAsync(1, 1, 126));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadHoldingRegistersAsync(1, 1, 0));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadHoldingRegistersAsync(1, 1, 126));
}

/// <summary>
/// Reads the input registers.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadInputRegisters()
public async Task ReadInputRegistersAsync()
{
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputRegistersAsync(1, 1, 0));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputRegistersAsync(1, 1, 126));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputRegistersAsync(1, 1, 0));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadInputRegistersAsync(1, 1, 126));
}

/// <summary>
/// Writes the multiple registers.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void WriteMultipleRegisters()
public async Task WriteMultipleRegistersAsync()
{
Assert.ThrowsAsync<ArgumentNullException>(async () => await Master.WriteMultipleRegistersAsync(1, 1, null!));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleRegistersAsync(1, 1, Array.Empty<ushort>()));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleRegistersAsync(1, 1, Enumerable.Repeat<ushort>(1, 124).ToArray()));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await Master.WriteMultipleRegistersAsync(1, 1, null!));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleRegistersAsync(1, 1, Array.Empty<ushort>()));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleRegistersAsync(1, 1, Enumerable.Repeat<ushort>(1, 124).ToArray()));
}

/// <summary>
/// Writes the multiple coils.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void WriteMultipleCoils()
public async Task WriteMultipleCoilsAsync()
{
Assert.ThrowsAsync<ArgumentNullException>(async () => await Master.WriteMultipleCoilsAsync(1, 1, null!));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleCoilsAsync(1, 1, Array.Empty<bool>()));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleCoilsAsync(1, 1, Enumerable.Repeat(false, 1969).ToArray()));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await Master.WriteMultipleCoilsAsync(1, 1, null!));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleCoilsAsync(1, 1, Array.Empty<bool>()));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.WriteMultipleCoilsAsync(1, 1, Enumerable.Repeat(false, 1969).ToArray()));
}

/// <summary>
/// Reads the write multiple registers.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadWriteMultipleRegisters()
public async Task ReadWriteMultipleRegistersAsync()
{
// validate numberOfPointsToRead
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 0, 1, new ushort[] { 1 }));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 126, 1, new ushort[] { 1 }));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 0, 1, new ushort[] { 1 }));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 126, 1, new ushort[] { 1 }));

// validate writeData
Assert.ThrowsAsync<ArgumentNullException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 1, 1, null!));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 1, 1, Array.Empty<ushort>()));
Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 1, 1, Enumerable.Repeat<ushort>(1, 122).ToArray()));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 1, 1, null!));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 1, 1, Array.Empty<ushort>()));
await Assert.ThrowsAsync<ArgumentException>(async () => await Master.ReadWriteMultipleRegistersAsync(1, 1, 1, 1, Enumerable.Repeat<ushort>(1, 122).ToArray()));
}
}
8 changes: 5 additions & 3 deletions src/ModbusRx.UnitTests/IO/EmptyTransportFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Threading.Tasks;
using ModbusRx.IO;
using ModbusRx.Message;
using Xunit;
Expand All @@ -16,12 +17,13 @@ public static class EmptyTransportFixture
/// <summary>
/// Negatives this instance.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public static void Negative()
public static async Task NegativeAsync()
{
var transport = new EmptyTransport();
Assert.ThrowsAsync<NotImplementedException>(() => transport.ReadRequest());
Assert.ThrowsAsync<NotImplementedException>(() => transport.ReadResponse<ReadCoilsInputsResponse>());
await Assert.ThrowsAsync<NotImplementedException>(() => transport.ReadRequest());
await Assert.ThrowsAsync<NotImplementedException>(() => transport.ReadResponse<ReadCoilsInputsResponse>());
Assert.Throws<NotImplementedException>(() => transport.BuildMessageFrame(null!));
Assert.Throws<NotImplementedException>(() => transport.Write(null!));
Assert.Throws<NotImplementedException>(() => transport.OnValidateResponse(null!, null!));
Expand Down
5 changes: 3 additions & 2 deletions src/ModbusRx.UnitTests/IO/ModbusAsciiTransportFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public async Task ReadRequestResponseAsync()
/// <summary>
/// Reads the request response not enough bytes.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadRequestResponseNotEnoughBytes()
public async Task ReadRequestResponseNotEnoughBytesAsync()
{
var mock = new Mock<IStreamResource>(MockBehavior.Strict);
var stream = mock.Object;
Expand All @@ -81,7 +82,7 @@ public void ReadRequestResponseNotEnoughBytes()
return 1;
});

Assert.ThrowsAsync<IOException>(() => transport.ReadRequestResponse());
await Assert.ThrowsAsync<IOException>(() => transport.ReadRequestResponse());
mock.VerifyAll();
}

Expand Down
5 changes: 3 additions & 2 deletions src/ModbusRx.UnitTests/IO/ModbusRtuTransportFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ public async void ReadResponseSlaveException()
/// We want to throw an IOException for any message w/ an invalid checksum,
/// this must preceed throwing a SlaveException based on function code &gt; 127.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadResponseSlaveExceptionWithErroneousLrc()
public async Task ReadResponseSlaveExceptionWithErroneousLrcAsync()
{
var mock = new Mock<ModbusRtuTransport>(StreamResource) { CallBase = true };
var transport = mock.Object;
Expand All @@ -245,7 +246,7 @@ public void ReadResponseSlaveExceptionWithErroneousLrc()
mock.Setup(t => t.Read(1))
.Returns(new byte[] { crc[1] });

Assert.ThrowsAsync<IOException>(() => transport.ReadResponse<ReadCoilsInputsResponse>());
await Assert.ThrowsAsync<IOException>(() => transport.ReadResponse<ReadCoilsInputsResponse>());

mock.VerifyAll();
}
Expand Down
5 changes: 3 additions & 2 deletions src/ModbusRx.UnitTests/IO/ModbusSerialTransportFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public async void CreateResponse()
/// <summary>
/// Creates the response erroneous LRC.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void CreateResponseErroneousLrc()
public async Task CreateResponseErroneousLrcAsync()
{
var transport = new ModbusAsciiTransport(StreamResource) { CheckFrame = true };
var frame = Task.FromResult(new byte[] { 19, Modbus.ReadCoils, 0, 0, 0, 2, 115 });

Assert.ThrowsAsync<IOException>(() => transport.CreateResponse<ReadCoilsInputsResponse>(frame));
await Assert.ThrowsAsync<IOException>(() => transport.CreateResponse<ReadCoilsInputsResponse>(frame));
}

/// <summary>
Expand Down
11 changes: 7 additions & 4 deletions src/ModbusRx.UnitTests/IO/ModbusTcpTransportFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ModbusRx.Data;
using ModbusRx.IO;
using ModbusRx.Message;
Expand Down Expand Up @@ -104,30 +105,32 @@ public async void ReadRequestResponse()
/// <summary>
/// Reads the request response connection aborted while reading mbap header.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadRequestResponse_ConnectionAbortedWhileReadingMBAPHeader()
public async Task ReadRequestResponse_ConnectionAbortedWhileReadingMBAPHeaderAsync()
{
var mock = new Mock<IStreamResource>(MockBehavior.Strict);
mock.Setup(s => s.ReadAsync(It.Is<byte[]>(x => x.Length == 6), 0, 6).Result).Returns(3);

Check warning on line 113 in src/ModbusRx.UnitTests/IO/ModbusTcpTransportFixture.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)
mock.Setup(s => s.ReadAsync(It.Is<byte[]>(x => x.Length == 6), 3, 3).Result).Returns(0);

Check warning on line 114 in src/ModbusRx.UnitTests/IO/ModbusTcpTransportFixture.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Assert.ThrowsAsync<IOException>(() => ModbusIpTransport.ReadRequestResponse(mock.Object));
await Assert.ThrowsAsync<IOException>(() => ModbusIpTransport.ReadRequestResponse(mock.Object));
mock.VerifyAll();
}

/// <summary>
/// Reads the request response connection aborted while reading message frame.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void ReadRequestResponse_ConnectionAbortedWhileReadingMessageFrame()
public async Task ReadRequestResponse_ConnectionAbortedWhileReadingMessageFrameAsync()
{
var mock = new Mock<IStreamResource>(MockBehavior.Strict);

mock.Setup(s => s.ReadAsync(It.Is<byte[]>(x => x.Length == 6), 0, 6).Result).Returns(6);
mock.Setup(s => s.ReadAsync(It.Is<byte[]>(x => x.Length == 6), 0, 6).Result).Returns(3);
mock.Setup(s => s.ReadAsync(It.Is<byte[]>(x => x.Length == 6), 3, 3).Result).Returns(0);

Assert.ThrowsAsync<IOException>(() => ModbusIpTransport.ReadRequestResponse(mock.Object));
await Assert.ThrowsAsync<IOException>(() => ModbusIpTransport.ReadRequestResponse(mock.Object));
mock.VerifyAll();
}

Expand Down
14 changes: 8 additions & 6 deletions src/ModbusRx.UnitTests/IO/UdpClientAdapterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Threading.Tasks;
using CP.IO.Ports;
using ModbusRx.IO;
using Xunit;
Expand All @@ -16,20 +17,21 @@ public class UdpClientAdapterFixture
/// <summary>
/// Reads the argument validation.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public void Read_ArgumentValidation()
public async Task Read_ArgumentValidationAsync()
{
var adapter = new UdpClientAdapter(new UdpClientRx());

// buffer
Assert.ThrowsAsync<ArgumentNullException>(() => adapter.ReadAsync(null!, 1, 1));
await Assert.ThrowsAsync<ArgumentNullException>(() => adapter.ReadAsync(null!, 1, 1));

// offset
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], -1, 2));
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], 3, 3));
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], -1, 2));
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], 3, 3));

Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], 0, -1));
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], 1, 2));
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], 0, -1));
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.ReadAsync(new byte[2], 1, 2));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/ModbusRx/ModbusRx.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
Expand All @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SerialPortRx" Version="2.3.4" />
<PackageReference Include="SerialPortRx" Version="3.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 8 additions & 7 deletions src/ModbusRx/Reactive/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using CP.IO.Ports;
using ModbusRx.Data;
using ModbusRx.Device;
using ReactiveMarbles.Extensions;

namespace ModbusRx.Reactive
{
Expand Down Expand Up @@ -657,7 +658,7 @@ public static IObservable<ModbusUdpSlave> UdpIpSlave(string ipAddress, int port
master = null;
observer.OnNext((connected, null, master));
comdis = [];
comdis.AddTo(dis);
comdis.DisposeWith(dis);
}
}
catch (Exception ex)
Expand All @@ -668,9 +669,9 @@ public static IObservable<ModbusUdpSlave> UdpIpSlave(string ipAddress, int port
master = null;
observer.OnNext((connected, new ModbusCommunicationException("ModbusRx Master Fault", ex), master));
comdis = [];
comdis.AddTo(dis);
comdis.DisposeWith(dis);
}
}).Retry().Subscribe().AddTo(dis);
}).Retry().Subscribe().DisposeWith(dis);

return dis;
}).Publish().RefCount();
Expand Down Expand Up @@ -727,7 +728,7 @@ public static IObservable<ModbusSerialSlave> SerialRtuSlave(string port, byte un
comdis?.Dispose();
slaveThread?.Dispose();
comdis = [];
comdis.AddTo(dis);
comdis.DisposeWith(dis);
}
}
catch (Exception ex)
Expand All @@ -737,7 +738,7 @@ public static IObservable<ModbusSerialSlave> SerialRtuSlave(string port, byte un
comdis?.Dispose();
slaveThread?.Dispose();
comdis = [];
comdis.AddTo(dis);
comdis.DisposeWith(dis);
}
}).Retry().Subscribe();

Expand Down Expand Up @@ -801,7 +802,7 @@ public static IObservable<ModbusSerialSlave> SerialAsciiSlave(string port, byte
comdis?.Dispose();
slaveThread?.Dispose();
comdis = [];
comdis.AddTo(dis);
comdis.DisposeWith(dis);
}
}
catch (Exception ex)
Expand All @@ -811,7 +812,7 @@ public static IObservable<ModbusSerialSlave> SerialAsciiSlave(string port, byte
comdis?.Dispose();
slaveThread?.Dispose();
comdis = [];
comdis.AddTo(dis);
comdis.DisposeWith(dis);
}
}).Retry().Subscribe();

Expand Down

0 comments on commit 5efd9ef

Please sign in to comment.