Skip to content

Commit 29fd9c1

Browse files
authored
refactor: improve code quality (#706)
* chore(tests): use 'using' declaration for CancellationTokenSource in test files Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com> * chore(EventExecutor): make _defaultClientName readonly Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com> * chore(InMemoryProvider): simplify flag initialization logic in constructor and UpdateFlagsAsync method Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com> * chore(EventExecutor): streamline event handler removal logic chore(LoggingHook): refine number value handling in logging Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com> --------- Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
1 parent 53152c3 commit 29fd9c1

File tree

7 files changed

+16
-30
lines changed

7 files changed

+16
-30
lines changed

src/OpenFeature/EventExecutor.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed partial class EventExecutor : IAsyncDisposable
1515
private readonly List<FeatureProvider> _activeSubscriptions = [];
1616

1717
/// placeholder for anonymous clients
18-
private static Guid _defaultClientName = Guid.NewGuid();
18+
private static readonly Guid _defaultClientName = Guid.NewGuid();
1919

2020
private readonly Dictionary<ProviderEventTypes, List<EventHandlerDelegate>> _apiHandlers = [];
2121
private readonly Dictionary<string, Dictionary<ProviderEventTypes, List<EventHandlerDelegate>>> _clientHandlers = [];
@@ -93,12 +93,10 @@ internal void RemoveClientHandler(string client, ProviderEventTypes type, EventH
9393

9494
lock (this._lockObj)
9595
{
96-
if (this._clientHandlers.TryGetValue(clientName, out var clientEventHandlers))
96+
if (this._clientHandlers.TryGetValue(clientName, out var clientEventHandlers)
97+
&& clientEventHandlers.TryGetValue(type, out var eventHandlers))
9798
{
98-
if (clientEventHandlers.TryGetValue(type, out var eventHandlers))
99-
{
100-
eventHandlers.Remove(handler);
101-
}
99+
eventHandlers.Remove(handler);
102100
}
103101
}
104102
}

src/OpenFeature/Hooks/LoggingHook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ public override string ToString()
153153
if (value.IsBoolean)
154154
return value.AsBoolean.ToString();
155155

156-
if (value.IsNumber)
156+
if (value.IsNumber && value.AsDouble != null)
157157
{
158158
// Value.AsDouble will attempt to cast other numbers to double
159159
// There is an implicit conversation for int/long to double
160-
if (value.AsDouble != null) return value.AsDouble.ToString();
160+
return value.AsDouble.ToString();
161161
}
162162

163163
if (value.IsDateTime)

src/OpenFeature/Providers/Memory/InMemoryProvider.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ public override Metadata GetMetadata()
2626
/// <param name="flags">dictionary of Flags</param>
2727
public InMemoryProvider(IDictionary<string, Flag>? flags = null)
2828
{
29-
if (flags == null)
30-
{
31-
this._flags = new Dictionary<string, Flag>();
32-
}
33-
else
34-
{
35-
this._flags = new Dictionary<string, Flag>(flags); // shallow copy
36-
}
29+
this._flags = flags == null ? [] : new Dictionary<string, Flag>(flags); // shallow copy
3730
}
3831

3932
/// <summary>
@@ -43,14 +36,8 @@ public InMemoryProvider(IDictionary<string, Flag>? flags = null)
4336
public async Task UpdateFlagsAsync(IDictionary<string, Flag>? flags = null)
4437
{
4538
var changed = this._flags.Keys.ToList();
46-
if (flags == null)
47-
{
48-
this._flags = new Dictionary<string, Flag>();
49-
}
50-
else
51-
{
52-
this._flags = new Dictionary<string, Flag>(flags); // shallow copy
53-
}
39+
this._flags = flags == null ? [] : new Dictionary<string, Flag>(flags); // shallow copy
40+
5441
changed.AddRange(this._flags.Keys.ToList());
5542
var @event = new ProviderEventPayload
5643
{

test/OpenFeature.Providers.MultiProvider.Tests/MultiProviderEventTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ private static async Task EmitEventToProvider(FeatureProvider provider, Provider
319319
private static async Task<List<ProviderEventPayload>> ReadEvents(Channel<object> channel, int expectedCount = 1, int timeoutMs = 1000)
320320
{
321321
var events = new List<ProviderEventPayload>();
322-
var cts = new CancellationTokenSource(timeoutMs);
322+
using var cts = new CancellationTokenSource(timeoutMs);
323323

324324
try
325325
{

test/OpenFeature.Providers.MultiProvider.Tests/ProviderExtensionsTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public async Task EvaluateAsync_WithCancellationToken_PassesToProvider()
220220
const string resolvedValue = "resolved";
221221
var expectedDetails = new ResolutionDetails<string>(TestFlagKey, resolvedValue, ErrorType.None, Reason.Static, TestVariant);
222222
var providerContext = new StrategyPerProviderContext<string>(this._mockProvider, TestProviderName, ProviderStatus.Ready, TestFlagKey);
223-
var customCancellationToken = new CancellationTokenSource().Token;
223+
using var cts = new CancellationTokenSource();
224+
var customCancellationToken = cts.Token;
224225

225226
this._mockProvider.ResolveStringValueAsync(TestFlagKey, defaultValue, this._evaluationContext, customCancellationToken)
226227
.Returns(expectedDetails);
@@ -283,7 +284,7 @@ public async Task EvaluateAsync_WhenOperationCancelled_ReturnsErrorResult()
283284
{
284285
// Arrange
285286
const bool defaultValue = false;
286-
var cancellationTokenSource = new CancellationTokenSource();
287+
using var cancellationTokenSource = new CancellationTokenSource();
287288
var providerContext = new StrategyPerProviderContext<bool>(this._mockProvider, TestProviderName, ProviderStatus.Ready, TestFlagKey);
288289

289290
this._mockProvider.ResolveBooleanValueAsync(TestFlagKey, defaultValue, this._evaluationContext, cancellationTokenSource.Token)

test/OpenFeature.Tests/OpenFeatureClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public async Task Cancellation_Token_Added_Is_Passed_To_Provider()
470470
var defaultString = fixture.Create<string>();
471471
var cancelledReason = "cancelled";
472472

473-
var cts = new CancellationTokenSource();
473+
using var cts = new CancellationTokenSource();
474474

475475

476476
var featureProviderMock = Substitute.For<FeatureProvider>();

test/OpenFeature.Tests/OpenFeatureHookTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ public async Task Successful_Resolution_Should_Pass_Cancellation_Token()
673673
{
674674
var featureProvider = Substitute.For<FeatureProvider>();
675675
var hook = Substitute.For<Hook>();
676-
var cts = new CancellationTokenSource();
676+
using var cts = new CancellationTokenSource();
677677

678678
featureProvider.GetMetadata().Returns(new Metadata(null));
679679
featureProvider.GetProviderHooks().Returns(ImmutableList<Hook>.Empty);
@@ -701,7 +701,7 @@ public async Task Failed_Resolution_Should_Pass_Cancellation_Token()
701701
var hook = Substitute.For<Hook>();
702702
var flagOptions = new FlagEvaluationOptions(hook);
703703
var exceptionToThrow = new GeneralException("Fake Exception");
704-
var cts = new CancellationTokenSource();
704+
using var cts = new CancellationTokenSource();
705705

706706
featureProvider.GetMetadata()
707707
.Returns(new Metadata(null));

0 commit comments

Comments
 (0)