From da96608cbf33b5f2ccaa4b30246da9857deb365e Mon Sep 17 00:00:00 2001 From: Max Vorchakov Date: Sun, 10 Dec 2023 16:31:30 +0100 Subject: [PATCH] Refactored Task to ValueTask (#51) --- samples/PowerPipe.Sample/SamplePipeline.cs | 2 +- samples/PowerPipe.Sample/Steps/SampleGenericStep.cs | 2 +- samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs | 6 +++--- .../Steps/SampleParallelStep5Compensation.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleStep1.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleStep2.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs | 6 +++--- samples/PowerPipe.Sample/Steps/SampleStep3.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep4.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep5.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep6.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep7.cs | 4 ++-- samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs | 6 +++--- src/PowerPipe/Builder/Steps/AddIfElseStep.cs | 4 ++-- src/PowerPipe/Builder/Steps/AddIfStep.cs | 4 ++-- src/PowerPipe/Builder/Steps/CompensationStep.cs | 2 +- src/PowerPipe/Builder/Steps/IfPipelineStep.cs | 4 ++-- src/PowerPipe/Builder/Steps/InternalStep.cs | 8 ++++---- src/PowerPipe/Builder/Steps/LazyStep.cs | 4 ++-- src/PowerPipe/Builder/Steps/ParallelStep.cs | 4 ++-- src/PowerPipe/Interfaces/IPipeline.cs | 6 +++--- src/PowerPipe/Interfaces/IPipelineCompensationStep.cs | 6 +++--- src/PowerPipe/Interfaces/IStepBase.cs | 6 +++--- src/PowerPipe/Pipeline.cs | 2 +- .../PipelineDependencyInjectionTests.cs | 2 +- tests/PowerPipe.UnitTests/PipelineTests.cs | 5 ++--- tests/PowerPipe.UnitTests/Steps/TestCompensationStep.cs | 6 +++--- tests/PowerPipe.UnitTests/Steps/TestGenericStep.cs | 2 +- tests/PowerPipe.UnitTests/Steps/TestParallelStep.cs | 4 ++-- tests/PowerPipe.UnitTests/Steps/TestStep1.cs | 4 ++-- tests/PowerPipe.UnitTests/Steps/TestStep2.cs | 4 ++-- tests/PowerPipe.UnitTests/Steps/TestStep3.cs | 4 ++-- 39 files changed, 89 insertions(+), 90 deletions(-) diff --git a/samples/PowerPipe.Sample/SamplePipeline.cs b/samples/PowerPipe.Sample/SamplePipeline.cs index 2df1cba..af60dc8 100644 --- a/samples/PowerPipe.Sample/SamplePipeline.cs +++ b/samples/PowerPipe.Sample/SamplePipeline.cs @@ -60,5 +60,5 @@ public class SamplePipelineContext : PipelineContext public int Test1 { get; set; } public override SamplePipelineResult GetPipelineResult() => null; -}; +} diff --git a/samples/PowerPipe.Sample/Steps/SampleGenericStep.cs b/samples/PowerPipe.Sample/Steps/SampleGenericStep.cs index 3381d90..f786e69 100644 --- a/samples/PowerPipe.Sample/Steps/SampleGenericStep.cs +++ b/samples/PowerPipe.Sample/Steps/SampleGenericStep.cs @@ -9,7 +9,7 @@ public class SampleGenericStep: IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleGenericStep)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs index 4dcaa92..f768f74 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep1 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep1)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs index 18df867..355ad3f 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep2 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep2)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs index 9455eb2..dad6573 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep3 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep3)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs index 65b7446..c9a3ee0 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs @@ -7,12 +7,12 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep4 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep4)} Executed"); throw new InvalidOperationException(); - // return Task.CompletedTask; + // return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs index 2bc9de5..54f7aae 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs @@ -7,12 +7,12 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep5 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep5)} Executed"); // throw new InvalidOperationException(); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep5Compensation.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep5Compensation.cs index b1a904b..ff8f286 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep5Compensation.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep5Compensation.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep5Compensation : IPipelineCompensationStep { - public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep5Compensation)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs index 651294e..caf3681 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep6 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep6)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs b/samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs index 64a1890..1534b6d 100644 --- a/samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs +++ b/samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleParallelStep7 : IPipelineParallelStep { - public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleParallelStep7)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep1.cs b/samples/PowerPipe.Sample/Steps/SampleStep1.cs index 3b29daa..f9ba88a 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep1.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep1.cs @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep1 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep1)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs b/samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs index 24bd96c..e08c484 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep1Compensation : IPipelineCompensationStep { - public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep1Compensation)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep2.cs b/samples/PowerPipe.Sample/Steps/SampleStep2.cs index a0502b4..7a68cd4 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep2.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep2.cs @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep2 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep2)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs b/samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs index 0fd66cc..8b5ad49 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep2Compensation : IPipelineCompensationStep { - public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep2Compensation)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep3.cs b/samples/PowerPipe.Sample/Steps/SampleStep3.cs index e1543e7..52f5aa4 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep3.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep3.cs @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep3 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep3)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep4.cs b/samples/PowerPipe.Sample/Steps/SampleStep4.cs index a4c5a38..bf90f4d 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep4.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep4.cs @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep4 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep4)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep5.cs b/samples/PowerPipe.Sample/Steps/SampleStep5.cs index 17a6f59..1cb2352 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep5.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep5.cs @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep5 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep5)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep6.cs b/samples/PowerPipe.Sample/Steps/SampleStep6.cs index 3b3d532..68c8993 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep6.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep6.cs @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep6 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep6)} Executed"); await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep7.cs b/samples/PowerPipe.Sample/Steps/SampleStep7.cs index 2fc39d8..7ef260f 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep7.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep7.cs @@ -8,7 +8,7 @@ namespace PowerPipe.Sample.Steps; public class SampleStep7 : IPipelineStep { public IPipelineStep NextStep { get; set; } - public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep7)} Executed"); @@ -16,4 +16,4 @@ public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs b/samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs index b05f09b..c5e8594 100644 --- a/samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs +++ b/samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps; public class SampleStep7Compensation : IPipelineCompensationStep { - public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) + public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken) { Console.WriteLine($"{nameof(SampleStep7Compensation)} Executed"); - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/src/PowerPipe/Builder/Steps/AddIfElseStep.cs b/src/PowerPipe/Builder/Steps/AddIfElseStep.cs index 068ba2c..4b21acf 100644 --- a/src/PowerPipe/Builder/Steps/AddIfElseStep.cs +++ b/src/PowerPipe/Builder/Steps/AddIfElseStep.cs @@ -34,7 +34,7 @@ internal AddIfElseStep(Predicate predicate, IPipelineStep if /// The context on which the if-else step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) + protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) { StepExecuted = true; @@ -51,4 +51,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio await _elseStep.ExecuteAsync(context, cancellationToken); } } -} \ No newline at end of file +} diff --git a/src/PowerPipe/Builder/Steps/AddIfStep.cs b/src/PowerPipe/Builder/Steps/AddIfStep.cs index 216e073..1dee3fd 100644 --- a/src/PowerPipe/Builder/Steps/AddIfStep.cs +++ b/src/PowerPipe/Builder/Steps/AddIfStep.cs @@ -31,7 +31,7 @@ internal AddIfStep(Predicate predicate, IPipelineStep step) /// The context on which the if step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) + protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) { if (_predicate(context)) { @@ -47,4 +47,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio await NextStep.ExecuteAsync(context, cancellationToken); } } -} \ No newline at end of file +} diff --git a/src/PowerPipe/Builder/Steps/CompensationStep.cs b/src/PowerPipe/Builder/Steps/CompensationStep.cs index bdd735d..1512b1c 100644 --- a/src/PowerPipe/Builder/Steps/CompensationStep.cs +++ b/src/PowerPipe/Builder/Steps/CompensationStep.cs @@ -29,7 +29,7 @@ internal CompensationStep(Func> factory) } /// - public async Task CompensateAsync(TContext context, CancellationToken cancellationToken) + public async ValueTask CompensateAsync(TContext context, CancellationToken cancellationToken) { IsCompensated = true; diff --git a/src/PowerPipe/Builder/Steps/IfPipelineStep.cs b/src/PowerPipe/Builder/Steps/IfPipelineStep.cs index e4e4259..ca8a6a6 100644 --- a/src/PowerPipe/Builder/Steps/IfPipelineStep.cs +++ b/src/PowerPipe/Builder/Steps/IfPipelineStep.cs @@ -33,7 +33,7 @@ public IfPipelineStep(Predicate predicate, PipelineBuilderThe context on which the conditional pipeline step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) + protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) { if (_predicate(context)) { @@ -45,4 +45,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio if (NextStep is not null) await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/src/PowerPipe/Builder/Steps/InternalStep.cs b/src/PowerPipe/Builder/Steps/InternalStep.cs index 37412cc..97cba3a 100644 --- a/src/PowerPipe/Builder/Steps/InternalStep.cs +++ b/src/PowerPipe/Builder/Steps/InternalStep.cs @@ -73,7 +73,7 @@ public void ConfigureErrorHandling(PipelineStepErrorHandling errorHandling, Time } /// - public async Task ExecuteAsync(TContext context, CancellationToken cancellationToken) + public async ValueTask ExecuteAsync(TContext context, CancellationToken cancellationToken) { try { @@ -107,8 +107,8 @@ public async Task ExecuteAsync(TContext context, CancellationToken cancellationT /// The context on which the step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - protected virtual Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) => - Task.CompletedTask; + protected virtual ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) => + ValueTask.CompletedTask; /// /// Handles an exception that occurred during the step's execution. @@ -116,7 +116,7 @@ protected virtual Task ExecuteInternalAsync(TContext context, CancellationToken /// The context on which the step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. Returns true if the exception is handled; otherwise, false. - protected virtual async Task HandleExceptionAsync(TContext context, CancellationToken cancellationToken) + protected virtual async ValueTask HandleExceptionAsync(TContext context, CancellationToken cancellationToken) { if (ErrorHandlingPredicate is not null && !ErrorHandlingPredicate(context)) return false; diff --git a/src/PowerPipe/Builder/Steps/LazyStep.cs b/src/PowerPipe/Builder/Steps/LazyStep.cs index babaa37..787daed 100644 --- a/src/PowerPipe/Builder/Steps/LazyStep.cs +++ b/src/PowerPipe/Builder/Steps/LazyStep.cs @@ -36,10 +36,10 @@ internal LazyStep(Func> factory) /// The context on which the step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) + protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) { StepExecuted = true; await _step.Value.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/src/PowerPipe/Builder/Steps/ParallelStep.cs b/src/PowerPipe/Builder/Steps/ParallelStep.cs index 94f6e6a..3006b2d 100644 --- a/src/PowerPipe/Builder/Steps/ParallelStep.cs +++ b/src/PowerPipe/Builder/Steps/ParallelStep.cs @@ -32,7 +32,7 @@ public ParallelStep(int maxDegreeOfParallelism, PipelineBuilderThe context on which the step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) + protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) { StepExecuted = true; @@ -47,4 +47,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio if (NextStep is not null) await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/src/PowerPipe/Interfaces/IPipeline.cs b/src/PowerPipe/Interfaces/IPipeline.cs index 42380fb..404d96d 100644 --- a/src/PowerPipe/Interfaces/IPipeline.cs +++ b/src/PowerPipe/Interfaces/IPipeline.cs @@ -14,7 +14,7 @@ public interface IPipeline /// /// A flag indicating whether to return a result. /// A task representing the asynchronous operation and optionally the result. - Task RunAsync(bool returnResult = true) => RunAsync(default, returnResult); + ValueTask RunAsync(bool returnResult = true) => RunAsync(default, returnResult); /// /// Runs the pipeline asynchronously with the option to return a result and a cancellation token. @@ -22,5 +22,5 @@ public interface IPipeline /// A cancellation token to cancel the operation. /// A flag indicating whether to return a result. /// A task representing the asynchronous operation and optionally the result. - Task RunAsync(CancellationToken cancellationToken, bool returnResult = true); -} \ No newline at end of file + ValueTask RunAsync(CancellationToken cancellationToken, bool returnResult = true); +} diff --git a/src/PowerPipe/Interfaces/IPipelineCompensationStep.cs b/src/PowerPipe/Interfaces/IPipelineCompensationStep.cs index 72ddea7..870ee6f 100644 --- a/src/PowerPipe/Interfaces/IPipelineCompensationStep.cs +++ b/src/PowerPipe/Interfaces/IPipelineCompensationStep.cs @@ -14,7 +14,7 @@ public interface IPipelineCompensationStep /// /// The context on which the compensation step operates. /// A task representing the asynchronous compensation operation. - Task CompensateAsync(TContext context) => CompensateAsync(context, default); + ValueTask CompensateAsync(TContext context) => CompensateAsync(context, default); /// /// Compensates for a failed pipeline execution asynchronously with a cancellation token. @@ -22,5 +22,5 @@ public interface IPipelineCompensationStep /// The context on which the compensation step operates. /// A cancellation token to cancel the compensation operation. /// A task representing the asynchronous compensation operation. - Task CompensateAsync(TContext context, CancellationToken cancellationToken); -} \ No newline at end of file + ValueTask CompensateAsync(TContext context, CancellationToken cancellationToken); +} diff --git a/src/PowerPipe/Interfaces/IStepBase.cs b/src/PowerPipe/Interfaces/IStepBase.cs index 89f546b..65a9ea4 100644 --- a/src/PowerPipe/Interfaces/IStepBase.cs +++ b/src/PowerPipe/Interfaces/IStepBase.cs @@ -14,7 +14,7 @@ public interface IStepBase /// /// The context on which the step operates. /// A task representing the asynchronous operation. - Task ExecuteAsync(TContext context) => ExecuteAsync(context, default); + ValueTask ExecuteAsync(TContext context) => ExecuteAsync(context, default); /// /// Executes the step asynchronously with a cancellation token. @@ -22,5 +22,5 @@ public interface IStepBase /// The context on which the step operates. /// A cancellation token to cancel the operation. /// A task representing the asynchronous operation. - Task ExecuteAsync(TContext context, CancellationToken cancellationToken); -} \ No newline at end of file + ValueTask ExecuteAsync(TContext context, CancellationToken cancellationToken); +} diff --git a/src/PowerPipe/Pipeline.cs b/src/PowerPipe/Pipeline.cs index 228e55a..649a8e3 100644 --- a/src/PowerPipe/Pipeline.cs +++ b/src/PowerPipe/Pipeline.cs @@ -28,7 +28,7 @@ public Pipeline(TContext context, IReadOnlyList> steps) } /// - public async Task RunAsync(CancellationToken cancellationToken, bool returnResult = true) + public async ValueTask RunAsync(CancellationToken cancellationToken, bool returnResult = true) { await _initStep.ExecuteAsync(_context, cancellationToken); diff --git a/tests/PowerPipe.UnitTests/PipelineDependencyInjectionTests.cs b/tests/PowerPipe.UnitTests/PipelineDependencyInjectionTests.cs index c7a2f35..32eb247 100644 --- a/tests/PowerPipe.UnitTests/PipelineDependencyInjectionTests.cs +++ b/tests/PowerPipe.UnitTests/PipelineDependencyInjectionTests.cs @@ -37,7 +37,7 @@ public async Task TestAutoDI() .Build(); // To check that the compensation step was resolved and executed TestStep2 throws an exception - var action = () => pipeline.RunAsync(cts.Token); + var action = async () => await pipeline.RunAsync(cts.Token); await action.Should().ThrowAsync(); context.Step1RunCount.Should().Be(1); diff --git a/tests/PowerPipe.UnitTests/PipelineTests.cs b/tests/PowerPipe.UnitTests/PipelineTests.cs index 4f8e49e..9157bf3 100644 --- a/tests/PowerPipe.UnitTests/PipelineTests.cs +++ b/tests/PowerPipe.UnitTests/PipelineTests.cs @@ -1,5 +1,4 @@ using System; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using FluentAssertions; @@ -156,7 +155,7 @@ public async Task OnError_Succeed(PipelineStepErrorHandling errorHandlingBehavio .OnError(errorHandlingBehaviour, maxRetryCount: isRetryBehaviour ? retryCount : default, predicate: ShouldApplyErrorHandling) .Build(); - var action = () => pipeline.RunAsync(cts.Token); + var action = async () => await pipeline.RunAsync(cts.Token); if (applyErrorHandling) { @@ -207,7 +206,7 @@ public async Task CompensateWith_Succeed() .CompensateWith() .Build(); - var action = () => pipeline.RunAsync(cts.Token); + var action = async () => await pipeline.RunAsync(cts.Token); await action.Should().ThrowAsync(); diff --git a/tests/PowerPipe.UnitTests/Steps/TestCompensationStep.cs b/tests/PowerPipe.UnitTests/Steps/TestCompensationStep.cs index f8e6d35..83563ef 100644 --- a/tests/PowerPipe.UnitTests/Steps/TestCompensationStep.cs +++ b/tests/PowerPipe.UnitTests/Steps/TestCompensationStep.cs @@ -6,10 +6,10 @@ namespace PowerPipe.UnitTests.Steps; public class TestCompensationStep : IPipelineCompensationStep { - public virtual Task CompensateAsync(TestPipelineContext context, CancellationToken cancellationToken) + public virtual ValueTask CompensateAsync(TestPipelineContext context, CancellationToken cancellationToken) { context.CompensationStepRunCount++; - return Task.CompletedTask; + return ValueTask.CompletedTask; } -} \ No newline at end of file +} diff --git a/tests/PowerPipe.UnitTests/Steps/TestGenericStep.cs b/tests/PowerPipe.UnitTests/Steps/TestGenericStep.cs index 13a5eb7..96afe4b 100644 --- a/tests/PowerPipe.UnitTests/Steps/TestGenericStep.cs +++ b/tests/PowerPipe.UnitTests/Steps/TestGenericStep.cs @@ -15,7 +15,7 @@ public TestGenericStep() CreationCount++; } - public virtual async Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) + public virtual async ValueTask ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) { context.GenericStepRunCount++; await NextStep.ExecuteAsync(context, cancellationToken); diff --git a/tests/PowerPipe.UnitTests/Steps/TestParallelStep.cs b/tests/PowerPipe.UnitTests/Steps/TestParallelStep.cs index 51e9831..b2455a5 100644 --- a/tests/PowerPipe.UnitTests/Steps/TestParallelStep.cs +++ b/tests/PowerPipe.UnitTests/Steps/TestParallelStep.cs @@ -6,11 +6,11 @@ namespace PowerPipe.UnitTests.Steps; public class TestParallelStep : IPipelineParallelStep { - public Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) + public ValueTask ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) { context.ParallelStepRunCount++; - return Task.CompletedTask; + return ValueTask.CompletedTask; } public IPipelineStep NextStep { get; set; } diff --git a/tests/PowerPipe.UnitTests/Steps/TestStep1.cs b/tests/PowerPipe.UnitTests/Steps/TestStep1.cs index 6c7a341..0c472f1 100644 --- a/tests/PowerPipe.UnitTests/Steps/TestStep1.cs +++ b/tests/PowerPipe.UnitTests/Steps/TestStep1.cs @@ -15,9 +15,9 @@ public TestStep1() CreationCount++; } - public virtual async Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) + public virtual async ValueTask ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) { context.Step1RunCount++; await NextStep.ExecuteAsync(context, cancellationToken); } -} \ No newline at end of file +} diff --git a/tests/PowerPipe.UnitTests/Steps/TestStep2.cs b/tests/PowerPipe.UnitTests/Steps/TestStep2.cs index a608142..191b3be 100644 --- a/tests/PowerPipe.UnitTests/Steps/TestStep2.cs +++ b/tests/PowerPipe.UnitTests/Steps/TestStep2.cs @@ -16,6 +16,6 @@ public TestStep2() CreationCount++; } - public virtual Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) => + public virtual ValueTask ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) => throw new InvalidOperationException(); -} \ No newline at end of file +} diff --git a/tests/PowerPipe.UnitTests/Steps/TestStep3.cs b/tests/PowerPipe.UnitTests/Steps/TestStep3.cs index 9eea544..4c63907 100644 --- a/tests/PowerPipe.UnitTests/Steps/TestStep3.cs +++ b/tests/PowerPipe.UnitTests/Steps/TestStep3.cs @@ -16,6 +16,6 @@ public TestStep3() CreationCount++; } - public virtual Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) => + public virtual ValueTask ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) => throw new InvalidOperationException(); -} \ No newline at end of file +}