Skip to content

Commit

Permalink
add asserts to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Dec 10, 2024
1 parent 6e3815c commit cf973d8
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions tests/MetricPusherTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Prometheus.Client.Collectors;
using Xunit;
Expand All @@ -12,19 +14,33 @@ public class MetricPusherTests(PushGatewayFixture fixture, ITestOutputHelper out
private readonly IMetricFactory _metricFactory = new MetricFactory(new CollectorRegistry());

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

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

var ex = await Record.ExceptionAsync(() => pusher.PushAsync());
Assert.Null(ex);
}

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

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

var ex = await Record.ExceptionAsync(() => pusher.PushAsync());
Assert.IsType<HttpRequestException>(ex);
}

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

const string accessToken = "";
Expand All @@ -35,22 +51,42 @@ public async Task PushWithAdditionalHeaders()
Instance = "instance",
AdditionalHeaders = new Dictionary<string, string> { { "Authorization", "Bearer " + accessToken } }
});
await pusher.PushAsync();

var ex = await Record.ExceptionAsync(() => pusher.PushAsync());
Assert.Null(ex);
}

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

var worker = new MetricPushServer(pusher);

var resultBuilder = new StringBuilder();
const string expectedResult = """
Step: 0, IsRunning: True
Step: 1, IsRunning: True
Step: 2, IsRunning: True
Step: 3, IsRunning: True
Step: 4, IsRunning: True
Step: 5, IsRunning: True
Step: 6, IsRunning: False
Step: 7, IsRunning: False
Step: 8, IsRunning: False
Step: 9, IsRunning: True
""";

worker.Start();

for (int i = 0; i < 10; i++)
{
var info = $"Step: {i}, IsRunning: {worker.IsRunning}";
resultBuilder.AppendLine(info);
output.WriteLine(info);

counter.Inc();
output.WriteLine($"Step: {i}, IsRunning: {worker.IsRunning}");

switch (i)
{
Expand All @@ -65,6 +101,8 @@ public async Task Worker10Steps()
await Task.Delay(100);
}

Assert.Equal(expectedResult, resultBuilder.ToString());

worker.Stop();
}
}

0 comments on commit cf973d8

Please sign in to comment.