Skip to content

Commit 7b15337

Browse files
committed
test: update tests, add try catch
1 parent 2a2a6fe commit 7b15337

File tree

5 files changed

+96
-42
lines changed

5 files changed

+96
-42
lines changed

Prometheus.Client.MetricServer.sln

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prometheus.Client.MetricSer
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{465CE32E-016E-46A1-9636-3411CB742385}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prometheus.Client.MetricServer.NetCore.Tests", "tests\Prometheus.Client.MetricServer.NetCore.Tests\Prometheus.Client.MetricServer.NetCore.Tests.csproj", "{7DC9CABA-8C8B-4ADD-BF3F-D67BB580027A}"
13-
EndProject
1412
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7ADF00AE-7F6D-4C7E-B0F0-B2A19627DDF8}"
1513
ProjectSection(SolutionItems) = preProject
1614
.editorconfig = .editorconfig
@@ -23,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2321
stylecop.json = stylecop.json
2422
EndProjectSection
2523
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prometheus.Client.MetricServer.Tests", "tests\Prometheus.Client.MetricServer.Tests\Prometheus.Client.MetricServer.Tests.csproj", "{21EE82C6-BFDD-4DDD-A49F-2543F44B4E70}"
25+
EndProject
2626
Global
2727
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2828
Debug|Any CPU = Debug|Any CPU
@@ -33,17 +33,17 @@ Global
3333
{C77F577A-8639-4A63-966D-80C174B3D999}.Debug|Any CPU.Build.0 = Debug|Any CPU
3434
{C77F577A-8639-4A63-966D-80C174B3D999}.Release|Any CPU.ActiveCfg = Release|Any CPU
3535
{C77F577A-8639-4A63-966D-80C174B3D999}.Release|Any CPU.Build.0 = Release|Any CPU
36-
{7DC9CABA-8C8B-4ADD-BF3F-D67BB580027A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37-
{7DC9CABA-8C8B-4ADD-BF3F-D67BB580027A}.Debug|Any CPU.Build.0 = Debug|Any CPU
38-
{7DC9CABA-8C8B-4ADD-BF3F-D67BB580027A}.Release|Any CPU.ActiveCfg = Release|Any CPU
39-
{7DC9CABA-8C8B-4ADD-BF3F-D67BB580027A}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{21EE82C6-BFDD-4DDD-A49F-2543F44B4E70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{21EE82C6-BFDD-4DDD-A49F-2543F44B4E70}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{21EE82C6-BFDD-4DDD-A49F-2543F44B4E70}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{21EE82C6-BFDD-4DDD-A49F-2543F44B4E70}.Release|Any CPU.Build.0 = Release|Any CPU
4040
EndGlobalSection
4141
GlobalSection(SolutionProperties) = preSolution
4242
HideSolutionNode = FALSE
4343
EndGlobalSection
4444
GlobalSection(NestedProjects) = preSolution
4545
{C77F577A-8639-4A63-966D-80C174B3D999} = {8E29CEE6-355D-427A-8EDA-F2A1FE60BC3B}
46-
{7DC9CABA-8C8B-4ADD-BF3F-D67BB580027A} = {465CE32E-016E-46A1-9636-3411CB742385}
46+
{21EE82C6-BFDD-4DDD-A49F-2543F44B4E70} = {465CE32E-016E-46A1-9636-3411CB742385}
4747
EndGlobalSection
4848
GlobalSection(ExtensibilityGlobals) = postSolution
4949
SolutionGuid = {6CA14866-8A1E-4C8C-AE98-A806F33741EF}

src/Prometheus.Client.MetricServer/IMetricServer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Threading.Tasks;
2+
13
namespace Prometheus.Client.MetricServer
24
{
35
/// <summary>

src/Prometheus.Client.MetricServer/MetricServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Net;
33
using System.Reflection;
4+
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Hosting;
67
using Microsoft.Extensions.Configuration;

tests/Prometheus.Client.MetricServer.NetCore.Tests/MetricServerTests.cs renamed to tests/Prometheus.Client.MetricServer.Tests/MetricServerTests.cs

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
using System.Threading.Tasks;
55
using Prometheus.Client.Collectors;
66
using 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
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.2</TargetFramework>
3+
<TargetFramework>netcoreapp3.1</TargetFramework>
44
<IsPackable>false</IsPackable>
55
<SignAssembly>true</SignAssembly>
66
<AssemblyOriginatorKeyFile>../../Prometheus.Client.MetricServer.snk</AssemblyOriginatorKeyFile>
77
</PropertyGroup>
88
<ItemGroup>
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
1010
<PackageReference Include="xunit" Version="2.4.1" />
11-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
12-
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
11+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
1312
</ItemGroup>
1413
<ItemGroup>
1514
<ProjectReference Include="..\..\src\Prometheus.Client.MetricServer\Prometheus.Client.MetricServer.csproj" />

0 commit comments

Comments
 (0)