diff --git a/.gitignore b/.gitignore index 7921d33..f614603 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ -*.user -*.opencover.xml -*.orig -*.nupkg -*.snupkg -.idea -bin -obj -BenchmarkDotNet.Artifacts +# Rider +.idea/ + +# Visual Studio +.vs/ + +# Build results +[Bb]in/ +[Oo]bj/ + diff --git a/Prometheus.Client.MetricPusher.HostedService.sln b/Prometheus.Client.MetricPusher.HostedService.sln index 38629b5..a153e82 100644 --- a/Prometheus.Client.MetricPusher.HostedService.sln +++ b/Prometheus.Client.MetricPusher.HostedService.sln @@ -1,6 +1,11 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prometheus.Client.MetricPusher.HostedService", "src\Prometheus.Client.MetricPusher.HostedService.csproj", "{C77EB3BB-08D7-4B18-9833-6D5486AA42C8}" +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34003.232 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prometheus.Client.MetricPusher.HostedService", "src\Prometheus.Client.MetricPusher.HostedService\Prometheus.Client.MetricPusher.HostedService.csproj", "{CD73BF99-211A-496F-983B-A55A22DE4FEE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prometheus.Client.MetricPusher.HostedService.Tests", "tests\Prometheus.Client.MetricPusher.HostedService.Tests\Prometheus.Client.MetricPusher.HostedService.Tests.csproj", "{2CC30054-6E22-4112-9DB1-7FFA154B92FD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -8,11 +13,19 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C77EB3BB-08D7-4B18-9833-6D5486AA42C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C77EB3BB-08D7-4B18-9833-6D5486AA42C8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C77EB3BB-08D7-4B18-9833-6D5486AA42C8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C77EB3BB-08D7-4B18-9833-6D5486AA42C8}.Release|Any CPU.Build.0 = Release|Any CPU + {CD73BF99-211A-496F-983B-A55A22DE4FEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD73BF99-211A-496F-983B-A55A22DE4FEE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD73BF99-211A-496F-983B-A55A22DE4FEE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD73BF99-211A-496F-983B-A55A22DE4FEE}.Release|Any CPU.Build.0 = Release|Any CPU + {2CC30054-6E22-4112-9DB1-7FFA154B92FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2CC30054-6E22-4112-9DB1-7FFA154B92FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2CC30054-6E22-4112-9DB1-7FFA154B92FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2CC30054-6E22-4112-9DB1-7FFA154B92FD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection - GlobalSection(NestedProjects) = preSolution + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2A144989-FB17-400D-A3E8-78C7534AF12F} EndGlobalSection EndGlobal diff --git a/src/MetricPusherService.cs b/src/Prometheus.Client.MetricPusher.HostedService/MetricPusherService.cs similarity index 100% rename from src/MetricPusherService.cs rename to src/Prometheus.Client.MetricPusher.HostedService/MetricPusherService.cs diff --git a/src/Prometheus.Client.MetricPusher.HostedService.csproj b/src/Prometheus.Client.MetricPusher.HostedService/Prometheus.Client.MetricPusher.HostedService.csproj similarity index 100% rename from src/Prometheus.Client.MetricPusher.HostedService.csproj rename to src/Prometheus.Client.MetricPusher.HostedService/Prometheus.Client.MetricPusher.HostedService.csproj diff --git a/src/ServiceCollectionExtensions.cs b/src/Prometheus.Client.MetricPusher.HostedService/ServiceCollectionExtensions.cs similarity index 100% rename from src/ServiceCollectionExtensions.cs rename to src/Prometheus.Client.MetricPusher.HostedService/ServiceCollectionExtensions.cs diff --git a/tests/Prometheus.Client.MetricPusher.HostedService.Tests/GlobalUsings.cs b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/GlobalUsings.cs new file mode 100644 index 0000000..3c3d664 --- /dev/null +++ b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/GlobalUsings.cs @@ -0,0 +1,4 @@ +global using Xunit; +global using FluentAssertions; +global using NSubstitute; +global using System; diff --git a/tests/Prometheus.Client.MetricPusher.HostedService.Tests/MetricPusherServiceTests.cs b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/MetricPusherServiceTests.cs new file mode 100644 index 0000000..974008d --- /dev/null +++ b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/MetricPusherServiceTests.cs @@ -0,0 +1,22 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace Prometheus.Client.MetricPusher.HostedService.Tests +{ + public class MetricPusherServiceTests + { + [Fact] + public async Task WithGivenInterval_PushMetricPeriodically() + { + var metricPusherMock = Substitute.For(); + var metricPusherService = new MetricPusherService(metricPusherMock, TimeSpan.FromSeconds(1)); + var canellationToken = Arg.Any(); + + await metricPusherService.StartAsync(canellationToken); + await Task.Delay(TimeSpan.FromSeconds(1), canellationToken); + await metricPusherService.StopAsync(canellationToken); + + await metricPusherMock.Received(3).PushAsync(); + } + } +} diff --git a/tests/Prometheus.Client.MetricPusher.HostedService.Tests/Prometheus.Client.MetricPusher.HostedService.Tests.csproj b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/Prometheus.Client.MetricPusher.HostedService.Tests.csproj new file mode 100644 index 0000000..953757a --- /dev/null +++ b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/Prometheus.Client.MetricPusher.HostedService.Tests.csproj @@ -0,0 +1,50 @@ + + + + netcoreapp3.1;net6.0;net7.0 + false + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + + + diff --git a/tests/Prometheus.Client.MetricPusher.HostedService.Tests/ServiceCollecttionExtensionsTests.cs b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/ServiceCollecttionExtensionsTests.cs new file mode 100644 index 0000000..7e6f8e1 --- /dev/null +++ b/tests/Prometheus.Client.MetricPusher.HostedService.Tests/ServiceCollecttionExtensionsTests.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Prometheus.Client.MetricPusher.HostedService.Tests +{ + public class ServiceCollecttionExtensionsTests + { + [Fact] + public void AddMetricPusherService_WithNullMetricPusher_ThrowsArgumentNullException() + { + var servicesCollection = Substitute.For(); + + Action act = () => servicesCollection.AddMetricPusherService(null, TimeSpan.FromSeconds(1)); + + act.Should().Throw(); + } + + [Fact] + public void AddMetricPusherService_WithZeroTimeInterval_ThrowsArgumentOutOfRangeException() + { + var servicesCollection = Substitute.For(); + var metricPusher = Substitute.For(); + + Action act = () => servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.Zero); + + act.Should().Throw(); + } + + [Fact] + public void AddMetricPusherService_WithValidParameterValues_AddsMetricPusherServiceInServiceCollection() + { + var servicesCollection = new ServiceCollection(); + var metricPusher = Substitute.For(); + + servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.FromSeconds(1)); + var provider = servicesCollection.BuildServiceProvider(); + var service = provider.GetRequiredService(); + + service.Should().BeOfType(); + } + } +}