Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 27 additions & 34 deletions tests/MetricServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@

namespace Prometheus.Client.MetricServer.Tests;

public class MetricServerTests
public class MetricServerTests(PortFixture fixture, ITestOutputHelper testOutputHelper) : IClassFixture<PortFixture>
{
private IMetricServer _metricServer;
private readonly ITestOutputHelper _testOutputHelper;
private const int _port = 9091;

public MetricServerTests(ITestOutputHelper testOutputHelper)
private MetricServer _metricServer = new(new MetricServerOptions
{
_testOutputHelper = testOutputHelper;
_metricServer = new MetricServer(new MetricServerOptions
{
Port = _port,
CollectorRegistryInstance = new CollectorRegistry()
});
}
Port = fixture.Port,
CollectorRegistryInstance = new CollectorRegistry()
});

[Fact]
public void Null_Options_Throws_ArgumentNullException()
Expand Down Expand Up @@ -91,14 +83,14 @@ public async Task BaseMapPath_FindMetrics()
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
counter.Inc();
using var httpClient = new HttpClient();
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}{Defaults.MapPath}");
Assert.False(string.IsNullOrEmpty(response));
Assert.Contains("process_private_memory_bytes", response);
Assert.Contains("dotnet_total_memory_bytes", response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -110,21 +102,21 @@ public async Task BaseMapPath_FindMetrics()
[Fact]
public async Task SetMapPath_FindMetricsWithEndSlash()
{
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = "/test" });
_metricServer = new MetricServer(new MetricServerOptions { Port = fixture.Port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = "/test" });
try
{
_metricServer.Start();
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
counter.Inc();
using var httpClient = new HttpClient();
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/test/");
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}/test/");
Assert.False(string.IsNullOrEmpty(response));
Assert.Contains("process_private_memory_bytes", response);
Assert.Contains("dotnet_total_memory_bytes", response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -140,21 +132,21 @@ public async Task SetMapPath_FindMetricsWithEndSlash()
[InlineData("/metrics965")]
public async Task SetMapPath_FindMetrics(string mapPath)
{
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = mapPath });
_metricServer = new MetricServer(new MetricServerOptions { Port = fixture.Port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = mapPath });
try
{
_metricServer.Start();
using var httpClient = new HttpClient();
if (!mapPath.StartsWith("/"))
mapPath = "/" + mapPath;
string response = await httpClient.GetStringAsync($"http://localhost:{_port}" + mapPath);
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}" + mapPath);
Assert.False(string.IsNullOrEmpty(response));
Assert.Contains("process_private_memory_bytes", response);
Assert.Contains("dotnet_total_memory_bytes", response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -168,7 +160,7 @@ public async Task CustomCounter_FindMetric()
{
var registry = new CollectorRegistry();
var factory = new MetricFactory(registry);
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry });
_metricServer = new MetricServer(new MetricServerOptions { Port = fixture.Port, CollectorRegistryInstance = registry });

try
{
Expand All @@ -179,12 +171,12 @@ public async Task CustomCounter_FindMetric()
counter.Inc();

using var httpClient = new HttpClient();
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}{Defaults.MapPath}");
Assert.Contains(metricName, response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -200,15 +192,15 @@ public async Task AddLegacyMetrics_False_FindMetrics()
{
_metricServer.Start();
using var httpClient = new HttpClient();
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}{Defaults.MapPath}");
Assert.Contains("process_private_memory_bytes", response);
Assert.Contains("dotnet_total_memory_bytes", response);
Assert.DoesNotContain("process_private_bytes", response);
Assert.DoesNotContain("dotnet_totalmemory", response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -220,21 +212,22 @@ public async Task AddLegacyMetrics_False_FindMetrics()
[Fact]
public async Task AddLegacyMetrics_True_FindMetrics()
{
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), AddLegacyMetrics = true });
_metricServer = new MetricServer(new MetricServerOptions
{ Port = fixture.Port, CollectorRegistryInstance = new CollectorRegistry(), AddLegacyMetrics = true });

try
{
_metricServer.Start();
using var httpClient = new HttpClient();
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}{Defaults.MapPath}");
Assert.Contains("process_private_memory_bytes", response);
Assert.Contains("dotnet_total_memory_bytes", response);
Assert.Contains("process_private_bytes", response);
Assert.Contains("dotnet_totalmemory", response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -253,12 +246,12 @@ public async Task WrongUrl_NotFound()
counter.Inc();
using var httpClient = new HttpClient();

var response = await httpClient.GetAsync($"http://localhost:{_port}/not-found");
var response = await httpClient.GetAsync($"http://localhost:{fixture.Port}/not-found");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand All @@ -272,7 +265,7 @@ public async Task CustormEncoding_FindHelp()
{
var registry = new CollectorRegistry();
var factory = new MetricFactory(registry);
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry, ResponseEncoding = Encoding.UTF8 });
_metricServer = new MetricServer(new MetricServerOptions { Port = fixture.Port, CollectorRegistryInstance = registry, ResponseEncoding = Encoding.UTF8 });

try
{
Expand All @@ -283,12 +276,12 @@ public async Task CustormEncoding_FindHelp()
counter.Inc();

using var httpClient = new HttpClient();
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
string response = await httpClient.GetStringAsync($"http://localhost:{fixture.Port}{Defaults.MapPath}");
Assert.Contains(help, response);
}
catch (Exception ex)
{
_testOutputHelper.WriteLine(ex.ToString());
testOutputHelper.WriteLine(ex.ToString());
throw;
}
finally
Expand Down
25 changes: 25 additions & 0 deletions tests/PortFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Net;
using System.Net.Sockets;

namespace Prometheus.Client.MetricServer.Tests;

public class PortFixture
{
public int Port { get; } = FindAvailablePort();

private static int FindAvailablePort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);

try
{
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
return port;
}
finally
{
listener.Stop();
}
}
}
Loading