Skip to content

Commit

Permalink
Merge branch 'main' into communication-captures
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Croes committed Jan 27, 2025
2 parents 316bd60 + ae88509 commit 3927553
Show file tree
Hide file tree
Showing 29 changed files with 619 additions and 212 deletions.
32 changes: 21 additions & 11 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@ jobs:
create_nuget:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Get all history to allow automatic versioning

- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v0
uses: gittools/actions/gitversion/setup@v1
with:
versionSpec: '6.x'
versionSpec: '5.x'
includePrerelease: true
preferLatestVersion: true

- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/execute@v0
uses: gittools/actions/gitversion/execute@v1

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
- run: >
dotnet pack
Expand All @@ -53,7 +58,7 @@ jobs:
/p:PackageVersion=${{ steps.gitversion.outputs.semVer }}
--output ${{ env.NuGetDirectory }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: nuget
if-no-files-found: error
Expand All @@ -65,9 +70,14 @@ jobs:
run_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
- name: Build
run: dotnet build --configuration Release -v q
- name: Run tests
Expand All @@ -81,13 +91,13 @@ jobs:
runs-on: ubuntu-latest
needs: [ create_nuget, run_test ]
steps:
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: nuget
path: ${{ env.NuGetDirectory }}

- name: Setup .NET Core
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4

# Publish all NuGet packages to NuGet.org
# Use --skip-duplicate to prevent errors if a package with the same version already exists.
Expand Down
2 changes: 1 addition & 1 deletion Sally7.Benchmarks/ConverterLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private class DataItemWithConverter<T>

public DataItemWithConverter(Func<byte[], T> converter)
{
this._converter = converter;
_converter = converter;
}

public T? Value { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Sally7.Benchmarks/Sally7.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net462;net48;net6;net7</TargetFrameworks>
<TargetFrameworks>net462;net48;net6;net7.0;net8</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sally7.Tests
{
public class ValueTypeConversionTests
public class FromPlcConverterTests
{
[Theory]
[InlineData(0)]
Expand Down
2 changes: 1 addition & 1 deletion Sally7.Tests/Protocol/CommunicationSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class CommunicationSequence

public CommunicationSequence(ITestOutputHelper output)
{
this._output = output;
_output = output;
}

public class Fragment
Expand Down
2 changes: 1 addition & 1 deletion Sally7.Tests/Protocol/CommunicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CommunicationTests

public CommunicationTests(ITestOutputHelper output)
{
this._output = output;
_output = output;
}

[Fact]
Expand Down
48 changes: 48 additions & 0 deletions Sally7.Tests/RequestExecutor/JobPoolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Threading.Channels;
using Sally7.RequestExecutor;

namespace Sally7.Tests.RequestExecutor;

public class JobPoolTests
{
[Fact]
public async Task RentJobIdAsync_Throws_If_Disposed_And_Depleted()
{
// Arrange
var sut = new JobPool(1);
sut.Dispose();
_ = await sut.RentJobIdAsync(CancellationToken.None); // Empty the pool

// Act
// Assert
await Should.ThrowAsync<ChannelClosedException>(() => sut.RentJobIdAsync(CancellationToken.None).AsTask());
}

[Fact]
public async Task ReturnJobId_Does_Not_Throw_If_Disposed()
{
// Arrange
var sut = new JobPool(1);
var jobId = await sut.RentJobIdAsync(CancellationToken.None);
sut.Dispose();

// Act
// Assert
sut.ReturnJobId(jobId);
}

[Fact]
public async Task Dispose_Calls_Dispose_On_Requests()
{
// Arrange
var sut = new JobPool(1);
var jobId = await sut.RentJobIdAsync(CancellationToken.None);
var request = sut.GetRequest(jobId);

// Act
sut.Dispose();

// Assert
Should.Throw<ObjectDisposedException>(() => request.GetResult());
}
}
35 changes: 35 additions & 0 deletions Sally7.Tests/RequestExecutor/RequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using FakeItEasy;
using Sally7.RequestExecutor;

namespace Sally7.Tests.RequestExecutor;

public class RequestTests
{
[Fact]
public void Completes_On_Dispose()
{
// Arrange
var sut = new Request();
var callback = A.Fake<Action>();
sut.OnCompleted(callback);

// Act
sut.Dispose();

// Assert
A.CallTo(() => callback.Invoke()).MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Throws_When_Awaited_After_Dispose()
{
// Arrange
var sut = new Request();

// Act
sut.Dispose();

// Assert
await Should.ThrowAsync<ObjectDisposedException>(async () => await sut);
}
}
19 changes: 19 additions & 0 deletions Sally7.Tests/RequestExecutor/SignalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading.Channels;
using Sally7.RequestExecutor;

namespace Sally7.Tests.RequestExecutor;

public class SignalTests
{
[Fact]
public async Task WaitAsync_Throws_If_Disposed()
{
// Arrange
var sut = new Signal();
sut.Dispose();

// Act
// Assert
await Should.ThrowAsync<ChannelClosedException>(() => sut.WaitAsync(CancellationToken.None).AsTask());
}
}
18 changes: 9 additions & 9 deletions Sally7.Tests/Sally7.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -15,17 +15,17 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="7.4.0" />
<PackageReference Include="GithubActionsTestLogger" Version="2.3.2">
<PackageReference Include="FakeItEasy" Version="8.3.0" />
<PackageReference Include="GithubActionsTestLogger" Version="2.4.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

Expand Down
106 changes: 106 additions & 0 deletions Sally7.Tests/TestValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Sally7.Tests;

internal static class TestValues
{
public static IEnumerable<byte> ByteData =>
Enumerable.Range(byte.MinValue, byte.MaxValue).Select(x => (byte)x);

public static IEnumerable<sbyte> SByteData =>
Enumerable.Range(sbyte.MinValue, sbyte.MaxValue).Select(x => (sbyte)x);


public static IEnumerable<short> Int16Data => ShortValues;

public static IEnumerable<ushort> UInt16Data =>
ShortValues.Select(x => (ushort)x);

public static IEnumerable<int> Int32Data => UIntValues.Select(x => (int)x);

public static IEnumerable<uint> UInt32Data => UIntValues;

public static IEnumerable<float> SingleData
{
get
{
float[] values =
[
0,
1,
0.1f,
123.45f,
float.MinValue,
float.MaxValue,
];

return values;
}
}

public static IEnumerable<long> Int64Data => ULongValues.Select(x => (long) x);

public static IEnumerable<ulong> UInt64Data => ULongValues;

public static IEnumerable<double> DoubleData
{
get
{
double[] values =
[
0,
1,
0.1,
123.45,
0.0000001,
(double) ulong.MaxValue * 33,
double.MinValue,
double.MaxValue,
];

return values;
}
}

private static readonly short[] ShortValues =
[
0,
1,
123,
12345,
-1,
-123,
-12345,
1 << 8,
1 | 2 << 8,
short.MinValue,
short.MaxValue
];

private static readonly uint[] UIntValues =
[
uint.MinValue,
uint.MaxValue,
1,
123,
12345,
1 << 8,
1 << 16,
1 << 24,
1 | 2 << 8,
1 | 2 << 8 | 3 << 16,
1 | 2 << 8 | 3 << 24,
1 | 2 << 8 | 3 << 16 | 4 << 24,
0xf,
0xf << 8,
0xf << 16,
0xf << 24,
];

private static readonly ulong[] ULongValues = UIntValues.Select(x => (ulong)x)
.SelectMany(x => new[] { x, x << 8, x << 16, x << 24, x << 32 })
.Concat(UIntValues.SelectMany(_ => UIntValues, (a, b) => (ulong)a << 32 | b))
.Concat(UIntValues.SelectMany(_ => UIntValues, (a, b) => a | (ulong)b << 32)).Distinct().ToArray();
}
22 changes: 22 additions & 0 deletions Sally7.Tests/TestValuesDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Xunit.Sdk;

namespace Sally7.Tests;

internal sealed class TestValuesDataAttribute : DataAttribute
{
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
var paramType = testMethod.GetParameters()[0].ParameterType;
var dataProperty = typeof(TestValues).GetProperty($"{paramType.Name}Data") ??
throw new ArgumentException($"No data available for type {paramType}");

var data = dataProperty.GetValue(null) as IEnumerable ??
throw new NotSupportedException($"Data from {dataProperty} could not be converted to {nameof(IEnumerable)}.");

foreach (var value in data) yield return [value];
}
}
Loading

0 comments on commit 3927553

Please sign in to comment.