Skip to content

Commit cbe7c8d

Browse files
committed
test: fix registry conflicts
1 parent c6091a5 commit cbe7c8d

File tree

1 file changed

+58
-70
lines changed

1 file changed

+58
-70
lines changed

tests/MetricServerTests.cs

Lines changed: 58 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,67 @@ namespace Prometheus.Client.MetricServer.Tests;
1111

1212
public class MetricServerTests
1313
{
14+
private IMetricServer _metricServer;
1415
private readonly ITestOutputHelper _testOutputHelper;
1516
private const int _port = 9091;
1617

1718
public MetricServerTests(ITestOutputHelper testOutputHelper)
1819
{
1920
_testOutputHelper = testOutputHelper;
21+
_metricServer = new MetricServer(new MetricServerOptions
22+
{
23+
Port = _port,
24+
CollectorRegistryInstance = new CollectorRegistry()
25+
});
2026
}
2127

2228
[Fact]
2329
public void Start_Stop_IsRunning()
2430
{
25-
var metricServer = new MetricServer(new MetricServerOptions
26-
{
27-
Port = _port,
28-
CollectorRegistryInstance = new CollectorRegistry()
29-
});
30-
metricServer.Start();
31-
Assert.True(metricServer.IsRunning);
32-
metricServer.Stop();
33-
Assert.False(metricServer.IsRunning);
31+
_metricServer.Start();
32+
Assert.True(_metricServer.IsRunning);
33+
_metricServer.Stop();
34+
Assert.False(_metricServer.IsRunning);
3435
}
3536

3637
[Fact]
3738
public void Start_DoubleStop_IsRunning()
3839
{
39-
var metricServer = new MetricServer(new MetricServerOptions
40-
{
41-
Port = _port,
42-
CollectorRegistryInstance = new CollectorRegistry()
43-
});
44-
metricServer.Start();
45-
Assert.True(metricServer.IsRunning);
46-
metricServer.Stop();
47-
Assert.False(metricServer.IsRunning);
48-
metricServer.Stop();
49-
Assert.False(metricServer.IsRunning);
40+
_metricServer.Start();
41+
Assert.True(_metricServer.IsRunning);
42+
_metricServer.Stop();
43+
Assert.False(_metricServer.IsRunning);
44+
_metricServer.Stop();
45+
Assert.False(_metricServer.IsRunning);
5046
}
5147

5248
[Fact]
5349
public void DoubleStart_Stop_IsRunning()
5450
{
55-
var metricServer = new MetricServer(new MetricServerOptions
56-
{
57-
Port = _port,
58-
CollectorRegistryInstance = new CollectorRegistry()
59-
});
60-
metricServer.Start();
61-
Assert.True(metricServer.IsRunning);
62-
metricServer.Start();
63-
Assert.True(metricServer.IsRunning);
64-
metricServer.Stop();
65-
Assert.False(metricServer.IsRunning);
51+
_metricServer.Start();
52+
Assert.True(_metricServer.IsRunning);
53+
_metricServer.Start();
54+
Assert.True(_metricServer.IsRunning);
55+
_metricServer.Stop();
56+
Assert.False(_metricServer.IsRunning);
6657
}
6758

6859
[Fact]
6960
public void Start_Stop_DefaultPort_IsRunning()
7061
{
71-
var metricServer = new MetricServer();
72-
metricServer.Start();
73-
Assert.True(metricServer.IsRunning);
74-
metricServer.Stop();
75-
Assert.False(metricServer.IsRunning);
62+
_metricServer = new MetricServer(new MetricServerOptions { CollectorRegistryInstance = new CollectorRegistry() });
63+
_metricServer.Start();
64+
Assert.True(_metricServer.IsRunning);
65+
_metricServer.Stop();
66+
Assert.False(_metricServer.IsRunning);
7667
}
7768

7869
[Fact]
7970
public async Task Base_MapPath()
8071
{
81-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port });
8272
try
8373
{
84-
metricServer.Start();
74+
_metricServer.Start();
8575
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
8676
counter.Inc();
8777
using var httpClient = new HttpClient();
@@ -95,17 +85,17 @@ public async Task Base_MapPath()
9585
}
9686
finally
9787
{
98-
metricServer.Stop();
88+
_metricServer.Stop();
9989
}
10090
}
10191

10292
[Fact]
10393
public async Task MapPath_WithEndSlash()
10494
{
105-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, MapPath = "/test" });
95+
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = "/test" });
10696
try
10797
{
108-
metricServer.Start();
98+
_metricServer.Start();
10999
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
110100
counter.Inc();
111101
using var httpClient = new HttpClient();
@@ -119,7 +109,7 @@ public async Task MapPath_WithEndSlash()
119109
}
120110
finally
121111
{
122-
metricServer.Stop();
112+
_metricServer.Stop();
123113
}
124114
}
125115

@@ -136,10 +126,10 @@ public void Wrong_MapPath()
136126
[InlineData("/metrics965")]
137127
public async Task MapPath(string mapPath)
138128
{
139-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, MapPath = mapPath });
129+
_metricServer= new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = mapPath });
140130
try
141131
{
142-
metricServer.Start();
132+
_metricServer.Start();
143133
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
144134
counter.Inc();
145135
using var httpClient = new HttpClient();
@@ -153,7 +143,7 @@ public async Task MapPath(string mapPath)
153143
}
154144
finally
155145
{
156-
metricServer.Stop();
146+
_metricServer.Stop();
157147
}
158148
}
159149

@@ -162,11 +152,11 @@ public async Task Custom_Find_Metric()
162152
{
163153
var registry = new CollectorRegistry();
164154
var factory = new MetricFactory(registry);
165-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry });
155+
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry });
166156

167157
try
168158
{
169-
metricServer.Start();
159+
_metricServer.Start();
170160

171161
const string metricName = "myCounter";
172162
var counter = factory.CreateCounter(metricName, "helptext");
@@ -183,23 +173,21 @@ public async Task Custom_Find_Metric()
183173
}
184174
finally
185175
{
186-
metricServer.Stop();
176+
_metricServer.Stop();
187177
}
188178
}
189179

190180
[Fact]
191181
public async Task AddLegacyMetrics_False_CheckMetrics()
192182
{
193-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry() });
194-
195183
try
196184
{
197-
metricServer.Start();
185+
_metricServer.Start();
198186
using var httpClient = new HttpClient();
199187
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
200-
Assert.Contains("process_working_set_bytes", response);
188+
Assert.Contains("process_private_memory_bytes", response);
201189
Assert.Contains("dotnet_total_memory_bytes", response);
202-
Assert.DoesNotContain("process_working_set", response);
190+
Assert.DoesNotContain("process_private_bytes", response);
203191
Assert.DoesNotContain("dotnet_totalmemory", response);
204192
}
205193
catch (Exception ex)
@@ -209,23 +197,23 @@ public async Task AddLegacyMetrics_False_CheckMetrics()
209197
}
210198
finally
211199
{
212-
metricServer.Stop();
200+
_metricServer.Stop();
213201
}
214202
}
215203

216204
[Fact]
217205
public async Task AddLegacyMetrics_True_CheckMetrics()
218206
{
219-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), AddLegacyMetrics = true });
207+
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), AddLegacyMetrics = true });
220208

221209
try
222210
{
223-
metricServer.Start();
211+
_metricServer.Start();
224212
using var httpClient = new HttpClient();
225213
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
226-
Assert.Contains("process_working_set_bytes", response);
214+
Assert.Contains("process_private_memory_bytes", response);
227215
Assert.Contains("dotnet_total_memory_bytes", response);
228-
Assert.Contains("process_working_set", response);
216+
Assert.Contains("process_private_bytes", response);
229217
Assert.Contains("dotnet_totalmemory", response);
230218
}
231219
catch (Exception ex)
@@ -235,17 +223,16 @@ public async Task AddLegacyMetrics_True_CheckMetrics()
235223
}
236224
finally
237225
{
238-
metricServer.Stop();
226+
_metricServer.Stop();
239227
}
240228
}
241229

242230
[Fact]
243231
public async Task Url_NotFound()
244232
{
245-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port });
246233
try
247234
{
248-
metricServer.Start();
235+
_metricServer.Start();
249236
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
250237
counter.Inc();
251238
using var httpClient = new HttpClient();
@@ -260,19 +247,18 @@ public async Task Url_NotFound()
260247
}
261248
finally
262249
{
263-
metricServer.Stop();
250+
_metricServer.Stop();
264251
}
265252
}
266253

267254
[Fact]
268255
public async Task Find_Default_Metric()
269256
{
270-
var registry = new CollectorRegistry();
271-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry, UseDefaultCollectors = true });
257+
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), UseDefaultCollectors = true });
272258

273259
try
274260
{
275-
metricServer.Start();
261+
_metricServer.Start();
276262

277263
using var httpClient = new HttpClient();
278264
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
@@ -286,21 +272,23 @@ public async Task Find_Default_Metric()
286272
}
287273
finally
288274
{
289-
metricServer.Stop();
275+
_metricServer.Stop();
290276
}
291277
}
292278

293279
[Fact]
294280
public async Task Add_Encoding()
295281
{
296-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, ResponseEncoding = Encoding.UTF8 });
282+
var registry = new CollectorRegistry();
283+
var factory = new MetricFactory(registry);
284+
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry, ResponseEncoding = Encoding.UTF8 });
297285

298286
try
299287
{
300-
metricServer.Start();
288+
_metricServer.Start();
301289

302290
const string help = "русский хелп";
303-
var counter = Metrics.DefaultFactory.CreateCounter("test_counter_rus", help);
291+
var counter = factory.CreateCounter("test_counter_rus", help);
304292
counter.Inc();
305293

306294
using var httpClient = new HttpClient();
@@ -314,7 +302,7 @@ public async Task Add_Encoding()
314302
}
315303
finally
316304
{
317-
metricServer.Stop();
305+
_metricServer.Stop();
318306
}
319307
}
320308

0 commit comments

Comments
 (0)