Skip to content

Commit f473eb7

Browse files
committed
test: improve tests
1 parent 37fe583 commit f473eb7

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

tests/MetricServerTests.cs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public MetricServerTests(ITestOutputHelper testOutputHelper)
2525
});
2626
}
2727

28+
[Fact]
29+
public void Null_Options_Throws_ArgumentNullException()
30+
{
31+
Assert.Throws<ArgumentNullException>(() => new MetricServer(null));
32+
}
33+
2834
[Fact]
2935
public void Start_Stop_IsRunning()
3036
{
@@ -34,6 +40,7 @@ public void Start_Stop_IsRunning()
3440
Assert.False(_metricServer.IsRunning);
3541
}
3642

43+
3744
[Fact]
3845
public void Start_DoubleStop_IsRunning()
3946
{
@@ -57,7 +64,7 @@ public void DoubleStart_Stop_IsRunning()
5764
}
5865

5966
[Fact]
60-
public void Start_Stop_DefaultPort_IsRunning()
67+
public void Start_Stop_WithDefaultPort_IsRunning()
6168
{
6269
_metricServer = new MetricServer(new MetricServerOptions { CollectorRegistryInstance = new CollectorRegistry() });
6370
_metricServer.Start();
@@ -66,8 +73,19 @@ public void Start_Stop_DefaultPort_IsRunning()
6673
Assert.False(_metricServer.IsRunning);
6774
}
6875

76+
77+
[Fact]
78+
public void Start_Stop_WithDefaultRegisry_IsRunning()
79+
{
80+
_metricServer = new MetricServer();
81+
_metricServer.Start();
82+
Assert.True(_metricServer.IsRunning);
83+
_metricServer.Stop();
84+
Assert.False(_metricServer.IsRunning);
85+
}
86+
6987
[Fact]
70-
public async Task Base_MapPath()
88+
public async Task BaseMapPath_FindMetrics()
7189
{
7290
try
7391
{
@@ -77,6 +95,8 @@ public async Task Base_MapPath()
7795
using var httpClient = new HttpClient();
7896
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
7997
Assert.False(string.IsNullOrEmpty(response));
98+
Assert.Contains("process_private_memory_bytes", response);
99+
Assert.Contains("dotnet_total_memory_bytes", response);
80100
}
81101
catch (Exception ex)
82102
{
@@ -90,7 +110,7 @@ public async Task Base_MapPath()
90110
}
91111

92112
[Fact]
93-
public async Task MapPath_WithEndSlash()
113+
public async Task SetMapPath_FindMetricsWithEndSlash()
94114
{
95115
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = "/test" });
96116
try
@@ -101,6 +121,8 @@ public async Task MapPath_WithEndSlash()
101121
using var httpClient = new HttpClient();
102122
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/test/");
103123
Assert.False(string.IsNullOrEmpty(response));
124+
Assert.Contains("process_private_memory_bytes", response);
125+
Assert.Contains("dotnet_total_memory_bytes", response);
104126
}
105127
catch (Exception ex)
106128
{
@@ -114,20 +136,23 @@ public async Task MapPath_WithEndSlash()
114136
}
115137

116138
[Theory]
139+
[InlineData("metrics")]
117140
[InlineData("/metrics")]
118-
[InlineData("/metrics12")]
141+
[InlineData("metrics12")]
119142
[InlineData("/metrics965")]
120-
public async Task MapPath(string mapPath)
143+
public async Task SetMapPath_FindMetrics(string mapPath)
121144
{
122-
_metricServer= new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = mapPath });
145+
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), MapPath = mapPath });
123146
try
124147
{
125148
_metricServer.Start();
126-
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
127-
counter.Inc();
128149
using var httpClient = new HttpClient();
150+
if (!mapPath.StartsWith("/"))
151+
mapPath = "/" + mapPath;
129152
string response = await httpClient.GetStringAsync($"http://localhost:{_port}" + mapPath);
130153
Assert.False(string.IsNullOrEmpty(response));
154+
Assert.Contains("process_private_memory_bytes", response);
155+
Assert.Contains("dotnet_total_memory_bytes", response);
131156
}
132157
catch (Exception ex)
133158
{
@@ -141,7 +166,7 @@ public async Task MapPath(string mapPath)
141166
}
142167

143168
[Fact]
144-
public async Task Custom_Find_Metric()
169+
public async Task CustomCounter_FindMetric()
145170
{
146171
var registry = new CollectorRegistry();
147172
var factory = new MetricFactory(registry);
@@ -171,7 +196,7 @@ public async Task Custom_Find_Metric()
171196
}
172197

173198
[Fact]
174-
public async Task AddLegacyMetrics_False_CheckMetrics()
199+
public async Task AddLegacyMetrics_False_FindMetrics()
175200
{
176201
try
177202
{
@@ -195,7 +220,7 @@ public async Task AddLegacyMetrics_False_CheckMetrics()
195220
}
196221

197222
[Fact]
198-
public async Task AddLegacyMetrics_True_CheckMetrics()
223+
public async Task AddLegacyMetrics_True_FindMetrics()
199224
{
200225
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), AddLegacyMetrics = true });
201226

@@ -221,7 +246,7 @@ public async Task AddLegacyMetrics_True_CheckMetrics()
221246
}
222247

223248
[Fact]
224-
public async Task Url_NotFound()
249+
public async Task WrongUrl_NotFound()
225250
{
226251
try
227252
{
@@ -230,7 +255,7 @@ public async Task Url_NotFound()
230255
counter.Inc();
231256
using var httpClient = new HttpClient();
232257

233-
var response = await httpClient.GetAsync($"http://localhost:{_port}");
258+
var response = await httpClient.GetAsync($"http://localhost:{_port}/not-found");
234259
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
235260
}
236261
catch (Exception ex)
@@ -245,32 +270,7 @@ public async Task Url_NotFound()
245270
}
246271

247272
[Fact]
248-
public async Task Find_Default_Metric()
249-
{
250-
_metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = new CollectorRegistry(), UseDefaultCollectors = true });
251-
252-
try
253-
{
254-
_metricServer.Start();
255-
256-
using var httpClient = new HttpClient();
257-
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
258-
Assert.Contains("dotnet_collection_count_total", response);
259-
Assert.Contains("process_cpu_seconds_total", response);
260-
}
261-
catch (Exception ex)
262-
{
263-
_testOutputHelper.WriteLine(ex.ToString());
264-
throw;
265-
}
266-
finally
267-
{
268-
_metricServer.Stop();
269-
}
270-
}
271-
272-
[Fact]
273-
public async Task Add_Encoding()
273+
public async Task CustormEncoding_FindHelp()
274274
{
275275
var registry = new CollectorRegistry();
276276
var factory = new MetricFactory(registry);
@@ -298,10 +298,4 @@ public async Task Add_Encoding()
298298
_metricServer.Stop();
299299
}
300300
}
301-
302-
[Fact]
303-
public void Null_Options_Throws_ArgumentNullException()
304-
{
305-
Assert.Throws<ArgumentNullException>(() => new MetricServer(null));
306-
}
307301
}

0 commit comments

Comments
 (0)