-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
355 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,15 @@ public async Task Run(PersonModel context, Func<PersonModel, Task> executeNext) | |
await executeNext(context); | ||
} | ||
} | ||
|
||
public class ThrowIfCancellationRequestedMiddleware : ICancellableAsyncMiddleware<PersonModel> | ||
{ | ||
public async Task Run(PersonModel context, Func<PersonModel, Task> executeNext, CancellationToken cancellationToken) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
await executeNext(context); | ||
} | ||
} | ||
#endregion | ||
|
||
[Fact] | ||
|
@@ -162,5 +171,33 @@ public void Add_AddTypeThatIsNotAMiddleware_ThrowsException() | |
pipeline.Add(typeof(AsyncPipelineTests)); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_RunPipelineWithCancellableMiddleware_CancellableMiddlewareIsExecuted() | ||
{ | ||
var pipeline = new AsyncPipeline<PersonModel>(new ActivatorMiddlewareResolver()) | ||
.Add<PersonWithEvenId>() | ||
.Add<PersonWithOddId>() | ||
.Add<PersonWithEmailName>() | ||
.Add<PersonWithGenderProperty>() | ||
.AddCancellable<ThrowIfCancellationRequestedMiddleware>(); | ||
|
||
// Create a new instance with a 'Gender' property. The 'ThrowIfCancellationRequestedMiddleware' | ||
// middleware should be the last one to execute. | ||
var personModel = new PersonModel | ||
{ | ||
Name = "[email protected]", | ||
Gender = Gender.Other | ||
}; | ||
|
||
// Create the cancellation token in the canceled state. | ||
var cancellationToken = new CancellationToken(canceled: true); | ||
|
||
// Check if 'ThrowIfCancellationRequestedMiddleware' threw 'OperationCanceledException'. | ||
await Assert.ThrowsAsync<OperationCanceledException>(() => pipeline.Execute(personModel, cancellationToken)); | ||
|
||
// Check if the level of 'personModel' is 4, which is configured by 'PersonWithGenderProperty' middleware. | ||
Assert.Equal(4, personModel.Level); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using PipelineNet.MiddlewareResolver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace PipelineNet | ||
{ | ||
/// <summary> | ||
/// Defines the base class for asynchronous middleware flows. | ||
/// </summary> | ||
/// <typeparam name="TMiddleware">The middleware type.</typeparam> | ||
/// <typeparam name="TCancellableMiddleware">The cancellable middleware type.</typeparam> | ||
public abstract class AsyncBaseMiddlewareFlow<TMiddleware, TCancellableMiddleware> | ||
{ | ||
/// <summary> | ||
/// The list of middleware types. | ||
/// </summary> | ||
protected IList<Type> MiddlewareTypes { get; private set; } | ||
|
||
/// <summary> | ||
/// The resolver used to create the middleware types. | ||
/// </summary> | ||
protected IMiddlewareResolver MiddlewareResolver { get; private set; } | ||
|
||
internal AsyncBaseMiddlewareFlow(IMiddlewareResolver middlewareResolver) | ||
{ | ||
MiddlewareResolver = middlewareResolver ?? throw new ArgumentNullException("middlewareResolver", | ||
"An instance of IMiddlewareResolver must be provided. You can use ActivatorMiddlewareResolver."); | ||
MiddlewareTypes = new List<Type>(); | ||
} | ||
|
||
/// <summary> | ||
/// Stores the <see cref="TypeInfo"/> of the middleware type. | ||
/// </summary> | ||
private static readonly TypeInfo MiddlewareTypeInfo = typeof(TMiddleware).GetTypeInfo(); | ||
|
||
/// <summary> | ||
/// Stores the <see cref="TypeInfo"/> of the cancellable middleware type. | ||
/// </summary> | ||
private static readonly TypeInfo CancellableMiddlewareTypeInfo = typeof(TCancellableMiddleware).GetTypeInfo(); | ||
|
||
|
||
/// <summary> | ||
/// Adds a new middleware type to the internal list of types. | ||
/// Middleware will be executed in the same order they are added. | ||
/// </summary> | ||
/// <param name="middlewareType">The middleware type to be executed.</param> | ||
/// <exception cref="ArgumentException">Thrown if the <paramref name="middlewareType"/> is | ||
/// not an implementation of <typeparamref name="TMiddleware"/> or <see cref="TCancellableMiddleware"/>.</exception> | ||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="middlewareType"/> is null.</exception> | ||
protected void AddMiddleware(Type middlewareType) | ||
{ | ||
if (middlewareType == null) throw new ArgumentNullException("middlewareType"); | ||
|
||
bool isAssignableFromMiddleware = MiddlewareTypeInfo.IsAssignableFrom(middlewareType.GetTypeInfo()) | ||
|| CancellableMiddlewareTypeInfo.IsAssignableFrom(middlewareType.GetTypeInfo()); | ||
if (!isAssignableFromMiddleware) | ||
throw new ArgumentException( | ||
$"The middleware type must implement \"{typeof(TMiddleware)}\" or \"{typeof(TCancellableMiddleware)}\"."); | ||
|
||
this.MiddlewareTypes.Add(middlewareType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.