diff --git a/X10D.Tests/X10D.Tests.csproj b/X10D.Tests/X10D.Tests.csproj index c3d7b6b..d6512d8 100644 --- a/X10D.Tests/X10D.Tests.csproj +++ b/X10D.Tests/X10D.Tests.csproj @@ -14,6 +14,7 @@ + @@ -25,6 +26,7 @@ + diff --git a/X10D.Tests/src/Hosting/ServiceCollectionTests.cs b/X10D.Tests/src/Hosting/ServiceCollectionTests.cs new file mode 100644 index 0000000..3713763 --- /dev/null +++ b/X10D.Tests/src/Hosting/ServiceCollectionTests.cs @@ -0,0 +1,60 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using X10D.Hosting.DependencyInjection; + +namespace X10D.Tests.Hosting; + +[TestClass] +public class ServiceCollectionTests +{ + [TestMethod] + public void AddHostedSingleton_ShouldRegisterServiceAsSingletonAndAsHostedService() + { + var services = new ServiceCollection(); + + services.AddHostedSingleton(); + + var serviceProvider = services.BuildServiceProvider(); + var service = serviceProvider.GetService(); + var hostedService = serviceProvider.GetService(); + + Assert.IsNotNull(service); + Assert.IsNotNull(hostedService); + Assert.IsInstanceOfType(service, typeof(TestService)); + Assert.IsInstanceOfType(hostedService, typeof(TestService)); + Assert.AreSame(service, hostedService); + } + + [TestMethod] + public void AddHostedSingleton_ShouldRegisterServiceTypeAsSingletonAndAsHostedService() + { + var services = new ServiceCollection(); + + services.AddHostedSingleton(typeof(TestService)); + + var serviceProvider = services.BuildServiceProvider(); + var service = serviceProvider.GetService(); + var hostedService = serviceProvider.GetService(); + + Assert.IsNotNull(service); + Assert.IsNotNull(hostedService); + Assert.IsInstanceOfType(service, typeof(TestService)); + Assert.IsInstanceOfType(hostedService, typeof(TestService)); + Assert.AreSame(service, hostedService); + } + + private sealed class TestService : IHostedService + { + public Task StartAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +}