Skip to content

Commit

Permalink
test: add register checks for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jul 14, 2024
1 parent 62364bc commit fcc0aa8
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/ApplicationBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Prometheus.Client.Collectors;
using Xunit;

namespace Prometheus.Client.HttpRequestDurations.Tests;

public class ApplicationBuilderExtensionsTests
{
private readonly ServiceCollection _services = new();

[Fact]
public void AppBuilderIsNull_Throws_ArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ((ApplicationBuilder)null).UsePrometheusRequestDurations());
}

[Fact]
public void TargetIsType_HttpRequestDurationsMiddleware()
{
var app = new ApplicationBuilder(_services.BuildServiceProvider());
_services.AddSingleton<ICollectorRegistry, CollectorRegistry>();
app.UsePrometheusRequestDurations();

Assert.IsType<HttpRequestDurationsMiddleware>(app.Build().Target);
}

[Fact]
public void With_DefaultCollectorRegistry()
{
var app = new ApplicationBuilder(_services.BuildServiceProvider());
app.UsePrometheusRequestDurations();
app.Build();

Assert.True(Metrics.DefaultCollectorRegistry.TryGet(HttpRequestDurationsOptions.DefaultMetricName, out var defaultCollector));

// Cleanup
Metrics.DefaultCollectorRegistry?.Remove(defaultCollector);
}

[Fact]
public void With_DICollecorRegistry()
{
var registry = new CollectorRegistry();
_services.AddSingleton<ICollectorRegistry>(registry);
var app = new ApplicationBuilder(_services.BuildServiceProvider());
app.UsePrometheusRequestDurations();
app.Build();

Assert.True(registry.TryGet(HttpRequestDurationsOptions.DefaultMetricName, out _));

Assert.False(Metrics.DefaultCollectorRegistry.TryGet(HttpRequestDurationsOptions.DefaultMetricName, out _));
}

[Fact]
public void With_CustomCollecorRegistry()
{
var registry = new CollectorRegistry();

var app = new ApplicationBuilder(_services.BuildServiceProvider());
app.UsePrometheusRequestDurations(q => q.CollectorRegistry = registry);
app.Build();

Assert.True(registry.TryGet(HttpRequestDurationsOptions.DefaultMetricName, out _));

Assert.False(Metrics.DefaultCollectorRegistry.TryGet(HttpRequestDurationsOptions.DefaultMetricName, out _));
}
}

0 comments on commit fcc0aa8

Please sign in to comment.