Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksii-valuiskyi committed Dec 12, 2020
1 parent 054a91b commit a0c895e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 8 deletions.
111 changes: 111 additions & 0 deletions prometheus-net.Contrib.Tests/CountersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using Prometheus.Contrib.EventListeners.Counters;
using Xunit;

namespace prometheus_net.Contrib.Tests
{
public class CountersTests
{
[Fact]
public void MeanCounter_WhenMeanIsGreaterThanZero_ThenUseMeanForValue()
{
var meanCounter = new MeanCounter("test1", "test2", "test3");

var eventData = new Dictionary<string, object>
{
["Name"] = "cpu-usage",
["DisplayName"] = "CPU Usage",
["Mean"] = 15d,
["StandardDeviation"] = 0,
["Count"] = 1,
["Min"] = 0,
["Max"] = 0,
["InvervalSec"] = 9.9996233,
["Series"] = "Interval=10000",
["CounterType"] = "Mean",
["Metadata"] = "",
["DisplayUnits"] = "%",
};

meanCounter.TryReadEventCounterData(eventData);

Assert.Equal(15, meanCounter.Metric.Value);
}

[Fact]
public void MeanCounter_WhenMeanIsZero_ThenUseCountForValue()
{
var meanCounter = new MeanCounter("test1", "test2", "test3");

var eventData = new Dictionary<string, object>
{
["Name"] = "active-db-contexts",
["DisplayName"] = "Active DbContexts",
["Mean"] = 0d,
["StandardDeviation"] = 0,
["Count"] = 5,
["Min"] = 0,
["Max"] = 0,
["InvervalSec"] = 9.9996233,
["Series"] = "Interval=10000",
["CounterType"] = "Mean",
["Metadata"] = "",
["DisplayUnits"] = "",
};

meanCounter.TryReadEventCounterData(eventData);

Assert.Equal(5, meanCounter.Metric.Value);
}

[Fact]
public void MeanCounter_WhenMeanIsNaN_ThenUseZeroForValue()
{
var meanCounter = new MeanCounter("test1", "test2", "test3");

var eventData = new Dictionary<string, object>
{
["Name"] = "compiled-query-cache-hit-rate",
["DisplayName"] = "Query Cache Hit Rate",
["Mean"] = double.NaN,
["StandardDeviation"] = 0,
["Count"] = 1,
["Min"] = double.NaN,
["Max"] = double.NaN,
["InvervalSec"] = 9.9996233,
["Series"] = "Interval=10000",
["CounterType"] = "Mean",
["Metadata"] = "",
["DisplayUnits"] = "%"
};

meanCounter.TryReadEventCounterData(eventData);

Assert.Equal(0, meanCounter.Metric.Value);
}

[Fact]
public void IncrementCounter_WhenIncrement_ThenUseIncrementForValue()
{
var incrementCounter = new IncrementCounter("test1", "test2", "test3");

var eventData = new Dictionary<string, object>
{
["Name"] = "optimistic-concurrency-failures-per-second",
["DisplayName"] = "Optimistic Concurrency Failures",
["DisplayRateTimeScale"] = "00:00:01",
["Increment"] = 1d,
["IntervalSec"] = 675.144775,
["Metadata"] = "",
["Series"] = "Interval=10000",
["CounterType"] = "Sum",
["DisplayUnits"] = ""
};

incrementCounter.TryReadEventCounterData(eventData);

Assert.Equal(1, incrementCounter.Metric.Value);
}
}
}
27 changes: 27 additions & 0 deletions prometheus-net.Contrib.Tests/prometheus-net.Contrib.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>prometheus_net.Contrib.Tests</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\prometheus-net.Contrib\prometheus-net.Contrib.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions prometheus-net.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.github\workflows\run-tests.yaml = .github\workflows\run-tests.yaml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F671BC85-327B-4D38-B79B-468E6E014F28}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "prometheus-net.Contrib.Tests", "prometheus-net.Contrib.Tests\prometheus-net.Contrib.Tests.csproj", "{6F8CB8F7-B50E-4523-AAA2-2AE23DD64DFA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +61,10 @@ Global
{3A94C4BE-4CAB-43B4-8B9C-C1D9170514A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A94C4BE-4CAB-43B4-8B9C-C1D9170514A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A94C4BE-4CAB-43B4-8B9C-C1D9170514A8}.Release|Any CPU.Build.0 = Release|Any CPU
{6F8CB8F7-B50E-4523-AAA2-2AE23DD64DFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F8CB8F7-B50E-4523-AAA2-2AE23DD64DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F8CB8F7-B50E-4523-AAA2-2AE23DD64DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F8CB8F7-B50E-4523-AAA2-2AE23DD64DFA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -68,6 +76,7 @@ Global
{A19FEE93-6856-452D-84A1-0A934959D823} = {3706E795-AB7E-4E74-BE2B-32443C7D8479}
{4D84BD15-265B-4429-89BE-8A4015040B2F} = {3706E795-AB7E-4E74-BE2B-32443C7D8479}
{3A94C4BE-4CAB-43B4-8B9C-C1D9170514A8} = {3706E795-AB7E-4E74-BE2B-32443C7D8479}
{6F8CB8F7-B50E-4523-AAA2-2AE23DD64DFA} = {F671BC85-327B-4D38-B79B-468E6E014F28}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A88CA7BA-1BAD-4ADA-A863-C8FC9768A0D0}
Expand Down
3 changes: 3 additions & 0 deletions src/prometheus-net.Contrib/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("prometheus-net.Contrib.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ namespace Prometheus.Contrib.EventListeners.Counters
{
internal class IncrementCounter : BaseCounter
{
private static Gauge _metric;

public IncrementCounter(string name, string displayName, string description) : base(name, displayName, description)
{
_metric = Metrics.CreateGauge(DisplayName, Description);
Metric = Metrics.CreateGauge(DisplayName, Description);
}

internal Gauge Metric { get; }

public override bool TryReadEventCounterData(IDictionary<string, object> eventData)
{
if (!eventData.TryGetValue("Increment", out var increment))
return false;

_metric.Set((double)increment);
Metric.Set((double)increment);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace Prometheus.Contrib.EventListeners.Counters
{
internal class MeanCounter : BaseCounter
{
private readonly Gauge _metric;

public MeanCounter(string name, string displayName, string description) : base(name, displayName, description)
{
_metric = Metrics.CreateGauge(DisplayName, Description);
Metric = Metrics.CreateGauge(DisplayName, Description);
}

internal Gauge Metric { get; }

public override bool TryReadEventCounterData(IDictionary<string, object> eventData)
{
if (!(eventData.TryGetValue("Mean", out var meanObj) && meanObj is double mean)
Expand All @@ -22,7 +22,7 @@ public override bool TryReadEventCounterData(IDictionary<string, object> eventDa
var val = mean == 0 && count > 0 ? count : mean;
val = double.IsNaN(val) ? 0 : val;

_metric.Set(val);
Metric.Set(val);

return true;
}
Expand Down

0 comments on commit a0c895e

Please sign in to comment.