From ef289d3f82be4cbceb73b2157861794ab1c65605 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Tue, 9 Jan 2024 09:46:22 +0100 Subject: [PATCH] set shutdown delegate to noop in unit tests Signed-off-by: Florian Bacher --- src/OpenFeature/Api.cs | 2 +- src/OpenFeature/EventExecutor.cs | 22 ++++++++++++++++--- .../OpenFeatureEventTests.cs | 2 +- test/OpenFeature.Tests/OpenFeatureTests.cs | 8 +++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/OpenFeature/Api.cs b/src/OpenFeature/Api.cs index ccf22351..4226a499 100644 --- a/src/OpenFeature/Api.cs +++ b/src/OpenFeature/Api.cs @@ -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); } /// diff --git a/src/OpenFeature/EventExecutor.cs b/src/OpenFeature/EventExecutor.cs index 788896a1..b883b9a9 100644 --- a/src/OpenFeature/EventExecutor.cs +++ b/src/OpenFeature/EventExecutor.cs @@ -9,6 +9,9 @@ namespace OpenFeature { + + internal delegate Task ShutdownDelegate(); + internal class EventExecutor { private readonly object _lockObj = new object(); @@ -18,11 +21,14 @@ internal class EventExecutor private readonly List _activeSubscriptions = new List(); private readonly SemaphoreSlim _shutdownSemaphore = new SemaphoreSlim(0); + private ShutdownDelegate _shutdownDelegate; + private readonly Dictionary> _apiHandlers = new Dictionary>(); private readonly Dictionary>> _clientHandlers = new Dictionary>>(); public EventExecutor() { + this._shutdownDelegate = this.SignalShutdownAsync; var eventProcessing = new Thread(this.ProcessEventAsync); eventProcessing.Start(); } @@ -206,9 +212,9 @@ private void EmitOnRegistration(FeatureProviderReference provider, ProviderEvent Message = message }); } - catch (Exception) + catch (Exception exc) { - throw; + Console.WriteLine(exc); } } } @@ -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); diff --git a/test/OpenFeature.Tests/OpenFeatureEventTests.cs b/test/OpenFeature.Tests/OpenFeatureEventTests.cs index ff1368d5..e660789b 100644 --- a/test/OpenFeature.Tests/OpenFeatureEventTests.cs +++ b/test/OpenFeature.Tests/OpenFeatureEventTests.cs @@ -34,7 +34,7 @@ public async Task Event_Executor_Should_Propagate_Events_ToGlobal_Handler() eventHandler.Received().Invoke(Arg.Is(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 }; diff --git a/test/OpenFeature.Tests/OpenFeatureTests.cs b/test/OpenFeature.Tests/OpenFeatureTests.cs index 28ba9f42..3e8b4d3d 100644 --- a/test/OpenFeature.Tests/OpenFeatureTests.cs +++ b/test/OpenFeature.Tests/OpenFeatureTests.cs @@ -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() @@ -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(); providerA.GetStatus().Returns(ProviderStatus.NotReady);