Skip to content

Commit 4ae6922

Browse files
committed
Fixing up the DotNetRuntimeStatsBuilderTests
1 parent 56437cf commit 4ae6922

File tree

1 file changed

+86
-36
lines changed

1 file changed

+86
-36
lines changed

src/prometheus-net.DotNetRuntime.Tests/DotNetRuntimeStatsBuilderTests.cs

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
using System.Collections.Generic;
33
using System.Net.Http;
44
using System.Threading.Tasks;
5+
using Microsoft.VisualStudio.TestPlatform.Common.Telemetry;
56
using NUnit.Framework;
67
#if PROMV2
7-
using Prometheus.Advanced;
8+
using Prometheus.Advanced;
89
#endif
910
using Prometheus.DotNetRuntime.StatsCollectors;
1011

@@ -23,25 +24,22 @@ public async Task Default_registers_all_expected_stats()
2324
{
2425
// arrange
2526
using (DotNetRuntimeStatsBuilder.Default().StartCollecting())
26-
using (var metricServer = new MetricServer(12203))
27-
using (var client = new HttpClient())
2827
{
29-
metricServer.Start();
30-
31-
// act + assert
32-
using (var resp = await client.GetAsync("http://localhost:12203/metrics"))
33-
{
34-
var content = await resp.Content.ReadAsStringAsync();
35-
36-
// Some basic assertions to check that the output of our stats collectors is present
37-
Assert.That(content, Contains.Substring("dotnet_threadpool"));
38-
Assert.That(content, Contains.Substring("dotnet_jit"));
39-
Assert.That(content, Contains.Substring("dotnet_gc"));
40-
Assert.That(content, Contains.Substring("dotnet_contention"));
41-
}
28+
await Assert_Expected_Stats_Are_Present_In_Registry(GetDefaultRegistry());
4229
}
4330
}
4431

32+
[Test]
33+
public async Task Default_registers_all_expected_stats_to_a_custom_registry()
34+
{
35+
// arrange
36+
var registry = NewRegistry();
37+
using (DotNetRuntimeStatsBuilder.Default().StartCollecting(registry))
38+
{
39+
await Assert_Expected_Stats_Are_Present_In_Registry(registry);
40+
}
41+
}
42+
4543
[Test]
4644
public void WithCustomCollector_will_not_register_the_same_collector_twice()
4745
{
@@ -52,7 +50,7 @@ public void WithCustomCollector_will_not_register_the_same_collector_twice()
5250

5351
Assert.That(builder.StatsCollectors.Count, Is.EqualTo(1));
5452
}
55-
53+
5654
[Test]
5755
public void StartCollecting_Does_Not_Allow_Two_Collectors_To_Run_Simultaneously()
5856
{
@@ -61,29 +59,26 @@ public void StartCollecting_Does_Not_Allow_Two_Collectors_To_Run_Simultaneously(
6159
Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting());
6260
}
6361
}
64-
62+
6563
[Test]
66-
public void StartCollecting_Allows_A_New_Collector_To_Run_After_Disposing_A_Previous_Collector()
64+
public async Task StartCollecting_Allows_A_New_Collector_To_Run_After_Disposing_A_Previous_Collector()
6765
{
6866
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting())
6967
{
68+
await Assert_Expected_Stats_Are_Present_In_Registry(GetDefaultRegistry());
7069
}
71-
70+
7271
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting())
7372
{
73+
await Assert_Expected_Stats_Are_Present_In_Registry(GetDefaultRegistry());
7474
}
7575
}
7676

7777
[Test]
7878
public void StartCollecting_Does_Not_Allow_Two_Collectors_To_Run_Simultaneously_For_Each_Registry_Instance()
7979
{
80-
#if PROMV2
81-
var registry1 = new DefaultCollectorRegistry();
82-
var registry2 = new DefaultCollectorRegistry();
83-
#elif PROMV3
84-
var registry1 = Metrics.NewCustomRegistry();
85-
var registry2 = Metrics.NewCustomRegistry();
86-
#endif
80+
var registry1 = NewRegistry();;
81+
var registry2 = NewRegistry();;
8782

8883
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting())
8984
{
@@ -98,42 +93,97 @@ public void StartCollecting_Does_Not_Allow_Two_Collectors_To_Run_Simultaneously_
9893
Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1));
9994
Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2));
10095
}
96+
10197
Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting());
10298
Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1));
10399
}
100+
104101
Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting());
105102
}
106103
}
107104

108105
[Test]
109106
public void StartCollecting_Allows_A_New_Collector_To_Run_After_Disposing_Previous_Collector_For_Each_Registry_Instance()
110107
{
111-
#if PROMV2
112-
var registry1 = new DefaultCollectorRegistry();
113-
var registry2 = new DefaultCollectorRegistry();
114-
#elif PROMV3
115-
var registry1 = Metrics.NewCustomRegistry();
116-
var registry2 = Metrics.NewCustomRegistry();
117-
#endif
118-
108+
var registry1 = NewRegistry();
109+
var registry2 = NewRegistry();
110+
119111
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1))
120112
{
121113
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2))
122114
{
123115
}
116+
124117
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2))
125118
{
126119
}
127120
}
121+
128122
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2))
129123
{
130124
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1))
131125
{
132126
}
127+
133128
using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1))
134129
{
135130
}
136131
}
137132
}
133+
134+
private async Task Assert_Expected_Stats_Are_Present_In_Registry(
135+
#if PROMV2
136+
DefaultCollectorRegistry registry
137+
#else
138+
CollectorRegistry registry
139+
#endif
140+
)
141+
{
142+
// arrange
143+
const int metricsPort = 12203;
144+
using (var metricServer = new MetricServer(metricsPort, registry: registry))
145+
using (var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(2) })
146+
{
147+
metricServer.Start();
148+
149+
// act + assert
150+
using (var resp = await client.GetAsync($"http://localhost:{metricsPort}/metrics"))
151+
{
152+
var content = await resp.Content.ReadAsStringAsync();
153+
154+
// Some basic assertions to check that the output of our stats collectors is present
155+
Assert.That(content, Contains.Substring("dotnet_threadpool"));
156+
Assert.That(content, Contains.Substring("dotnet_jit"));
157+
Assert.That(content, Contains.Substring("dotnet_gc"));
158+
Assert.That(content, Contains.Substring("dotnet_contention"));
159+
Assert.That(content, Contains.Substring("dotnet_build_info"));
160+
Assert.That(content, Contains.Substring("process_cpu_count"));
161+
}
162+
}
163+
}
164+
165+
#if PROMV2
166+
private DefaultCollectorRegistry NewRegistry()
167+
{
168+
return new DefaultCollectorRegistry();
169+
}
170+
171+
private DefaultCollectorRegistry GetDefaultRegistry()
172+
{
173+
return DefaultCollectorRegistry.Instance;
174+
}
175+
176+
#elif PROMV3
177+
private CollectorRegistry NewRegistry()
178+
{
179+
return Metrics.NewCustomRegistry();
180+
}
181+
182+
private CollectorRegistry GetDefaultRegistry()
183+
{
184+
return Metrics.DefaultRegistry;
185+
}
186+
#endif
187+
138188
}
139-
}
189+
}

0 commit comments

Comments
 (0)