Skip to content

Commit

Permalink
use Testcontainers for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Dec 10, 2024
1 parent d4b17d7 commit 45f424d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
23 changes: 23 additions & 0 deletions tests/MetricPushServerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;
using NSubstitute;
using Xunit;

namespace Prometheus.Client.MetricPusher.Tests;

public class MetricPushServerTests
{
[Fact]
public async Task PushContinuesOnError()
{
var pusher = Substitute.For<IMetricPusher>();
pusher.PushAsync().Returns(Task.FromException(new Exception("Push error")));

var worker = new MetricPushServer(pusher, TimeSpan.FromSeconds(0.05));
worker.Start();
await Task.Delay(150);

await pusher.Received(3).PushAsync();
worker.Stop();
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using Prometheus.Client.Collectors;
using Xunit;
using Xunit.Abstractions;

namespace Prometheus.Client.MetricPusher.Tests.Integration;
// Need Environment
// https://github.com/prometheus/pushgateway
namespace Prometheus.Client.MetricPusher.Tests;

public class MetricPusherTests
public class MetricPusherTests(PushGatewayFixture fixture, ITestOutputHelper output) : IClassFixture<PushGatewayFixture>
{
private readonly ITestOutputHelper _output;
private readonly IMetricFactory _metricFactory;

public MetricPusherTests(ITestOutputHelper output)
{
_output = output;
_metricFactory = new MetricFactory(new CollectorRegistry());
}
private readonly string _endpoint = fixture.GetEndpoint();
private readonly IMetricFactory _metricFactory = new MetricFactory(new CollectorRegistry());

[Fact]
public async Task Simple_Push()
public async Task Push()
{
var counter = _metricFactory.CreateCounter("test_c12", "help");
counter.Inc();

var pusher = new MetricPusher(new MetricPusherOptions { Endpoint = "http://localhost:9091", Job = "pushgateway-test", Instance = "instance" });
var pusher = new MetricPusher(new MetricPusherOptions { Endpoint = _endpoint, Job = "pushgateway-test", Instance = "instance" });
await pusher.PushAsync();
}

[Fact]
public async Task Auth_Puth()
public async Task PushWithAdditionalHeaders()
{
var counter = _metricFactory.CreateCounter("test_", "help");
counter.Inc();

const string accessToken = "";
var pusher = new MetricPusher(new MetricPusherOptions
{
Endpoint = "http://localhost:9091",
Endpoint = _endpoint,
Job = "pushgateway-test",
Instance = "instance",
AdditionalHeaders = new Dictionary<string, string> { { "Authorization", "Bearer " + accessToken } }
Expand All @@ -49,18 +40,18 @@ public async Task Auth_Puth()
}

[Fact]
public async Task Worker_10Step()
public async Task Worker10Steps()
{
var counter = _metricFactory.CreateCounter("worker_counter1", "help");
var pusher = new MetricPusher(new MetricPusherOptions { Endpoint = "http://localhost:9091", Job = "pushgateway-testworker" });
var pusher = new MetricPusher(new MetricPusherOptions { Endpoint = _endpoint, Job = "pushgateway-testworker" });

var worker = new MetricPushServer(pusher);
worker.Start();

for (int i = 0; i < 10; i++)
{
counter.Inc();
_output.WriteLine($"Step: {i}, IsRunning: {worker.IsRunning}");
output.WriteLine($"Step: {i}, IsRunning: {worker.IsRunning}");

switch (i)
{
Expand All @@ -72,23 +63,9 @@ public async Task Worker_10Step()
break;
}

await Task.Delay(2000);
await Task.Delay(100);
}

worker.Stop();
}

[Fact]
public async Task TestPushContinuesOnError()
{
var pusher = Substitute.For<IMetricPusher>();
pusher.PushAsync().Returns(Task.FromException(new Exception("Push error")));

var worker = new MetricPushServer(pusher, TimeSpan.FromSeconds(0.05));
worker.Start();
await Task.Delay(150);

await pusher.Received(3).PushAsync();
worker.Stop();
}
}
1 change: 1 addition & 0 deletions tests/Prometheus.Client.MetricPusher.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="Testcontainers" Version="4.1.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
29 changes: 29 additions & 0 deletions tests/PushGatewayFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using Xunit;

namespace Prometheus.Client.MetricPusher.Tests;

public class PushGatewayFixture : IAsyncLifetime
{
private readonly IContainer _container;

public PushGatewayFixture()
{
_container = new ContainerBuilder()
.WithImage("prom/pushgateway")
.WithPortBinding(9091, true)
//.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(9091)))
.Build();
}

public string GetEndpoint() => $"{Uri.UriSchemeHttp}://{_container.Hostname}:{_container.GetMappedPublicPort(9091)}";

public Task InitializeAsync()
=> _container.StartAsync();

public Task DisposeAsync()
=> _container.DisposeAsync().AsTask();
}

0 comments on commit 45f424d

Please sign in to comment.