Skip to content

Commit 32bd2bc

Browse files
committed
Fix: ConcurrentDictionary as enum flasg cache
1 parent b2751d7 commit 32bd2bc

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/CodeOfChaos.Extensions/EnumExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using System.Collections.Concurrent;
5+
46
// ReSharper disable once CheckNamespace
57
namespace System;
68
// ---------------------------------------------------------------------------------------------------------------------
79
// Code
810
// ---------------------------------------------------------------------------------------------------------------------
911
public static class EnumExtensions {
10-
private static readonly Dictionary<Type, Array> EnumValuesCache = new();
12+
private static readonly ConcurrentDictionary<Type, Array> EnumValuesCache = new();
1113

1214
/// <summary>
1315
/// Retrieves all values of the specified enum type from the cache, falling back to reflection if uncached.
1416
/// </summary>
1517
private static IEnumerable<T> GetEnumValues<T>() where T : struct, Enum {
16-
if (EnumValuesCache.TryGetValue(typeof(T), out var values)) return (T[])values;
18+
if (EnumValuesCache.TryGetValue(typeof(T), out Array? values)) return (T[])values;
1719

1820
values = Enum.GetValues<T>();
1921
EnumValuesCache[typeof(T)] = values;

tests/Tests.CodeOfChaos.Extensions/StringExtensionTests.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using TUnit.Core.Exceptions;
5+
46
namespace Tests.CodeOfChaos.Extensions;
57

68
// ---------------------------------------------------------------------------------------------------------------------
@@ -105,9 +107,33 @@ public async Task ToGuid_ShouldThrowException_WhenInputIsInvalid(string input) {
105107
// Arrange
106108

107109
// Act
108-
Func<Guid> act = input.ToGuid;
109110

110111
// Assert
111-
await Assert.That(act).Throws<FormatException>();
112+
Assert.Throws<TUnitException>(() => input.ToGuid());
113+
}
114+
115+
116+
[Test]
117+
[Arguments("test", "00000000-0000-0000-0000-000000000000")]
118+
public async Task ToGuidAsHash_ShouldReturnSameGuid(string input, string expectedGuidString) {
119+
120+
121+
}
122+
123+
[Test]
124+
[Arguments("test")]
125+
[Arguments("some_data")]
126+
[Arguments("1234")]
127+
[Arguments("")]
128+
public async Task ToGuidAsHash_ShouldReturnSameGuid_ForSameString(string input) {
129+
// Arrange
130+
131+
// Act
132+
Guid guid1 = input.ToGuidAsHashed();
133+
Guid guid2 = input.ToGuidAsHashed();
134+
135+
// Assert
136+
await Assert.That(guid1).IsEqualTo(guid2);
137+
await Assert.That(guid2).IsEqualTo(guid1);
112138
}
113139
}

0 commit comments

Comments
 (0)