Skip to content

Commit

Permalink
set shutdown delegate to noop in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl committed Jan 9, 2024
1 parent c30345a commit ef289d3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/OpenFeature/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public EvaluationContext GetContext()
public async Task Shutdown()
{
await this._repository.Shutdown().ConfigureAwait(false);
//await this.EventExecutor.SignalShutdownAsync().ConfigureAwait(false);
await this.EventExecutor.Shutdown().ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down
22 changes: 19 additions & 3 deletions src/OpenFeature/EventExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace OpenFeature
{

internal delegate Task ShutdownDelegate();

internal class EventExecutor
{
private readonly object _lockObj = new object();
Expand All @@ -18,11 +21,14 @@ internal class EventExecutor
private readonly List<FeatureProviderReference> _activeSubscriptions = new List<FeatureProviderReference>();
private readonly SemaphoreSlim _shutdownSemaphore = new SemaphoreSlim(0);

private ShutdownDelegate _shutdownDelegate;

private readonly Dictionary<ProviderEventTypes, List<EventHandlerDelegate>> _apiHandlers = new Dictionary<ProviderEventTypes, List<EventHandlerDelegate>>();
private readonly Dictionary<string, Dictionary<ProviderEventTypes, List<EventHandlerDelegate>>> _clientHandlers = new Dictionary<string, Dictionary<ProviderEventTypes, List<EventHandlerDelegate>>>();

public EventExecutor()
{
this._shutdownDelegate = this.SignalShutdownAsync;
var eventProcessing = new Thread(this.ProcessEventAsync);
eventProcessing.Start();
}
Expand Down Expand Up @@ -206,9 +212,9 @@ private void EmitOnRegistration(FeatureProviderReference provider, ProviderEvent
Message = message
});
}
catch (Exception)
catch (Exception exc)
{
throw;
Console.WriteLine(exc);
}
}
}
Expand Down Expand Up @@ -316,8 +322,18 @@ private void InvokeEventHandler(EventHandlerDelegate eventHandler, Event e)
}
}

public async Task Shutdown()
{
await this._shutdownDelegate().ConfigureAwait(false);
}

internal void SetShutdownDelegate(ShutdownDelegate del)
{
this._shutdownDelegate = del;
}

// Method to signal shutdown
public async Task SignalShutdownAsync()
private async Task SignalShutdownAsync()
{
// Enqueue a shutdown signal
await this.EventChannel.Writer.WriteAsync(new ShutdownSignal()).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion test/OpenFeature.Tests/OpenFeatureEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Event_Executor_Should_Propagate_Events_ToGlobal_Handler()
eventHandler.Received().Invoke(Arg.Is<ProviderEventPayload>(payload => payload.Type == ProviderEventTypes.ProviderReady));

// shut down the event executor
await eventExecutor.SignalShutdownAsync();
await eventExecutor.Shutdown();

// the next event should not be propagated to the event handler
var newEventPayload = new ProviderEventPayload { Type = ProviderEventTypes.ProviderStale };
Expand Down
8 changes: 8 additions & 0 deletions test/OpenFeature.Tests/OpenFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace OpenFeature.Tests
{
public class OpenFeatureTests : ClearOpenFeatureInstanceFixture
{
static async Task EmptyShutdown()
{
await Task.FromResult(0).ConfigureAwait(false);
}

[Fact]
[Specification("1.1.1", "The `API`, and any state it maintains SHOULD exist as a global singleton, even in cases wherein multiple versions of the `API` are present at runtime.")]
public void OpenFeature_Should_Be_Singleton()
Expand Down Expand Up @@ -74,6 +79,9 @@ public async Task OpenFeature_Should_Shutdown_Unused_Provider()
[Specification("1.6.1", "The API MUST define a mechanism to propagate a shutdown request to active providers.")]
public async Task OpenFeature_Should_Support_Shutdown()
{
// configure the shutdown method of the event executor to do nothing
// to prevent eventing tests from failing
Api.Instance.EventExecutor.SetShutdownDelegate(EmptyShutdown);
var providerA = Substitute.For<FeatureProvider>();
providerA.GetStatus().Returns(ProviderStatus.NotReady);

Expand Down

0 comments on commit ef289d3

Please sign in to comment.