Skip to content

Commit

Permalink
This commented out pipeline should be working but I'm getting "Exit c…
Browse files Browse the repository at this point in the history
…ode is 134 (Output is too long"
  • Loading branch information
aritchie committed Jun 3, 2024
1 parent 167f115 commit 8aaa2c4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
36 changes: 16 additions & 20 deletions src/Shiny.Mediator/Impl/DefaultRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,29 @@ async Task<TResult> ExecuteMiddleware<TRequest, TResult>(
CancellationToken cancellationToken
) where TRequest : IRequest<TResult>
{
var middlewareType = typeof(IRequestMiddleware<,>).MakeGenericType(request.GetType(), typeof(TResult));
var middlewareMethod = middlewareType.GetMethod("Process", BindingFlags.Instance | BindingFlags.Public)!;
var middlewares = scope.ServiceProvider.GetServices(middlewareType).ToList();
// var middlewareType = typeof(IRequestMiddleware<,>).MakeGenericType(request.GetType(), typeof(TResult));
// var middlewareMethod = middlewareType.GetMethod("Process", BindingFlags.Instance | BindingFlags.Public)!;
// var middlewares = scope.ServiceProvider.GetServices(middlewareType).ToList();
// var pipeline = new List<Func<Task<TResult>>> { initialExecute };

// we get the middleware reverse ordered from last to first so execution
// middlewares.Reverse();
// foreach (var middleware in middlewares)
// {
// var next = () =>
// {
// return (Task<TResult>)middlewareMethod.Invoke(middleware, [
// request,
// next,
// cancellationToken
// ]);
// };
// var pipelineAdd = () => (Task<TResult>)middlewareMethod.Invoke(middleware, [
// request,
// pipeline.Last(),
// cancellationToken
// ])!;
// pipeline.Add(pipelineAdd);
// }
//
// await next!.Invoke().ConfigureAwait(false);
// we setup execution in reverse - with the top being our start/await point
// middlewares.Reverse();
// var next = initialExecute;
//
// foreach (var middleware in middlewares)
// next = () => (Task<TResult>)middlewareMethod.Invoke(middleware, [request, next, cancellationToken]);
//
// var result = await next().ConfigureAwait(false);
// var result = await pipeline
// .Last()
// .Invoke()
// .ConfigureAwait(false);
// return result;

var result = await initialExecute.Invoke().ConfigureAwait(false);
return result;
}
Expand Down
51 changes: 27 additions & 24 deletions tests/Shiny.Mediator.Tests/MiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,47 @@ public MiddlewareTests()
}


public async Task Constrained()
{

}


[Fact]
public async Task ConstrainedAndOpen()
[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
public async Task ConstrainedAndOpen(bool addConstrained, bool addOpen)
{
var services = new ServiceCollection();
services.AddShinyMediator();
services.AddSingletonAsImplementedInterfaces<MiddlewareRequestResponseHandler>();
services.AddSingletonAsImplementedInterfaces<ConstrainedMiddleware>();
services.AddSingleton(typeof(IRequestMiddleware<,>), typeof(VariantRequestMiddleware<,>));
services.AddSingletonAsImplementedInterfaces<MiddlewareRequestResultHandler>();

if (addConstrained)
services.AddSingleton<IRequestMiddleware<MiddlewareResultRequest, int>, ConstrainedMiddleware>();
//services.AddSingletonAsImplementedInterfaces<ConstrainedMiddleware>();

if (addOpen)
services.AddSingleton(typeof(IRequestMiddleware<,>), typeof(VariantRequestMiddleware<,>));

var sp = services.BuildServiceProvider();

var mediator = sp.GetRequiredService<IMediator>();
var result = await mediator.Request(new MiddlewareResponseRequest());
Executed.Constrained.Should().BeTrue();
Executed.Variant.Should().BeTrue();
var result = await mediator.Request(new MiddlewareResultRequest());
Executed.Constrained.Should().Be(addConstrained);
Executed.Variant.Should().Be(addOpen);
}
}


public class MiddlewareRequest : IRequest;

public class MiddlewareResponseRequest : IRequest<int>;
public class MiddlewareResultRequest : IRequest<int>;


public class MiddlewareRequestHandler : IRequestHandler<MiddlewareRequest>
{
public Task Handle(MiddlewareRequest request, CancellationToken cancellationToken)
=> Task.CompletedTask;
}
// public class MiddlewareRequestHandler : IRequestHandler<MiddlewareRequest>
// {
// public Task Handle(MiddlewareRequest request, CancellationToken cancellationToken)
// => Task.CompletedTask;
// }

public class MiddlewareRequestResponseHandler : IRequestHandler<MiddlewareResponseRequest, int>
public class MiddlewareRequestResultHandler : IRequestHandler<MiddlewareResultRequest, int>
{
public Task<int> Handle(MiddlewareResponseRequest request, CancellationToken cancellationToken)
public Task<int> Handle(MiddlewareResultRequest request, CancellationToken cancellationToken)
=> Task.FromResult(1);
}

Expand All @@ -57,9 +60,9 @@ public static class Executed
public static bool Constrained { get; set; }
public static bool Variant { get; set; }
}
public class ConstrainedMiddleware : IRequestMiddleware<MiddlewareResponseRequest, int>
public class ConstrainedMiddleware : IRequestMiddleware<MiddlewareResultRequest, int>
{
public Task<int> Process(MiddlewareResponseRequest request, Func<Task<int>> next, CancellationToken cancellationToken)
public Task<int> Process(MiddlewareResultRequest request, Func<Task<int>> next, CancellationToken cancellationToken)
{
Executed.Constrained = true;
return next();
Expand Down

0 comments on commit 8aaa2c4

Please sign in to comment.