Skip to content

Commit 6fd43e9

Browse files
committed
Add test from prometheus-net
1 parent c306c91 commit 6fd43e9

File tree

11 files changed

+252
-213
lines changed

11 files changed

+252
-213
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Sergey Kuznetsov
3+
Copyright (c) 2017 Sergey Kuznetsov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using Prometheus.Advanced.DataContracts;
6+
using Prometheus.Client.Internal;
7+
using Xunit;
8+
9+
namespace Prometheus.Client.Tests
10+
{
11+
public sealed class AsciiFormatterTests
12+
{
13+
[Theory]
14+
[InlineData("simple-label-value-1")]
15+
[InlineData("with\nlinebreaks")]
16+
[InlineData("with\nlinebreaks and \\slashes and quotes \"")]
17+
public void family_should_be_formatted_to_one_line(string labelValue)
18+
{
19+
using (var ms = new MemoryStream())
20+
{
21+
var metricFamily = new MetricFamily
22+
{
23+
name = "family1",
24+
help = "help",
25+
type = MetricType.COUNTER,
26+
};
27+
28+
var metricCounter = new Prometheus.Advanced.DataContracts.Counter { value = 100 };
29+
metricFamily.metric.Add(new Metric
30+
{
31+
counter = metricCounter,
32+
label = new List<LabelPair>
33+
{
34+
new LabelPair {name = "label1", value = labelValue }
35+
}
36+
});
37+
38+
AsciiFormatter.Format(ms, new[]
39+
{
40+
metricFamily
41+
});
42+
43+
using (var sr = new StringReader(Encoding.UTF8.GetString(ms.ToArray())))
44+
{
45+
var linesCount = 0;
46+
var line = "";
47+
while ((line = sr.ReadLine()) != null)
48+
{
49+
Console.WriteLine(line);
50+
linesCount += 1;
51+
}
52+
Assert.Equal(3, linesCount);
53+
}
54+
}
55+
}
56+
}
57+
}

Prometheus.Client.Tests/SummaryBenchmarks.cs

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Prometheus.Client.Advanced;
2+
using Xunit;
3+
4+
namespace Prometheus.Client.Tests
5+
{
6+
public class ThreadSafeDoubleTests
7+
{
8+
[Fact]
9+
public void ThreadSafeDouble_Constructors()
10+
{
11+
var tsdouble = new ThreadSafeDouble();
12+
Assert.Equal(0.0, tsdouble.Value);
13+
14+
tsdouble = new ThreadSafeDouble(1.42);
15+
Assert.Equal(1.42, tsdouble.Value);
16+
}
17+
18+
[Fact]
19+
public void ThreadSafeDouble_ValueSet()
20+
{
21+
var tsdouble = new ThreadSafeDouble();
22+
tsdouble.Value = 3.14;
23+
Assert.Equal(3.14, tsdouble.Value);
24+
}
25+
26+
[Fact]
27+
public void ThreadSafeDouble_Overrides()
28+
{
29+
var tsdouble = new ThreadSafeDouble(9.15);
30+
var equaltsdouble = new ThreadSafeDouble(9.15);
31+
var notequaltsdouble = new ThreadSafeDouble(10.11);
32+
33+
Assert.Equal("9.15", tsdouble.ToString());
34+
Assert.True(tsdouble.Equals(equaltsdouble));
35+
Assert.False(tsdouble.Equals(notequaltsdouble));
36+
Assert.False(tsdouble.Equals(null));
37+
Assert.True(tsdouble.Equals(9.15));
38+
Assert.False(tsdouble.Equals(10.11));
39+
40+
Assert.Equal((9.15).GetHashCode(), tsdouble.GetHashCode());
41+
}
42+
43+
[Fact]
44+
public void ThreadSafeDouble_Add()
45+
{
46+
var tsdouble = new ThreadSafeDouble(3.10);
47+
tsdouble.Add(0.50);
48+
tsdouble.Add(2.00);
49+
Assert.Equal(5.6, tsdouble.Value);
50+
}
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Prometheus.Client.Advanced;
2+
using Xunit;
3+
4+
namespace Prometheus.Client.Tests
5+
{
6+
public class ThreadSafeLongTests
7+
{
8+
[Fact]
9+
public void ThreadSafeLong_Constructors()
10+
{
11+
var tsdouble = new ThreadSafeLong();
12+
Assert.Equal(0L, tsdouble.Value);
13+
14+
tsdouble = new ThreadSafeLong(1L);
15+
Assert.Equal(1L, tsdouble.Value);
16+
}
17+
18+
[Fact]
19+
public void ThreadSafeLong_ValueSet()
20+
{
21+
var tsdouble = new ThreadSafeLong();
22+
tsdouble.Value = 3L;
23+
Assert.Equal(3L, tsdouble.Value);
24+
}
25+
26+
[Fact]
27+
public void ThreadSafeLong_Overrides()
28+
{
29+
var tsdouble = new ThreadSafeLong(9L);
30+
var equaltsdouble = new ThreadSafeLong(9L);
31+
var notequaltsdouble = new ThreadSafeLong(10L);
32+
33+
Assert.Equal("9", tsdouble.ToString());
34+
Assert.True(tsdouble.Equals(equaltsdouble));
35+
Assert.False(tsdouble.Equals(notequaltsdouble));
36+
Assert.False(tsdouble.Equals(null));
37+
Assert.True(tsdouble.Equals(9L));
38+
Assert.False(tsdouble.Equals(10L));
39+
40+
Assert.Equal((9L).GetHashCode(), tsdouble.GetHashCode());
41+
}
42+
43+
[Fact]
44+
public void ThreadSafeLong_Add()
45+
{
46+
var tsdouble = new ThreadSafeLong(3L);
47+
tsdouble.Add(2L);
48+
tsdouble.Add(5L);
49+
Assert.Equal(10L, tsdouble.Value);
50+
}
51+
}
52+
}

Prometheus.Client.Tests/project.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
"dependencies": {
99
"dotnet-test-xunit": "2.2.0-preview2-build1029",
10+
"Microsoft.NETCore.App": "1.1.0",
1011
"NETStandard.Library": "1.6.1",
1112
"Prometheus.Client": {
1213
"target": "project"
1314
},
14-
"xunit": "2.2.0-beta2-build3300"
15+
"xunit": "2.2.0"
1516
},
1617

1718
"testRunner": "xunit",
@@ -20,7 +21,7 @@
2021
"dependencies": {
2122
"Microsoft.NETCore.App": {
2223
"type": "platform",
23-
"version": "1.0.1"
24+
"version": "1.1.0"
2425
}
2526
},
2627
"imports": [

Prometheus.Client/Advanced/ThreadSafeDouble.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Threading;
34

45
namespace Prometheus.Client.Advanced
@@ -38,7 +39,7 @@ public void Add(double increment)
3839

3940
public override string ToString()
4041
{
41-
return Value.ToString();
42+
return Value.ToString(CultureInfo.InvariantCulture);
4243
}
4344

4445
public override bool Equals(object obj)

Prometheus.Client/Advanced/ThreadSafeLong.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading;
1+
using System.Globalization;
2+
using System.Threading;
23

34
namespace Prometheus.Client.Advanced
45
{
@@ -37,7 +38,7 @@ public void Add(long increment)
3738

3839
public override string ToString()
3940
{
40-
return Value.ToString();
41+
return Value.ToString(CultureInfo.InvariantCulture);
4142
}
4243

4344
public override bool Equals(object obj)

Prometheus.Client/Internal/AsciiFormatter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
using System.Globalization;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.CompilerServices;
56
using System.Text;
67
using Prometheus.Advanced.DataContracts;
78

9+
[assembly: InternalsVisibleTo("Prometheus.Client.Tests")]
10+
811
namespace Prometheus.Client.Internal
912
{
13+
1014
internal class AsciiFormatter
1115
{
1216
public static void Format(Stream destination, IEnumerable<MetricFamily> metrics)

0 commit comments

Comments
 (0)