44using System . Threading . Tasks ;
55using Prometheus . Client . Collectors ;
66using Xunit ;
7+ using Xunit . Abstractions ;
78
8- namespace Prometheus . Client . MetricServer . NetCore . Tests
9+ namespace Prometheus . Client . MetricServer . Tests
910{
1011 public class MetricServerTests
1112 {
12- private const int _port = 5050 ;
13+ private readonly ITestOutputHelper _testOutputHelper ;
14+ private const int _port = 5055 ;
15+
16+ public MetricServerTests ( ITestOutputHelper testOutputHelper )
17+ {
18+ _testOutputHelper = testOutputHelper ;
19+ }
1320
1421 [ Fact ]
1522 public void Start_Stop_IsRunning ( )
@@ -25,15 +32,24 @@ public void Start_Stop_IsRunning()
2532 public async Task Base_MapPath ( )
2633 {
2734 var metricServer = new MetricServer ( new CollectorRegistry ( ) , new MetricServerOptions { Port = _port } ) ;
28- metricServer . Start ( ) ;
29-
30- using ( var httpClient = new HttpClient ( ) )
35+ try
3136 {
37+ metricServer . Start ( ) ;
38+ var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
39+ counter . Inc ( ) ;
40+ using var httpClient = new HttpClient ( ) ;
3241 var response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /metrics") ;
3342 Assert . False ( string . IsNullOrEmpty ( response ) ) ;
3443 }
35-
36- metricServer . Stop ( ) ;
44+ catch ( Exception ex )
45+ {
46+ _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
47+ throw ;
48+ }
49+ finally
50+ {
51+ metricServer . Stop ( ) ;
52+ }
3753 }
3854
3955 [ Fact ]
@@ -42,15 +58,24 @@ public async Task MapPath_WithEndSlash()
4258 var metricServer = new MetricServer (
4359 new CollectorRegistry ( ) ,
4460 new MetricServerOptions { Port = _port , MapPath = "/test" } ) ;
45- metricServer . Start ( ) ;
46-
47- using ( var httpClient = new HttpClient ( ) )
61+ try
4862 {
63+ metricServer . Start ( ) ;
64+ var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
65+ counter . Inc ( ) ;
66+ using var httpClient = new HttpClient ( ) ;
4967 var response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /test/") ;
5068 Assert . False ( string . IsNullOrEmpty ( response ) ) ;
5169 }
52-
53- metricServer . Stop ( ) ;
70+ catch ( Exception ex )
71+ {
72+ _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
73+ throw ;
74+ }
75+ finally
76+ {
77+ metricServer . Stop ( ) ;
78+ }
5479 }
5580
5681 [ Fact ]
@@ -70,52 +95,79 @@ public async Task MapPath(string mapPath)
7095 var metricServer = new MetricServer (
7196 new CollectorRegistry ( ) ,
7297 new MetricServerOptions { Port = _port , MapPath = mapPath } ) ;
73- metricServer . Start ( ) ;
74-
75- using ( var httpClient = new HttpClient ( ) )
98+ try
7699 {
100+ metricServer . Start ( ) ;
101+ var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
102+ counter . Inc ( ) ;
103+ using var httpClient = new HttpClient ( ) ;
77104 var response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } " + mapPath ) ;
78105 Assert . False ( string . IsNullOrEmpty ( response ) ) ;
79106 }
80-
81- metricServer . Stop ( ) ;
107+ catch ( Exception ex )
108+ {
109+ _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
110+ throw ;
111+ }
112+ finally
113+ {
114+ metricServer . Stop ( ) ;
115+ }
82116 }
83117
84118 [ Fact ]
85119 public async Task FindMetric ( )
86120 {
87121 var registry = new CollectorRegistry ( ) ;
88- var factory = new MetricFactory ( registry ) ;
122+ var factory = new MetricFactory ( new CollectorRegistry ( ) ) ;
89123 var metricServer = new MetricServer ( registry , new MetricServerOptions { Port = _port } ) ;
90124
91- metricServer . Start ( ) ;
125+ try
126+ {
127+ metricServer . Start ( ) ;
92128
93- const string metricName = "myCounter" ;
94- var counter = factory . CreateCounter ( metricName , "helptext" ) ;
95- counter . Inc ( ) ;
129+ const string metricName = "myCounter" ;
130+ var counter = factory . CreateCounter ( metricName , "helptext" ) ;
131+ counter . Inc ( ) ;
96132
97- using ( var httpClient = new HttpClient ( ) )
98- {
133+ using var httpClient = new HttpClient ( ) ;
99134 var response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /metrics") ;
100135 Assert . Contains ( metricName , response ) ;
101136 }
102-
103- metricServer . Stop ( ) ;
137+ catch ( Exception ex )
138+ {
139+ _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
140+ throw ;
141+ }
142+ finally
143+ {
144+ metricServer . Stop ( ) ;
145+ }
104146 }
105147
106148 [ Fact ]
107149 public async Task Url_NotFound ( )
108150 {
109151 var metricServer = new MetricServer ( new CollectorRegistry ( ) , new MetricServerOptions { Port = _port } ) ;
110- metricServer . Start ( ) ;
111-
112- using ( var httpClient = new HttpClient ( ) )
152+ try
113153 {
154+ metricServer . Start ( ) ;
155+ var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
156+ counter . Inc ( ) ;
157+ using var httpClient = new HttpClient ( ) ;
158+
114159 var response = await httpClient . GetAsync ( $ "http://localhost:{ _port } ") ;
115160 Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
116161 }
117-
118- metricServer . Stop ( ) ;
162+ catch ( Exception ex )
163+ {
164+ _testOutputHelper . WriteLine ( ex . ToString ( ) ) ;
165+ throw ;
166+ }
167+ finally
168+ {
169+ metricServer . Stop ( ) ;
170+ }
119171 }
120172 }
121173}
0 commit comments