99
1010namespace Prometheus . Client . MetricServer . Tests ;
1111
12- public class MetricServerTests
12+ public class MetricServerTests ( PortFixture fixture , ITestOutputHelper testOutputHelper ) : IClassFixture < PortFixture >
1313{
14- private IMetricServer _metricServer ;
15- private readonly ITestOutputHelper _testOutputHelper ;
16- private const int _port = 9091 ;
17-
18- public MetricServerTests ( ITestOutputHelper testOutputHelper )
14+ private MetricServer _metricServer = new ( new MetricServerOptions
1915 {
20- _testOutputHelper = testOutputHelper ;
21- _metricServer = new MetricServer ( new MetricServerOptions
22- {
23- Port = _port ,
24- CollectorRegistryInstance = new CollectorRegistry ( )
25- } ) ;
26- }
16+ Port = fixture . Port ,
17+ CollectorRegistryInstance = new CollectorRegistry ( )
18+ } ) ;
2719
2820 [ Fact ]
2921 public void Null_Options_Throws_ArgumentNullException ( )
@@ -91,14 +83,14 @@ public async Task BaseMapPath_FindMetrics()
9183 var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
9284 counter . Inc ( ) ;
9385 using var httpClient = new HttpClient ( ) ;
94- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } { Defaults . MapPath } ") ;
86+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } { Defaults . MapPath } ") ;
9587 Assert . False ( string . IsNullOrEmpty ( response ) ) ;
9688 Assert . Contains ( "process_private_memory_bytes" , response ) ;
9789 Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
9890 }
9991 catch ( Exception ex )
10092 {
101- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
93+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
10294 throw ;
10395 }
10496 finally
@@ -110,21 +102,21 @@ public async Task BaseMapPath_FindMetrics()
110102 [ Fact ]
111103 public async Task SetMapPath_FindMetricsWithEndSlash ( )
112104 {
113- _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = "/test" } ) ;
105+ _metricServer = new MetricServer ( new MetricServerOptions { Port = fixture . Port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = "/test" } ) ;
114106 try
115107 {
116108 _metricServer . Start ( ) ;
117109 var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
118110 counter . Inc ( ) ;
119111 using var httpClient = new HttpClient ( ) ;
120- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /test/") ;
112+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } /test/") ;
121113 Assert . False ( string . IsNullOrEmpty ( response ) ) ;
122114 Assert . Contains ( "process_private_memory_bytes" , response ) ;
123115 Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
124116 }
125117 catch ( Exception ex )
126118 {
127- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
119+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
128120 throw ;
129121 }
130122 finally
@@ -140,21 +132,21 @@ public async Task SetMapPath_FindMetricsWithEndSlash()
140132 [ InlineData ( "/metrics965" ) ]
141133 public async Task SetMapPath_FindMetrics ( string mapPath )
142134 {
143- _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = mapPath } ) ;
135+ _metricServer = new MetricServer ( new MetricServerOptions { Port = fixture . Port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = mapPath } ) ;
144136 try
145137 {
146138 _metricServer . Start ( ) ;
147139 using var httpClient = new HttpClient ( ) ;
148140 if ( ! mapPath . StartsWith ( "/" ) )
149141 mapPath = "/" + mapPath ;
150- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } " + mapPath ) ;
142+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } " + mapPath ) ;
151143 Assert . False ( string . IsNullOrEmpty ( response ) ) ;
152144 Assert . Contains ( "process_private_memory_bytes" , response ) ;
153145 Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
154146 }
155147 catch ( Exception ex )
156148 {
157- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
149+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
158150 throw ;
159151 }
160152 finally
@@ -168,7 +160,7 @@ public async Task CustomCounter_FindMetric()
168160 {
169161 var registry = new CollectorRegistry ( ) ;
170162 var factory = new MetricFactory ( registry ) ;
171- _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = registry } ) ;
163+ _metricServer = new MetricServer ( new MetricServerOptions { Port = fixture . Port , CollectorRegistryInstance = registry } ) ;
172164
173165 try
174166 {
@@ -179,12 +171,12 @@ public async Task CustomCounter_FindMetric()
179171 counter . Inc ( ) ;
180172
181173 using var httpClient = new HttpClient ( ) ;
182- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } { Defaults . MapPath } ") ;
174+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } { Defaults . MapPath } ") ;
183175 Assert . Contains ( metricName , response ) ;
184176 }
185177 catch ( Exception ex )
186178 {
187- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
179+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
188180 throw ;
189181 }
190182 finally
@@ -200,15 +192,15 @@ public async Task AddLegacyMetrics_False_FindMetrics()
200192 {
201193 _metricServer . Start ( ) ;
202194 using var httpClient = new HttpClient ( ) ;
203- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } { Defaults . MapPath } ") ;
195+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } { Defaults . MapPath } ") ;
204196 Assert . Contains ( "process_private_memory_bytes" , response ) ;
205197 Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
206198 Assert . DoesNotContain ( "process_private_bytes" , response ) ;
207199 Assert . DoesNotContain ( "dotnet_totalmemory" , response ) ;
208200 }
209201 catch ( Exception ex )
210202 {
211- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
203+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
212204 throw ;
213205 }
214206 finally
@@ -220,21 +212,22 @@ public async Task AddLegacyMetrics_False_FindMetrics()
220212 [ Fact ]
221213 public async Task AddLegacyMetrics_True_FindMetrics ( )
222214 {
223- _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , AddLegacyMetrics = true } ) ;
215+ _metricServer = new MetricServer ( new MetricServerOptions
216+ { Port = fixture . Port , CollectorRegistryInstance = new CollectorRegistry ( ) , AddLegacyMetrics = true } ) ;
224217
225218 try
226219 {
227220 _metricServer . Start ( ) ;
228221 using var httpClient = new HttpClient ( ) ;
229- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } { Defaults . MapPath } ") ;
222+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } { Defaults . MapPath } ") ;
230223 Assert . Contains ( "process_private_memory_bytes" , response ) ;
231224 Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
232225 Assert . Contains ( "process_private_bytes" , response ) ;
233226 Assert . Contains ( "dotnet_totalmemory" , response ) ;
234227 }
235228 catch ( Exception ex )
236229 {
237- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
230+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
238231 throw ;
239232 }
240233 finally
@@ -253,12 +246,12 @@ public async Task WrongUrl_NotFound()
253246 counter . Inc ( ) ;
254247 using var httpClient = new HttpClient ( ) ;
255248
256- var response = await httpClient . GetAsync ( $ "http://localhost:{ _port } /not-found") ;
249+ var response = await httpClient . GetAsync ( $ "http://localhost:{ fixture . Port } /not-found") ;
257250 Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
258251 }
259252 catch ( Exception ex )
260253 {
261- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
254+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
262255 throw ;
263256 }
264257 finally
@@ -272,7 +265,7 @@ public async Task CustormEncoding_FindHelp()
272265 {
273266 var registry = new CollectorRegistry ( ) ;
274267 var factory = new MetricFactory ( registry ) ;
275- _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = registry , ResponseEncoding = Encoding . UTF8 } ) ;
268+ _metricServer = new MetricServer ( new MetricServerOptions { Port = fixture . Port , CollectorRegistryInstance = registry , ResponseEncoding = Encoding . UTF8 } ) ;
276269
277270 try
278271 {
@@ -283,12 +276,12 @@ public async Task CustormEncoding_FindHelp()
283276 counter . Inc ( ) ;
284277
285278 using var httpClient = new HttpClient ( ) ;
286- string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } { Defaults . MapPath } ") ;
279+ string response = await httpClient . GetStringAsync ( $ "http://localhost:{ fixture . Port } { Defaults . MapPath } ") ;
287280 Assert . Contains ( help , response ) ;
288281 }
289282 catch ( Exception ex )
290283 {
291- _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
284+ testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
292285 throw ;
293286 }
294287 finally
0 commit comments