Skip to content

Commit 3507cb8

Browse files
authored
Refactor and unit test for ToSnakeCase extension method (#42)
* Refactor and unit test for ToSnakeCase extension method * Code cleanup * Lock contention assertion should be GreaterThanOrEqualTo instead of EqualTo
1 parent 15fb9b4 commit 3507cb8

File tree

8 files changed

+30
-29
lines changed

8 files changed

+30
-29
lines changed

src/prometheus-net.DotNetRuntime.Tests/StatsCollectors/IntegrationTests/ContentionStatsCollectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task Will_measure_contention_on_a_contested_lock()
6363

6464
// Why -1? The first thread will not contend the lock
6565
const int numLocksContended = numThreads - 1;
66-
Assert.That(() => StatsCollector.ContentionTotal.Value, Is.EqualTo(numLocksContended).After(200, 10));
66+
Assert.That(() => StatsCollector.ContentionTotal.Value, Is.GreaterThanOrEqualTo(numLocksContended).After(200, 10));
6767

6868
// Pattern of expected contention times is: 50ms, 100ms, 150ms, etc.
6969
var expectedDelay = TimeSpan.FromMilliseconds(Enumerable.Range(1, numLocksContended).Aggregate(sleepForMs, (acc, next) => acc + (sleepForMs * next)));

src/prometheus-net.DotNetRuntime.Tests/StatsCollectors/IntegrationTests/ExceptionStatsCollectorTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using NUnit.Framework;
22
using Prometheus.DotNetRuntime.StatsCollectors;
33
using System;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
74

85
namespace Prometheus.DotNetRuntime.Tests.StatsCollectors.IntegrationTests
96
{
@@ -23,9 +20,9 @@ public void Will_measure_when_occurring_an_exception()
2320

2421
try
2522
{
26-
var result = 1 / divider;
23+
_ = 1 / divider;
2724
}
28-
catch (System.DivideByZeroException divZeroEx)
25+
catch (DivideByZeroException)
2926
{
3027
}
3128

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NUnit.Framework;
2+
using Prometheus.DotNetRuntime.StatsCollectors.Util;
3+
4+
namespace Prometheus.DotNetRuntime.Tests.StatsCollectors.Util
5+
{
6+
public class StringExtensionsTests
7+
{
8+
[TestCase("", "")]
9+
[TestCase("myGreatVariableName", "my_great_variable_name")]
10+
[TestCase("my_great_variable_name", "my_great_variable_name")]
11+
[TestCase("MyGreatVariableName", "my_great_variable_name")]
12+
public void ToSnakeCase_Should_Convert_To_Snake_Case(string given, string expected)
13+
{
14+
Assert.AreEqual(given.ToSnakeCase(), expected);
15+
}
16+
}
17+
}

src/prometheus-net.DotNetRuntime/StatsCollectors/Util/Cache.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ private void CleanupExpiredValues()
100100

101101
foreach (var key in _cache.Keys.ToArray())
102102
{
103-
CacheValue<TValue> value;
104-
if (!_cache.TryGetValue(key, out value))
103+
if (!_cache.TryGetValue(key, out var value))
105104
continue;
106105

107106
if (value.TimeStamp < earliestAddedTime)
108-
_cache.TryRemove(key, out var _);
107+
_cache.TryRemove(key, out _);
109108
}
110109
}
111110
}

src/prometheus-net.DotNetRuntime/StatsCollectors/Util/EventPairTimer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics.Tracing;
3-
using System.Runtime.CompilerServices;
43

54
namespace Prometheus.DotNetRuntime.StatsCollectors.Util
65
{
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54

65
namespace Prometheus.DotNetRuntime.StatsCollectors.Util
76
{
@@ -14,14 +13,8 @@ public class LabelGenerator
1413
public static Dictionary<TEnum, string> MapEnumToLabelValues<TEnum>()
1514
where TEnum : Enum
1615
{
17-
var dict = new Dictionary<TEnum, string>();
18-
19-
foreach (var v in Enum.GetValues(typeof(TEnum)).Cast<TEnum>())
20-
{
21-
dict.Add(v, Enum.GetName(typeof(TEnum), v).ToSnakeCase());
22-
}
23-
24-
return dict;
16+
return Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
17+
.ToDictionary(k => k, v => Enum.GetName(typeof(TEnum), v).ToSnakeCase());
2518
}
2619
}
2720
}

src/prometheus-net.DotNetRuntime/StatsCollectors/Util/SamplingRate.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Threading;
32

43
namespace Prometheus.DotNetRuntime.StatsCollectors.Util

src/prometheus-net.DotNetRuntime/StatsCollectors/Util/StringExtensions.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,25 @@ public static class StringExtensions
77
public static string ToSnakeCase(this string str)
88
{
99
var sb = new StringBuilder();
10-
bool lastCharWasUpper = false;
11-
bool isFirst = true;
10+
var lastCharWasUpper = false;
1211

13-
foreach (var c in str)
12+
for(var i = 0 ; i < str.Length ; i++)
1413
{
15-
if (char.IsUpper(c))
14+
if (char.IsUpper(str[i]))
1615
{
17-
if (!lastCharWasUpper && !isFirst)
16+
if (!lastCharWasUpper && i != 0)
1817
{
1918
sb.Append("_");
2019
}
2120

22-
sb.Append(char.ToLower(c));
21+
sb.Append(char.ToLower(str[i]));
2322
lastCharWasUpper = true;
2423
}
2524
else
2625
{
27-
sb.Append(c);
26+
sb.Append(str[i]);
2827
lastCharWasUpper = false;
2928
}
30-
31-
isFirst = false;
3229
}
3330

3431
return sb.ToString();

0 commit comments

Comments
 (0)