@@ -25,6 +25,12 @@ public MetricServerTests(ITestOutputHelper testOutputHelper)
25
25
} ) ;
26
26
}
27
27
28
+ [ Fact ]
29
+ public void Null_Options_Throws_ArgumentNullException ( )
30
+ {
31
+ Assert . Throws < ArgumentNullException > ( ( ) => new MetricServer ( null ) ) ;
32
+ }
33
+
28
34
[ Fact ]
29
35
public void Start_Stop_IsRunning ( )
30
36
{
@@ -34,6 +40,7 @@ public void Start_Stop_IsRunning()
34
40
Assert . False ( _metricServer . IsRunning ) ;
35
41
}
36
42
43
+
37
44
[ Fact ]
38
45
public void Start_DoubleStop_IsRunning ( )
39
46
{
@@ -57,7 +64,7 @@ public void DoubleStart_Stop_IsRunning()
57
64
}
58
65
59
66
[ Fact ]
60
- public void Start_Stop_DefaultPort_IsRunning ( )
67
+ public void Start_Stop_WithDefaultPort_IsRunning ( )
61
68
{
62
69
_metricServer = new MetricServer ( new MetricServerOptions { CollectorRegistryInstance = new CollectorRegistry ( ) } ) ;
63
70
_metricServer . Start ( ) ;
@@ -66,8 +73,19 @@ public void Start_Stop_DefaultPort_IsRunning()
66
73
Assert . False ( _metricServer . IsRunning ) ;
67
74
}
68
75
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
+
69
87
[ Fact ]
70
- public async Task Base_MapPath ( )
88
+ public async Task BaseMapPath_FindMetrics ( )
71
89
{
72
90
try
73
91
{
@@ -77,6 +95,8 @@ public async Task Base_MapPath()
77
95
using var httpClient = new HttpClient ( ) ;
78
96
string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /metrics") ;
79
97
Assert . False ( string . IsNullOrEmpty ( response ) ) ;
98
+ Assert . Contains ( "process_private_memory_bytes" , response ) ;
99
+ Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
80
100
}
81
101
catch ( Exception ex )
82
102
{
@@ -90,7 +110,7 @@ public async Task Base_MapPath()
90
110
}
91
111
92
112
[ Fact ]
93
- public async Task MapPath_WithEndSlash ( )
113
+ public async Task SetMapPath_FindMetricsWithEndSlash ( )
94
114
{
95
115
_metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = "/test" } ) ;
96
116
try
@@ -101,6 +121,8 @@ public async Task MapPath_WithEndSlash()
101
121
using var httpClient = new HttpClient ( ) ;
102
122
string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /test/") ;
103
123
Assert . False ( string . IsNullOrEmpty ( response ) ) ;
124
+ Assert . Contains ( "process_private_memory_bytes" , response ) ;
125
+ Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
104
126
}
105
127
catch ( Exception ex )
106
128
{
@@ -114,20 +136,23 @@ public async Task MapPath_WithEndSlash()
114
136
}
115
137
116
138
[ Theory ]
139
+ [ InlineData ( "metrics" ) ]
117
140
[ InlineData ( "/metrics" ) ]
118
- [ InlineData ( "/ metrics12" ) ]
141
+ [ InlineData ( "metrics12" ) ]
119
142
[ InlineData ( "/metrics965" ) ]
120
- public async Task MapPath ( string mapPath )
143
+ public async Task SetMapPath_FindMetrics ( string mapPath )
121
144
{
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 } ) ;
123
146
try
124
147
{
125
148
_metricServer . Start ( ) ;
126
- var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
127
- counter . Inc ( ) ;
128
149
using var httpClient = new HttpClient ( ) ;
150
+ if ( ! mapPath . StartsWith ( "/" ) )
151
+ mapPath = "/" + mapPath ;
129
152
string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } " + mapPath ) ;
130
153
Assert . False ( string . IsNullOrEmpty ( response ) ) ;
154
+ Assert . Contains ( "process_private_memory_bytes" , response ) ;
155
+ Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
131
156
}
132
157
catch ( Exception ex )
133
158
{
@@ -141,7 +166,7 @@ public async Task MapPath(string mapPath)
141
166
}
142
167
143
168
[ Fact ]
144
- public async Task Custom_Find_Metric ( )
169
+ public async Task CustomCounter_FindMetric ( )
145
170
{
146
171
var registry = new CollectorRegistry ( ) ;
147
172
var factory = new MetricFactory ( registry ) ;
@@ -171,7 +196,7 @@ public async Task Custom_Find_Metric()
171
196
}
172
197
173
198
[ Fact ]
174
- public async Task AddLegacyMetrics_False_CheckMetrics ( )
199
+ public async Task AddLegacyMetrics_False_FindMetrics ( )
175
200
{
176
201
try
177
202
{
@@ -195,7 +220,7 @@ public async Task AddLegacyMetrics_False_CheckMetrics()
195
220
}
196
221
197
222
[ Fact ]
198
- public async Task AddLegacyMetrics_True_CheckMetrics ( )
223
+ public async Task AddLegacyMetrics_True_FindMetrics ( )
199
224
{
200
225
_metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , AddLegacyMetrics = true } ) ;
201
226
@@ -221,7 +246,7 @@ public async Task AddLegacyMetrics_True_CheckMetrics()
221
246
}
222
247
223
248
[ Fact ]
224
- public async Task Url_NotFound ( )
249
+ public async Task WrongUrl_NotFound ( )
225
250
{
226
251
try
227
252
{
@@ -230,7 +255,7 @@ public async Task Url_NotFound()
230
255
counter . Inc ( ) ;
231
256
using var httpClient = new HttpClient ( ) ;
232
257
233
- var response = await httpClient . GetAsync ( $ "http://localhost:{ _port } ") ;
258
+ var response = await httpClient . GetAsync ( $ "http://localhost:{ _port } /not-found ") ;
234
259
Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
235
260
}
236
261
catch ( Exception ex )
@@ -245,32 +270,7 @@ public async Task Url_NotFound()
245
270
}
246
271
247
272
[ 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 ( )
274
274
{
275
275
var registry = new CollectorRegistry ( ) ;
276
276
var factory = new MetricFactory ( registry ) ;
@@ -298,10 +298,4 @@ public async Task Add_Encoding()
298
298
_metricServer . Stop ( ) ;
299
299
}
300
300
}
301
-
302
- [ Fact ]
303
- public void Null_Options_Throws_ArgumentNullException ( )
304
- {
305
- Assert . Throws < ArgumentNullException > ( ( ) => new MetricServer ( null ) ) ;
306
- }
307
301
}
0 commit comments