Skip to content

Commit

Permalink
Merge pull request #329 from atc-net/feature/maintenance
Browse files Browse the repository at this point in the history
Feature/maintenance
  • Loading branch information
davidkallesen authored Aug 15, 2024
2 parents 1303010 + 616fdce commit 3ceb5cc
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,14 @@ dotnet_diagnostic.SA1615.severity = suggestion # Category: 'Documentation' - El
dotnet_diagnostic.S1168.severity = suggestion # Return an empty collection instead of null
dotnet_diagnostic.S1172.severity = none # False positive: Unused method parameters should be removed
dotnet_diagnostic.S2094.severity = none # Remove this empty class, write its code or make it an "interface".
dotnet_diagnostic.S2325.severity = none # Make xxx to a static method
dotnet_diagnostic.S3267.severity = suggestion # Simplified with LINQ expressions
dotnet_diagnostic.S4456.severity = none # Split method into two - one validating the parameters and one handling the iterator
dotnet_diagnostic.S4457.severity = none # Split this method into two, one handling parameters check and the other handling the asynchronous code
dotnet_diagnostic.S4487.severity = none # Remove this unread private field 'testOutputHelper' or refactor the code to use its value.
dotnet_diagnostic.S6562.severity = suggestion # Provide the "DateTimeKind" when creating this object
dotnet_diagnostic.S6588.severity = suggestion # Use "DateTime.UnixEpoch" instead of creating DateTime instances that point to the unix epoch time
dotnet_diagnostic.S6608.severity = none # Indexing at 0 should be used instead of the "Enumerable" extension method "First"

dotnet_diagnostic.IDE0079.severity = suggestion # Remove unnecessary suppression (not working with all SonarSource) - https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0079

Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.161" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.163" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.30.0.95878" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167" PrivateAssets="All" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Atc.OpenApi/Atc.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.15" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.17" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.Rest.Extended/Atc.Rest.Extended.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Atc.Rest.HealthChecks/Atc.Rest.HealthChecks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.7" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Mvc.Filters;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ErrorHandlingExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly TelemetryClient telemetryClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ReSharper disable once CheckNamespace
namespace System.ComponentModel.DataAnnotations;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class KeyStringAttribute : StringAttribute
{
public KeyStringAttribute()
Expand Down
25 changes: 21 additions & 4 deletions src/Atc/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the asynchronous operation to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the number of elements in the sequence.</returns>
/// <exception cref="ArgumentNullException">Thrown when the source sequence is null.</exception>
public static Task<int> CountAsync<T>(
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1312:Variable names should begin with lower-case letter", Justification = "False/Positive")]
public static async Task<int> CountAsync<T>(
this IEnumerable<T> source,
CancellationToken cancellationToken = default)
{
Expand All @@ -47,7 +48,15 @@ public static Task<int> CountAsync<T>(
throw new ArgumentNullException(nameof(source));
}

return Task.Run(source.Count, cancellationToken);
var count = 0;
foreach (var _ in source)
{
cancellationToken.ThrowIfCancellationRequested();
count++;
await Task.Yield();
}

return count;
}

/// <summary>
Expand All @@ -58,7 +67,7 @@ public static Task<int> CountAsync<T>(
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the asynchronous operation to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a list with the elements from the input sequence.</returns>
/// <exception cref="ArgumentNullException">Thrown when the source sequence is null.</exception>
public static Task<List<T>> ToListAsync<T>(
public static async Task<List<T>> ToListAsync<T>(
this IEnumerable<T> source,
CancellationToken cancellationToken = default)
{
Expand All @@ -67,7 +76,15 @@ public static Task<List<T>> ToListAsync<T>(
throw new ArgumentNullException(nameof(source));
}

return Task.Run(source.ToList, cancellationToken);
var list = new List<T>();
foreach (var item in source)
{
cancellationToken.ThrowIfCancellationRequested();
list.Add(item);
await Task.Yield();
}

return list;
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Atc/Factories/AsyncEnumerableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ public static async IAsyncEnumerable<T> Empty<T>()
await Task.CompletedTask;
yield break;
}

/// <summary>
/// Converts a single item into an <see cref="IAsyncEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="item">The item to convert.</param>
/// <returns>An <see cref="IAsyncEnumerable{T}"/> containing the single item.</returns>
public static async IAsyncEnumerable<T> FromSingleItem<T>(T item)
{
yield return item;
await Task.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.141" PrivateAssets="All" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions test/Atc.Tests/Factories/AsyncEnumerableFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,67 @@ public async Task Empty_ReturnsEmptySequence_WithDifferentTypes()
Assert.Empty(stringResult);
Assert.Empty(customTypeResult);
}

[Fact]
public async Task FromSingleItem_ReturnsAsyncEnumerableWithSingleItem()
{
// Arrange
const int item = 42;

// Act
var result = new List<int>();
await foreach (var value in AsyncEnumerableFactory.FromSingleItem(item))
{
result.Add(value);
}

// Assert
Assert.Single(result);
Assert.Equal(item, result.First());
}

[Fact]
public async Task FromSingleItem_ReturnsAsyncEnumerable_WithReferenceType()
{
// Arrange
const string item = "TestString";

// Act
var result = new List<string>();
await foreach (var value in AsyncEnumerableFactory.FromSingleItem(item))
{
result.Add(value);
}

// Assert
Assert.Single(result);
Assert.Equal(item, result.First());
}

[Fact]
public async Task FromSingleItem_CanBeEnumeratedMultipleTimes()
{
// Arrange
var item = 42;
var asyncEnumerable = AsyncEnumerableFactory.FromSingleItem(item);

// Act
var firstEnumeration = new List<int>();
await foreach (var value in asyncEnumerable)
{
firstEnumeration.Add(value);
}

var secondEnumeration = new List<int>();
await foreach (var value in asyncEnumerable)
{
secondEnumeration.Add(value);
}

// Assert
Assert.Single(firstEnumeration);
Assert.Single(secondEnumeration);
Assert.Equal(item, firstEnumeration.First());
Assert.Equal(item, secondEnumeration.First());
}
}

0 comments on commit 3ceb5cc

Please sign in to comment.