From 7b9ea132336b4a70003e803ee087db19a6e84a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= <2493377+askpt@users.noreply.github.com> Date: Fri, 20 Dec 2024 13:29:59 +0000 Subject: [PATCH] Fixing unit tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com> --- .../OpenFeature.Tests/OpenFeatureHookTests.cs | 70 ++++++++++--------- test/OpenFeature.Tests/TestImplementations.cs | 1 + 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/test/OpenFeature.Tests/OpenFeatureHookTests.cs b/test/OpenFeature.Tests/OpenFeatureHookTests.cs index cc8b08a1..48de5ee5 100644 --- a/test/OpenFeature.Tests/OpenFeatureHookTests.cs +++ b/test/OpenFeature.Tests/OpenFeatureHookTests.cs @@ -45,10 +45,10 @@ public async Task Hooks_Should_Be_Called_In_Order() invocationHook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); clientHook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); apiHook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); - providerHook.FinallyAsync(Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); - invocationHook.FinallyAsync(Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); - clientHook.FinallyAsync(Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); - apiHook.FinallyAsync(Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); + providerHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); + invocationHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); + clientHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); + apiHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()).Returns(new ValueTask()); var testProvider = new TestProvider(); testProvider.AddHook(providerHook); @@ -70,10 +70,10 @@ await client.GetBooleanValueAsync(flagName, defaultValue, EvaluationContext.Empt invocationHook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); clientHook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); apiHook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - providerHook.FinallyAsync(Arg.Any>(), Arg.Any>()); - invocationHook.FinallyAsync(Arg.Any>(), Arg.Any>()); - clientHook.FinallyAsync(Arg.Any>(), Arg.Any>()); - apiHook.FinallyAsync(Arg.Any>(), Arg.Any>()); + providerHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); + invocationHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); + clientHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); + apiHook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); }); _ = apiHook.Received(1).BeforeAsync(Arg.Any>(), Arg.Any>()); @@ -84,10 +84,10 @@ await client.GetBooleanValueAsync(flagName, defaultValue, EvaluationContext.Empt _ = invocationHook.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); _ = clientHook.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); _ = apiHook.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - _ = providerHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>()); - _ = invocationHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>()); - _ = clientHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>()); - _ = apiHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>()); + _ = providerHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); + _ = invocationHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); + _ = clientHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); + _ = apiHook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); } [Fact] @@ -239,10 +239,12 @@ public async Task Hook_Should_Return_No_Errors() }; var hookContext = new HookContext("test", false, FlagValueType.Boolean, new ClientMetadata(null, null), new Metadata(null), EvaluationContext.Empty); + var evaluationDetails = + new FlagEvaluationDetails("test", false, ErrorType.None, "testing", "testing"); await hook.BeforeAsync(hookContext, hookHints); - await hook.AfterAsync(hookContext, new FlagEvaluationDetails("test", false, ErrorType.None, "testing", "testing"), hookHints); - await hook.FinallyAsync(hookContext, hookHints); + await hook.AfterAsync(hookContext, evaluationDetails, hookHints); + await hook.FinallyAsync(hookContext, evaluationDetails, hookHints); await hook.ErrorAsync(hookContext, new Exception(), hookHints); hookContext.ClientMetadata.Name.Should().BeNull(); @@ -269,7 +271,7 @@ public async Task Hook_Should_Execute_In_Correct_Order() hook.BeforeAsync(Arg.Any>(), Arg.Any>()).Returns(EvaluationContext.Empty); featureProvider.ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns(new ResolutionDetails("test", false)); _ = hook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - _ = hook.FinallyAsync(Arg.Any>(), Arg.Any>()); + _ = hook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); await Api.Instance.SetProviderAsync(featureProvider); var client = Api.Instance.GetClient(); @@ -282,12 +284,12 @@ public async Task Hook_Should_Execute_In_Correct_Order() hook.BeforeAsync(Arg.Any>(), Arg.Any>()); featureProvider.ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any()); hook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - hook.FinallyAsync(Arg.Any>(), Arg.Any>()); + hook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); }); _ = hook.Received(1).BeforeAsync(Arg.Any>(), Arg.Any>()); _ = hook.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>()); + _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); _ = featureProvider.Received(1).ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any()); } @@ -331,8 +333,8 @@ public async Task Finally_Hook_Should_Be_Executed_Even_If_Abnormal_Termination() featureProvider.ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns(new ResolutionDetails("test", false)); hook2.AfterAsync(Arg.Any>(), Arg.Any>(), null).Returns(new ValueTask()); hook1.AfterAsync(Arg.Any>(), Arg.Any>(), null).Returns(new ValueTask()); - hook2.FinallyAsync(Arg.Any>(), null).Returns(new ValueTask()); - hook1.FinallyAsync(Arg.Any>(), null).Throws(new Exception()); + hook2.FinallyAsync(Arg.Any>(), Arg.Any>(), null).Returns(new ValueTask()); + hook1.FinallyAsync(Arg.Any>(), Arg.Any>(), null).Throws(new Exception()); await Api.Instance.SetProviderAsync(featureProvider); var client = Api.Instance.GetClient(); @@ -348,8 +350,8 @@ public async Task Finally_Hook_Should_Be_Executed_Even_If_Abnormal_Termination() featureProvider.ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any()); hook2.AfterAsync(Arg.Any>(), Arg.Any>(), null); hook1.AfterAsync(Arg.Any>(), Arg.Any>(), null); - hook2.FinallyAsync(Arg.Any>(), null); - hook1.FinallyAsync(Arg.Any>(), null); + hook2.FinallyAsync(Arg.Any>(), Arg.Any>(), null); + hook1.FinallyAsync(Arg.Any>(), Arg.Any>(), null); }); _ = hook1.Received(1).BeforeAsync(Arg.Any>(), null); @@ -357,8 +359,8 @@ public async Task Finally_Hook_Should_Be_Executed_Even_If_Abnormal_Termination() _ = featureProvider.Received(1).ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any()); _ = hook2.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), null); _ = hook1.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), null); - _ = hook2.Received(1).FinallyAsync(Arg.Any>(), null); - _ = hook1.Received(1).FinallyAsync(Arg.Any>(), null); + _ = hook2.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), null); + _ = hook1.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), null); } [Fact] @@ -458,7 +460,7 @@ public async Task Hook_Hints_May_Be_Optional() hook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()) .Returns(new ValueTask()); - hook.FinallyAsync(Arg.Any>(), Arg.Any>()) + hook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()) .Returns(new ValueTask()); await Api.Instance.SetProviderAsync(featureProvider); @@ -471,7 +473,7 @@ public async Task Hook_Hints_May_Be_Optional() hook.Received().BeforeAsync(Arg.Any>(), Arg.Any>()); featureProvider.Received().ResolveBooleanValueAsync("test", false, Arg.Any()); hook.Received().AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - hook.Received().FinallyAsync(Arg.Any>(), Arg.Any>()); + hook.Received().FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); }); } @@ -489,7 +491,7 @@ public async Task When_Error_Occurs_In_Before_Hook_Should_Return_Default_Value() // Sequence hook.BeforeAsync(Arg.Any>(), Arg.Any>()).Throws(exceptionToThrow); hook.ErrorAsync(Arg.Any>(), Arg.Any(), null).Returns(new ValueTask()); - hook.FinallyAsync(Arg.Any>(), null).Returns(new ValueTask()); + hook.FinallyAsync(Arg.Any>(), Arg.Any>(), null).Returns(new ValueTask()); var client = Api.Instance.GetClient(); client.AddHooks(hook); @@ -500,13 +502,13 @@ public async Task When_Error_Occurs_In_Before_Hook_Should_Return_Default_Value() { hook.BeforeAsync(Arg.Any>(), Arg.Any>()); hook.ErrorAsync(Arg.Any>(), Arg.Any(), null); - hook.FinallyAsync(Arg.Any>(), null); + hook.FinallyAsync(Arg.Any>(), Arg.Any>(), null); }); resolvedFlag.Should().BeTrue(); _ = hook.Received(1).BeforeAsync(Arg.Any>(), null); _ = hook.Received(1).ErrorAsync(Arg.Any>(), exceptionToThrow, null); - _ = hook.Received(1).FinallyAsync(Arg.Any>(), null); + _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), null); } [Fact] @@ -536,7 +538,7 @@ public async Task When_Error_Occurs_In_After_Hook_Should_Invoke_Error_Hook() hook.ErrorAsync(Arg.Any>(), Arg.Any(), Arg.Any>()) .Returns(new ValueTask()); - hook.FinallyAsync(Arg.Any>(), Arg.Any>()) + hook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()) .Returns(new ValueTask()); await Api.Instance.SetProviderAsync(featureProvider); @@ -550,7 +552,7 @@ public async Task When_Error_Occurs_In_After_Hook_Should_Invoke_Error_Hook() { hook.Received(1).BeforeAsync(Arg.Any>(), Arg.Any>()); hook.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); - hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>()); + hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()); }); await featureProvider.DidNotReceive().ResolveBooleanValueAsync("test", false, Arg.Any()); @@ -569,7 +571,7 @@ public async Task Successful_Resolution_Should_Pass_Cancellation_Token() hook.BeforeAsync(Arg.Any>(), Arg.Any>(), cts.Token).Returns(EvaluationContext.Empty); featureProvider.ResolveBooleanValueAsync(Arg.Any(), Arg.Any(), Arg.Any(), cts.Token).Returns(new ResolutionDetails("test", false)); _ = hook.AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>(), cts.Token); - _ = hook.FinallyAsync(Arg.Any>(), Arg.Any>(), cts.Token); + _ = hook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>(), cts.Token); await Api.Instance.SetProviderAsync(featureProvider); var client = Api.Instance.GetClient(); @@ -579,7 +581,7 @@ public async Task Successful_Resolution_Should_Pass_Cancellation_Token() _ = hook.Received(1).BeforeAsync(Arg.Any>(), Arg.Any>(), cts.Token); _ = hook.Received(1).AfterAsync(Arg.Any>(), Arg.Any>(), Arg.Any>(), cts.Token); - _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), cts.Token); + _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>(), cts.Token); } [Fact] @@ -606,7 +608,7 @@ public async Task Failed_Resolution_Should_Pass_Cancellation_Token() hook.ErrorAsync(Arg.Any>(), Arg.Any(), Arg.Any>()) .Returns(new ValueTask()); - hook.FinallyAsync(Arg.Any>(), Arg.Any>()) + hook.FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>()) .Returns(new ValueTask()); await Api.Instance.SetProviderAsync(featureProvider); @@ -616,7 +618,7 @@ public async Task Failed_Resolution_Should_Pass_Cancellation_Token() _ = hook.Received(1).BeforeAsync(Arg.Any>(), Arg.Any>(), cts.Token); _ = hook.Received(1).ErrorAsync(Arg.Any>(), Arg.Any(), Arg.Any>(), cts.Token); - _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), cts.Token); + _ = hook.Received(1).FinallyAsync(Arg.Any>(), Arg.Any>(), Arg.Any>(), cts.Token); await featureProvider.DidNotReceive().ResolveBooleanValueAsync("test", false, Arg.Any()); } diff --git a/test/OpenFeature.Tests/TestImplementations.cs b/test/OpenFeature.Tests/TestImplementations.cs index aa4dc784..724278e8 100644 --- a/test/OpenFeature.Tests/TestImplementations.cs +++ b/test/OpenFeature.Tests/TestImplementations.cs @@ -48,6 +48,7 @@ public override ValueTask ErrorAsync(HookContext context, Exception error, } public override ValueTask FinallyAsync(HookContext context, + FlagEvaluationDetails evaluationDetails, IReadOnlyDictionary? hints = null, CancellationToken cancellationToken = default) { Interlocked.Increment(ref this._finallyCallCount);