From 5241b2201ebfc7a62db03ba48b23120d2d7e7227 Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Tue, 25 Jul 2023 20:57:10 +0900 Subject: [PATCH] patch: update example application using step (#37) --- BatchSharp.Example/ExampleBatchApplication.cs | 13 +-- BatchSharp.Example/Program.cs | 3 + .../DefaultBatchApplicationTest.cs | 104 +++--------------- BatchSharp/DefaultBatchApplication.cs | 41 +++---- README.md | 18 +-- docs/index.md | 2 + 6 files changed, 41 insertions(+), 140 deletions(-) diff --git a/BatchSharp.Example/ExampleBatchApplication.cs b/BatchSharp.Example/ExampleBatchApplication.cs index 3f70b54..d7aad15 100644 --- a/BatchSharp.Example/ExampleBatchApplication.cs +++ b/BatchSharp.Example/ExampleBatchApplication.cs @@ -1,5 +1,6 @@ using BatchSharp.Processor; using BatchSharp.Reader; +using BatchSharp.Step; using BatchSharp.Writer; using Microsoft.Extensions.Logging; @@ -9,21 +10,17 @@ namespace BatchSharp.Example; /// /// Class of example batch application. /// -public class ExampleBatchApplication : DefaultBatchApplication +public class ExampleBatchApplication : DefaultBatchApplication { /// /// Initializes a new instance of the class. /// /// Logger. - /// Reader. - /// Processor. - /// Writer. + /// Step. public ExampleBatchApplication( ILogger logger, - IReader reader, - IProcessor processor, - IWriter writer) - : base(logger, reader, processor, writer) + IStep step) + : base(logger, step) { } } \ No newline at end of file diff --git a/BatchSharp.Example/Program.cs b/BatchSharp.Example/Program.cs index 99ae9c1..11c8f9e 100644 --- a/BatchSharp.Example/Program.cs +++ b/BatchSharp.Example/Program.cs @@ -5,6 +5,7 @@ using BatchSharp.Example.Writer; using BatchSharp.Processor; using BatchSharp.Reader; +using BatchSharp.Step; using BatchSharp.Writer; using Microsoft.Extensions.DependencyInjection; @@ -22,6 +23,8 @@ .AddScoped, ExampleReader>(); services.AddScoped, ExampleProcessor>(); services.AddScoped, ExampleWriter>(); + services.AddScoped>(); + services.AddScoped(); }); var app = builder.Build(); diff --git a/BatchSharp.Tests/DefaultBatchApplicationTest.cs b/BatchSharp.Tests/DefaultBatchApplicationTest.cs index 062b49b..28535bb 100644 --- a/BatchSharp.Tests/DefaultBatchApplicationTest.cs +++ b/BatchSharp.Tests/DefaultBatchApplicationTest.cs @@ -1,5 +1,6 @@ using BatchSharp.Processor; using BatchSharp.Reader; +using BatchSharp.Step; using BatchSharp.Writer; using Microsoft.Extensions.Logging; @@ -13,95 +14,22 @@ namespace BatchSharp.Tests; /// public class DefaultBatchApplicationTest { - private readonly Mock> _reader = new(); - private readonly Mock> _processor = new(); - private readonly Mock> _writer = new(); + private readonly Mock> _logger = new(); + private readonly Mock _step = new(); /// /// Test for . - /// If reader returns single element list, should return completed. /// /// Asynchronous task. [Fact] - public async Task ShouldReturnCompletedWhenReadSingleAsync() + public async Task ShouldReturnCompletedWhenExecutionSuccess() { - var logger = new Mock>>(); - _reader.SetupSequence(x => x.ReadAsync()) - .Returns(new[] { "test" }.ToAsyncEnumerable()) - .Returns(AsyncEnumerable.Empty()); - - _processor.SetupSequence(x => x.Process("test")) - .Returns(4); - _writer.SetupSequence(x => x.WriteAsync(It.IsIn(4), It.IsAny())) - .Returns(Task.CompletedTask); - var application = - new DefaultBatchApplication( - logger.Object, - _reader.Object, - _processor.Object, - _writer.Object); - - await application.RunAsync(); - - _reader.Verify((r) => r.ReadAsync(), Times.Once()); - _processor.Verify(p => p.Process("test"), Times.Once()); - _writer.Verify(x => x.WriteAsync(It.IsIn(4), It.IsAny()), Times.Once()); - } - - /// - /// Test for . - /// Should return completed when reader returns empty list. - /// - /// Asynchronous task. - [Fact] - public async Task ShouldReturnCompletedWhenReadEmptyAsync() - { - var logger = new Mock>>(); - _reader.SetupSequence(x => x.ReadAsync()) - .Returns(AsyncEnumerable.Empty()); - _processor.SetupSequence(x => x.Process(It.IsAny())); - _writer.SetupSequence(x => x.WriteAsync(It.IsAny(), It.IsAny())); - var application = - new DefaultBatchApplication( - logger.Object, - _reader.Object, - _processor.Object, - _writer.Object); + _step.Setup(x => x.ExecuteAsync(default)); + var application = new DefaultBatchApplication(_logger.Object, _step.Object); await application.RunAsync(); - _reader.Verify(r => r.ReadAsync(), Times.Once()); - _processor.Verify(x => x.Process(It.IsAny()), Times.Never()); - _writer.Verify(x => x.WriteAsync(It.IsAny(), It.IsAny()), Times.Never()); - } - - /// - /// Test for . - /// - /// Asynchronous task. - [Fact] - public async Task ShouldReturnCompletedWhenReadSingleWithCancellationTokenAsync() - { - var logger = new Mock>>(); - _reader.SetupSequence(x => x.ReadAsync()) - .Returns(new[] { "test" }.ToAsyncEnumerable()) - .Returns(AsyncEnumerable.Empty()); - _processor.SetupSequence(x => x.Process(It.IsIn("test"))) - .Returns(4); - _writer.SetupSequence(x => x.WriteAsync(It.IsIn(4), It.IsAny())) - .Returns(Task.CompletedTask); - var application = - new DefaultBatchApplication( - logger.Object, - _reader.Object, - _processor.Object, - _writer.Object); - using var cancellationTokenSource = new CancellationTokenSource(); - await application.RunAsync(cancellationTokenSource.Token); - - _reader.Verify((r) => r.ReadAsync(), Times.Once()); - _processor.Verify(p => p.Process(It.IsIn("test")), Times.Once()); - _writer.Verify(x => x.WriteAsync(It.IsIn(4), It.IsAny()), Times.Once()); + _step.Verify(x => x.ExecuteAsync(default), Times.Once()); } /// @@ -111,22 +39,16 @@ public async Task ShouldReturnCompletedWhenReadSingleWithCancellationTokenAsync( [Fact] public async Task ShouldReturnCancelledAsync() { - var logger = new Mock>>(); - _reader.Setup(x => x.ReadAsync()); - _processor.Setup(x => x.Process(It.IsAny())); - _writer.Setup(x => x.WriteAsync(It.IsAny(), It.IsAny())); + _step.Setup(x => x.ExecuteAsync(It.IsAny())); var application = - new DefaultBatchApplication( - logger.Object, - _reader.Object, - _processor.Object, - _writer.Object); + new DefaultBatchApplication( + _logger.Object, + _step.Object); + using var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Cancel(); await Assert.ThrowsAsync(() => application.RunAsync(cancellationTokenSource.Token)); - _reader.Verify(r => r.ReadAsync(), Times.Never()); - _processor.Verify(x => x.Process(It.IsAny()), Times.Never()); - _writer.Verify(x => x.WriteAsync(It.IsAny(), It.IsAny()), Times.Never()); + _step.Verify(x => x.ExecuteAsync(It.IsAny()), Times.Never()); } } \ No newline at end of file diff --git a/BatchSharp/DefaultBatchApplication.cs b/BatchSharp/DefaultBatchApplication.cs index aea48c7..5e4de0c 100644 --- a/BatchSharp/DefaultBatchApplication.cs +++ b/BatchSharp/DefaultBatchApplication.cs @@ -1,5 +1,8 @@ +using System.Runtime.CompilerServices; + using BatchSharp.Processor; using BatchSharp.Reader; +using BatchSharp.Step; using BatchSharp.Writer; using Microsoft.Extensions.Logging; @@ -9,34 +12,22 @@ namespace BatchSharp; /// /// Class for running a batch application. /// -/// DataBinding class of datasource. -/// Processing result type. -public class DefaultBatchApplication : IBatchApplication - where TRead : notnull - where TResult : notnull +public class DefaultBatchApplication : IBatchApplication { - private readonly ILogger> _logger; - private readonly IReader _reader; - private readonly IProcessor _processor; - private readonly IWriter _writer; + private readonly ILogger _logger; + private readonly IStep _step; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Logger. - /// Reader instance. - /// Processor instance. - /// Writer instance. + /// Step. public DefaultBatchApplication( - ILogger> logger, - IReader reader, - IProcessor processor, - IWriter writer) + ILogger logger, + IStep step) { _logger = logger; - _reader = reader; - _processor = processor; - _writer = writer; + _step = step; } /// @@ -50,13 +41,9 @@ public async Task RunAsync(CancellationToken cancellationToken) { if (!cancellationToken.IsCancellationRequested) { - await foreach (var readData in _reader.ReadAsync().WithCancellation(cancellationToken)) - { - _logger.LogInformation("Read data: {Item}", readData); - var processingResult = _processor.Process(readData); - _logger.LogInformation("Processed data: {Item}", processingResult); - await _writer.WriteAsync(processingResult, cancellationToken); - } + _logger.LogDebug("Start batch application."); + await _step.ExecuteAsync(cancellationToken); + _logger.LogDebug("End batch application."); } else { diff --git a/README.md b/README.md index 21333dc..f80f5a9 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ BatchSharp can be installed using the Nuget package manager or the `dotnet` CLI. .AddScoped, ExampleReader>(); services.AddScoped, ExampleProcessor>(); services.AddScoped, ExampleWriter>(); + services.AddScoped>(); + services.AddScoped(); }); var app = builder.Build(); @@ -69,24 +71,12 @@ BatchSharp can be installed using the Nuget package manager or the `dotnet` CLI. `IReader` is used to read input data. `IProcessor` is used to process input data. `IWriter` is used to write output data. These class are injected by DI container. ```csharp - /// - /// Class of example batch application. - /// public class ExampleBatchApplication : DefaultBatchApplication { - /// - /// Initializes a new instance of the class. - /// - /// Logger. - /// Reader. - /// Processor. - /// Writer. public ExampleBatchApplication( ILogger logger, - IReader reader, - IProcessor processor, - IWriter writer) - : base(logger, reader, processor, writer) + IStep step) + : base(logger, step) { } } diff --git a/docs/index.md b/docs/index.md index d8cd25a..97cc05b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -76,6 +76,8 @@ Refer to [Markdown](http://daringfireball.net/projects/markdown/) for how to wri services.AddScoped(c => new FileWriterSetting("output.txt")); // Register writer class services.AddScoped, ExampleWriter>(); + services.AddScoped>(); + services.AddScoped(); }); var app = builder.Build();