generated from prom-client-net/prom-client-tmpl
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from prom-client-net/feat/default-interval
feat: add default interval
- Loading branch information
Showing
8 changed files
with
111 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace Prometheus.Client.MetricPusher.HostedService; | ||
|
||
internal static class Defaults | ||
{ | ||
internal static TimeSpan Interval = TimeSpan.FromMilliseconds(1000); | ||
} |
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
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 |
---|---|---|
@@ -1,22 +1,45 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Prometheus.Client.MetricPusher.HostedService.Tests | ||
namespace Prometheus.Client.MetricPusher.HostedService.Tests; | ||
|
||
public class MetricPusherServiceTests | ||
{ | ||
public class MetricPusherServiceTests | ||
[Fact] | ||
public async Task WithDefaultInterval_PushMetricPeriodically() | ||
{ | ||
var metricPusherMock = Substitute.For<IMetricPusher>(); | ||
var metricPusherService = new MetricPusherService(metricPusherMock); | ||
var canellationToken = Arg.Any<CancellationToken>(); | ||
|
||
await metricPusherService.StartAsync(canellationToken); | ||
await Task.Delay(GetDelay(1), canellationToken); | ||
await metricPusherService.StopAsync(canellationToken); | ||
|
||
await metricPusherMock.Received(3).PushAsync(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(2)] | ||
[InlineData(3)] | ||
[InlineData(5)] | ||
public async Task WithGivenInterval_PushMetricPeriodically(int seconds) | ||
{ | ||
var metricPusherMock = Substitute.For<IMetricPusher>(); | ||
var metricPusherService = new MetricPusherService(metricPusherMock, TimeSpan.FromSeconds(seconds)); | ||
var canellationToken = Arg.Any<CancellationToken>(); | ||
|
||
await metricPusherService.StartAsync(canellationToken); | ||
await Task.Delay(GetDelay(seconds), canellationToken); | ||
await metricPusherService.StopAsync(canellationToken); | ||
|
||
await metricPusherMock.Received(3).PushAsync(); | ||
} | ||
|
||
private static TimeSpan GetDelay(int seconds) | ||
{ | ||
[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(); | ||
} | ||
var milliseconds = seconds * 1000 + 100; | ||
return TimeSpan.FromMilliseconds(milliseconds); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,42 +1,54 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Prometheus.Client.MetricPusher.HostedService.Tests | ||
namespace Prometheus.Client.MetricPusher.HostedService.Tests; | ||
|
||
public class ServiceCollecttionExtensionsTests | ||
{ | ||
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() | ||
{ | ||
[Fact] | ||
public void AddMetricPusherService_WithNullMetricPusher_ThrowsArgumentNullException() | ||
{ | ||
var servicesCollection = Substitute.For<IServiceCollection>(); | ||
var servicesCollection = Substitute.For<IServiceCollection>(); | ||
var metricPusher = Substitute.For<IMetricPusher>(); | ||
|
||
Action act = () => servicesCollection.AddMetricPusherService(null, TimeSpan.FromSeconds(1)); | ||
Action act = () => servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.Zero); | ||
|
||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
act.Should().Throw<ArgumentOutOfRangeException>(); | ||
} | ||
|
||
[Fact] | ||
public void AddMetricPusherService_WithZeroTimeInterval_ThrowsArgumentOutOfRangeException() | ||
{ | ||
var servicesCollection = Substitute.For<IServiceCollection>(); | ||
var metricPusher = Substitute.For<IMetricPusher>(); | ||
[Fact] | ||
public void AddMetricPusherService_WithDefaultInterval_AddsMetricPusherServiceInServiceCollection() | ||
{ | ||
var servicesCollection = new ServiceCollection(); | ||
var metricPusher = Substitute.For<IMetricPusher>(); | ||
|
||
Action act = () => servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.Zero); | ||
servicesCollection.AddMetricPusherService(metricPusher); | ||
var provider = servicesCollection.BuildServiceProvider(); | ||
var service = provider.GetRequiredService<IHostedService>(); | ||
|
||
act.Should().Throw<ArgumentOutOfRangeException>(); | ||
} | ||
service.Should().BeOfType<MetricPusherService>(); | ||
} | ||
|
||
[Fact] | ||
public void AddMetricPusherService_WithValidParameterValues_AddsMetricPusherServiceInServiceCollection() | ||
{ | ||
var servicesCollection = new ServiceCollection(); | ||
var metricPusher = Substitute.For<IMetricPusher>(); | ||
[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>(); | ||
servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.FromSeconds(1)); | ||
var provider = servicesCollection.BuildServiceProvider(); | ||
var service = provider.GetRequiredService<IHostedService>(); | ||
|
||
service.Should().BeOfType<MetricPusherService>(); | ||
} | ||
service.Should().BeOfType<MetricPusherService>(); | ||
} | ||
} |