Skip to content

Commit

Permalink
Merge pull request #5 from manan-habib/main
Browse files Browse the repository at this point in the history
Added unit tests and restructured folders (#1)
  • Loading branch information
phnx47 authored Aug 16, 2023
2 parents b62ca0f + 89552b0 commit 3a4302d
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 16 deletions.
19 changes: 10 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/

27 changes: 20 additions & 7 deletions Prometheus.Client.MetricPusher.HostedService.sln
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@


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
Debug|Any CPU = Debug|Any CPU
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
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using Xunit;
global using FluentAssertions;
global using NSubstitute;
global using System;
Original file line number Diff line number Diff line change
@@ -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<IMetricPusher>();
var metricPusherService = new MetricPusherService(metricPusherMock, TimeSpan.FromSeconds(1));
var canellationToken = Arg.Any<CancellationToken>();

await metricPusherService.StartAsync(canellationToken);
await Task.Delay(TimeSpan.FromSeconds(1), canellationToken);
await metricPusherService.StopAsync(canellationToken);

await metricPusherMock.Received(3).PushAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0'">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.32" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Prometheus.Client.MetricPusher.HostedService\Prometheus.Client.MetricPusher.HostedService.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<IServiceCollection>();

Action act = () => servicesCollection.AddMetricPusherService(null, TimeSpan.FromSeconds(1));

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AddMetricPusherService_WithZeroTimeInterval_ThrowsArgumentOutOfRangeException()
{
var servicesCollection = Substitute.For<IServiceCollection>();
var metricPusher = Substitute.For<IMetricPusher>();

Action act = () => servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.Zero);

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void AddMetricPusherService_WithValidParameterValues_AddsMetricPusherServiceInServiceCollection()
{
var servicesCollection = new ServiceCollection();
var metricPusher = Substitute.For<IMetricPusher>();

servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.FromSeconds(1));
var provider = servicesCollection.BuildServiceProvider();
var service = provider.GetRequiredService<IHostedService>();

service.Should().BeOfType<MetricPusherService>();
}
}
}

0 comments on commit 3a4302d

Please sign in to comment.