Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests and upgrading to .NET 8 preview #955

Merged
merged 9 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="6.5.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Lamar" Version="8.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="LightInject" Version="6.6.1" />
<PackageReference Include="LightInject.Microsoft.DependencyInjection" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="SimpleInjector" Version="5.4.1" />
<PackageReference Include="SimpleInjector.Integration.ServiceCollection" Version="5.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Stashbox" Version="5.5.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/MediatR/MediatR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MediatR.Contracts" Version="[2.0.1, 3.0.0)" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0-rc.1.23419.4" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0-rc.1.23419.4" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="All" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/MediatR/Registration/ServiceRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public static void AddRequiredServices(IServiceCollection services, MediatRServi
private static void RegisterBehaviorIfImplementationsExist(IServiceCollection services, Type behaviorType, Type subBehaviorType)
{
var hasAnyRegistrationsOfSubBehaviorType = services
.Where(service => !service.IsKeyedService)
.Select(service => service.ImplementationType)
.OfType<Type>()
.SelectMany(type => type.GetInterfaces())
Expand Down
2 changes: 1 addition & 1 deletion test/MediatR.Benchmarks/MediatR.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MediatR\MediatR.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion test/MediatR.Tests/MediatR.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ShouldResolveInternalHandler()
[Fact]
public void ShouldResolveNotificationHandlers()
{
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(3);
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(4);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void ShouldResolveRequestHandler()
[Fact]
public void ShouldResolveNotificationHandlers()
{
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(3);
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(4);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public DuplicateAssemblyResolutionTests()
[Fact]
public void ShouldResolveNotificationHandlersOnlyOnce()
{
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(3);
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(4);
}
}
6 changes: 6 additions & 0 deletions test/MediatR.Tests/MicrosoftExtensionsDI/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public Task Handle(Pinged notification, CancellationToken cancellationToken)
}
}

public class PingedAlsoOpenHandler<TNotification> : INotificationHandler<TNotification>
where TNotification : Pinged
{
public Task Handle(TNotification notification, CancellationToken cancellationToken) => Task.CompletedTask;
}

public class Logger
{
public IList<string> Messages { get; } = new List<string>();
Expand Down
96 changes: 96 additions & 0 deletions test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Xunit.Abstractions;

namespace MediatR.Extensions.Microsoft.DependencyInjection.Tests;

Expand Down Expand Up @@ -843,4 +845,98 @@ public void Should_handle_open_behaviors_registration_from_a_single_type()
services.BuildServiceProvider();
});
}

public sealed record FooRequest : IRequest;

public interface IBlogger<T>
{
IList<string> Messages { get; }
}

public class Blogger<T> : IBlogger<T>
{
private readonly Logger _logger;

public Blogger(Logger logger)
{
_logger = logger;
}

public IList<string> Messages => _logger.Messages;
}

public sealed class FooRequestHandler : IRequestHandler<FooRequest> {
public FooRequestHandler(IBlogger<FooRequestHandler> logger)
{
this.logger = logger;
}

readonly IBlogger<FooRequestHandler> logger;

public Task Handle(FooRequest request, CancellationToken cancellationToken) {
logger.Messages.Add("Invoked Handler");
return Task.CompletedTask;
}
}

sealed class ClosedBehavior : IPipelineBehavior<FooRequest, Unit> {
public ClosedBehavior(IBlogger<ClosedBehavior> logger)
{
this.logger = logger;
}

readonly IBlogger<ClosedBehavior> logger;

public Task<Unit> Handle(FooRequest request, RequestHandlerDelegate<Unit> next, CancellationToken cancellationToken) {
logger.Messages.Add("Invoked Closed Behavior");
return next();
}
}

sealed class Open2Behavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull {
public Open2Behavior(IBlogger<Open2Behavior<TRequest, TResponse>> logger) {
this.logger = logger;
}

readonly IBlogger<Open2Behavior<TRequest, TResponse>> logger;

public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) {
logger.Messages.Add("Invoked Open Behavior");
return next();
}
}
[Fact]
public async Task Should_register_correctly()
{
var services = new ServiceCollection();
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<FooRequest>();
cfg.AddBehavior<ClosedBehavior>();
cfg.AddOpenBehavior(typeof(Open2Behavior<,>));
});
var logger = new Logger();
services.AddSingleton(logger);
services.AddSingleton(new MediatR.Tests.PipelineTests.Logger());
services.AddSingleton(new MediatR.Tests.StreamPipelineTests.Logger());
services.AddSingleton(new MediatR.Tests.SendTests.Dependency());
services.AddSingleton<System.IO.TextWriter>(new System.IO.StringWriter());
services.AddTransient(typeof(IBlogger<>), typeof(Blogger<>));
var provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true
});

var mediator = provider.GetRequiredService<IMediator>();
var request = new FooRequest();
await mediator.Send(request);

logger.Messages.ShouldBe(new []
{
"Invoked Closed Behavior",
"Invoked Open Behavior",
"Invoked Handler",
});
}
}
16 changes: 15 additions & 1 deletion test/MediatR.Tests/MicrosoftExtensionsDI/TypeResolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void ShouldResolveVoidRequestHandler()
[Fact]
public void ShouldResolveNotificationHandlers()
{
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(3);
_provider.GetServices<INotificationHandler<Pinged>>().Count().ShouldBe(4);
}

[Fact]
Expand All @@ -77,4 +77,18 @@ public void ShouldResolveIgnoreSecondDuplicateHandler()
{
_provider.GetServices<IRequestHandler<DuplicateTest, string>>().Count().ShouldBe(1);
}

[Fact]
public void ShouldHandleKeyedServices()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(new Logger());
services.AddKeyedSingleton<string>("Foo", "Foo");
services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining(typeof(Ping)));
var serviceProvider = services.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

mediator.ShouldNotBeNull();
}
}