-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Keyed DI services in .NET 8 (#331)
* Support Keyed DI services in .NET 8 #315 * add NATS.Client.Hosting.Tests * StyleCop suppression for conditional build
- Loading branch information
1 parent
62a6c31
commit dc6301f
Showing
5 changed files
with
143 additions
and
15 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
34 changes: 34 additions & 0 deletions
34
tests/NATS.Client.Hosting.Tests/NATS.Client.Hosting.Tests.csproj
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,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<NoWarn>$(NoWarn);CS8002</NoWarn> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/> | ||
<PackageReference Include="xunit" Version="2.4.2"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
<Using Include="Xunit.Abstractions"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\NATS.Client.Hosting\NATS.Client.Hosting.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
58 changes: 58 additions & 0 deletions
58
tests/NATS.Client.Hosting.Tests/NatsHostingExtensionsTests.cs
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,58 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using NATS.Client.Core; | ||
|
||
namespace NATS.Client.Hosting.Tests; | ||
|
||
public class NatsHostingExtensionsTests | ||
{ | ||
[Fact] | ||
public void AddNats_RegistersNatsConnectionAsSingleton_WhenPoolSizeIsOne() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<ILoggerFactory, NullLoggerFactory>(); | ||
|
||
services.AddNats(poolSize: 1); | ||
var provider = services.BuildServiceProvider(); | ||
|
||
var natsConnection1 = provider.GetRequiredService<INatsConnection>(); | ||
var natsConnection2 = provider.GetRequiredService<INatsConnection>(); | ||
|
||
Assert.NotNull(natsConnection1); | ||
Assert.Same(natsConnection1, natsConnection2); // Singleton should return the same instance | ||
} | ||
|
||
[Fact] | ||
public void AddNats_RegistersNatsConnectionAsTransient_WhenPoolSizeIsGreaterThanOne() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<ILoggerFactory, NullLoggerFactory>(); | ||
|
||
services.AddNats(poolSize: 2); | ||
var provider = services.BuildServiceProvider(); | ||
|
||
var natsConnection1 = provider.GetRequiredService<INatsConnection>(); | ||
var natsConnection2 = provider.GetRequiredService<INatsConnection>(); | ||
|
||
Assert.NotNull(natsConnection1); | ||
Assert.NotSame(natsConnection1, natsConnection2); // Transient should return different instances | ||
} | ||
|
||
#if NET8_0_OR_GREATER | ||
[Fact] | ||
public void AddNats_RegistersKeyedNatsConnection_WhenKeyIsProvided() | ||
{ | ||
var key = "TestKey"; | ||
|
||
var services = new ServiceCollection(); | ||
services.AddSingleton<ILoggerFactory, NullLoggerFactory>(); | ||
|
||
services.AddNats(poolSize: 1, key: key); | ||
var provider = services.BuildServiceProvider(); | ||
|
||
var natsConnection = provider.GetKeyedService<INatsConnection>(key); | ||
Assert.NotNull(natsConnection); | ||
} | ||
#endif | ||
} |