Skip to content

Commit

Permalink
Return metadata provider task whether it is successful or not (#10598)
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem authored Nov 6, 2024
1 parent 287ceac commit 74e3ca7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/WebJobs.Script/Host/FunctionMetadataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private void AddMetadataFromCustomProviders(IEnumerable<IFunctionProvider> funct

var completedTask = Task.WhenAny(getFunctionMetadataFromProviderTask, delayTask).ContinueWith(t =>
{
if (t.Result == getFunctionMetadataFromProviderTask && getFunctionMetadataFromProviderTask.IsCompletedSuccessfully)
if (t.Result == getFunctionMetadataFromProviderTask)
{
return getFunctionMetadataFromProviderTask.Result;
}
Expand Down
29 changes: 29 additions & 0 deletions test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FunctionMetadata>();
var mockFunctionErrors = new Dictionary<string, ImmutableArray<string>>();
var mockFunctionMetadataProvider = new Mock<IFunctionMetadataProvider>();
var badFunctionMetadataProvider = new Mock<IFunctionProvider>();
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<FunctionMetadata>().ToImmutableArray()));
mockFunctionMetadataProvider.Setup(m => m.FunctionErrors).Returns(new Dictionary<string, ICollection<string>>().ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value.ToImmutableArray()));

// A bad provider that returns a faulty task
var tcs = new TaskCompletionSource<ImmutableArray<FunctionMetadata>>();
badFunctionMetadataProvider
.Setup(m => m.GetFunctionMetadataAsync())
.Returns(Task.FromException<ImmutableArray<FunctionMetadata>>(new Exception("Simulated failure")));

FunctionMetadataManager testFunctionMetadataManager = TestFunctionMetadataManager.GetFunctionMetadataManager(new OptionsWrapper<ScriptJobHostOptions>(_scriptJobHostOptions),
mockFunctionMetadataProvider.Object, new List<IFunctionProvider>() { badFunctionMetadataProvider.Object }, new OptionsWrapper<HttpWorkerOptions>(_defaultHttpWorkerOptions), loggerFactory, new TestOptionsMonitor<LanguageWorkerOptions>(TestHelpers.GetTestLanguageWorkerOptions()));

var exception = Assert.Throws<AggregateException>(() => testFunctionMetadataManager.LoadFunctionMetadata());
Assert.Contains("Simulated failure", exception.InnerException.Message);
}

[Fact]
public void FunctionMetadataManager_LoadFunctionMetadata_Throws_WhenFunctionProvidersTimesOut()
{
Expand Down

0 comments on commit 74e3ca7

Please sign in to comment.