Skip to content

Commit

Permalink
fix: fix sast issue on IBatchApplication (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuju63 authored Jul 1, 2023
1 parent a044e2b commit 3d53fe9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
32 changes: 32 additions & 0 deletions BatchSharp.Tests/DefaultBatchApplicationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,36 @@ public async Task ShouldReturnCompletedWhenReadEmptyAsync()
processor.Verify(x => x.Process(It.IsAny<string>()), Times.Never());
writer.Verify(x => x.WriteAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Never());
}

/// <summary>
/// Test for <see cref="DefaultBatchApplication{TRead,TResult}.RunAsync"/>.
/// </summary>
/// <returns>Asynchronous task.</returns>
[Fact]
public async Task ShouldReturnCompletedWhenReadSingleWithCancellationTokenAsync()
{
var logger = new Mock<ILogger<DefaultBatchApplication<string, int>>>();
var reader = new Mock<IReader<string>>();
reader.SetupSequence(x => x.Read())
.Returns(new List<string> { "test" })
.Returns(new List<string>());
var processor = new Mock<IProcessor<string, int>>();
processor.SetupSequence(x => x.Process(It.IsIn("test")))
.Returns(4);
var writer = new Mock<IWriter<int>>();
writer.SetupSequence(x => x.WriteAsync(It.IsIn(4), It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
var application =
new DefaultBatchApplication<string, int>(
logger.Object,
reader.Object,
processor.Object,
writer.Object);
using var cancellationTokenSource = new CancellationTokenSource();
await application.RunAsync(cancellationTokenSource.Token);

reader.Verify((r) => r.Read(), Times.Exactly(2));
processor.Verify(p => p.Process(It.IsIn("test")), Times.Once());
writer.Verify(x => x.WriteAsync(It.IsIn(4), cancellationTokenSource.Token), Times.Once());
}
}
21 changes: 18 additions & 3 deletions BatchSharp/DefaultBatchApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,28 @@ public DefaultBatchApplication(
}

/// <inheritdoc/>
public async Task RunAsync(CancellationToken cancellationToken = default)
public async Task RunAsync()
{
IEnumerable<TRead> readData;
while ((readData = _reader.Read()).Any())
{
await Task.Run(
await RunAsync(readData, default);
}
}

/// <inheritdoc/>
public async Task RunAsync(CancellationToken cancellationToken)
{
IEnumerable<TRead> readData;
while ((readData = _reader.Read()).Any())
{
await RunAsync(readData, cancellationToken);
}
}

private Task RunAsync(IEnumerable<TRead> readData, CancellationToken cancellationToken)
{
return Task.Run(
async () =>
{
foreach (var item in readData)
Expand All @@ -57,6 +73,5 @@ await Task.Run(
}
},
cancellationToken);
}
}
}
8 changes: 7 additions & 1 deletion BatchSharp/IBatchApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ namespace BatchSharp;
/// </summary>
public interface IBatchApplication
{
/// <summary>
/// Execute batch logic by asynchronous.
/// </summary>
/// <returns>Task result.</returns>
Task RunAsync();

/// <summary>
/// Execute batch logic by asynchronous.
/// </summary>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>task result.</returns>
Task RunAsync(CancellationToken cancellationToken = default);
Task RunAsync(CancellationToken cancellationToken);
}

0 comments on commit 3d53fe9

Please sign in to comment.