From 74e3ca75e0c013b5f5cd821f25eaa3cb8f08ffc5 Mon Sep 17 00:00:00 2001 From: Lilian Kasem Date: Wed, 6 Nov 2024 09:35:50 -0800 Subject: [PATCH] Return metadata provider task whether it is successful or not (#10598) --- .../Host/FunctionMetadataManager.cs | 2 +- .../FunctionMetadataManagerTests.cs | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/WebJobs.Script/Host/FunctionMetadataManager.cs b/src/WebJobs.Script/Host/FunctionMetadataManager.cs index 99f4169518..98ea2d789a 100644 --- a/src/WebJobs.Script/Host/FunctionMetadataManager.cs +++ b/src/WebJobs.Script/Host/FunctionMetadataManager.cs @@ -236,7 +236,7 @@ private void AddMetadataFromCustomProviders(IEnumerable funct var completedTask = Task.WhenAny(getFunctionMetadataFromProviderTask, delayTask).ContinueWith(t => { - if (t.Result == getFunctionMetadataFromProviderTask && getFunctionMetadataFromProviderTask.IsCompletedSuccessfully) + if (t.Result == getFunctionMetadataFromProviderTask) { return getFunctionMetadataFromProviderTask.Result; } diff --git a/test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs b/test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs index 04a5187cb0..b8bd98a3c4 100644 --- a/test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs +++ b/test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs @@ -7,6 +7,7 @@ using System.Collections.ObjectModel; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.WebJobs.Script.Description; using Microsoft.Azure.WebJobs.Script.Workers.Http; @@ -222,6 +223,34 @@ public void FunctionMetadataManager_LoadFunctionMetadata_Throws_WhenFunctionProv Assert.DoesNotContain(traces, t => t.FormattedMessage.Contains("2 functions found (Custom)")); } + [Fact] + public void FunctionMetadataManager_LoadFunctionMetadata_BadMetadataProvider_ReturnsFailedTask() + { + var functionMetadataCollection = new Collection(); + var mockFunctionErrors = new Dictionary>(); + var mockFunctionMetadataProvider = new Mock(); + var badFunctionMetadataProvider = new Mock(); + var workerConfigs = TestHelpers.GetTestWorkerConfigs(); + var testLoggerProvider = new TestLoggerProvider(); + var loggerFactory = new LoggerFactory(); + loggerFactory.AddProvider(testLoggerProvider); + + mockFunctionMetadataProvider.Setup(m => m.GetFunctionMetadataAsync(workerConfigs, SystemEnvironment.Instance, false)).Returns(Task.FromResult(new Collection().ToImmutableArray())); + mockFunctionMetadataProvider.Setup(m => m.FunctionErrors).Returns(new Dictionary>().ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value.ToImmutableArray())); + + // A bad provider that returns a faulty task + var tcs = new TaskCompletionSource>(); + badFunctionMetadataProvider + .Setup(m => m.GetFunctionMetadataAsync()) + .Returns(Task.FromException>(new Exception("Simulated failure"))); + + FunctionMetadataManager testFunctionMetadataManager = TestFunctionMetadataManager.GetFunctionMetadataManager(new OptionsWrapper(_scriptJobHostOptions), + mockFunctionMetadataProvider.Object, new List() { badFunctionMetadataProvider.Object }, new OptionsWrapper(_defaultHttpWorkerOptions), loggerFactory, new TestOptionsMonitor(TestHelpers.GetTestLanguageWorkerOptions())); + + var exception = Assert.Throws(() => testFunctionMetadataManager.LoadFunctionMetadata()); + Assert.Contains("Simulated failure", exception.InnerException.Message); + } + [Fact] public void FunctionMetadataManager_LoadFunctionMetadata_Throws_WhenFunctionProvidersTimesOut() {