Skip to content

Commit

Permalink
Fixed incorrect function count in the log message when getting functi…
Browse files Browse the repository at this point in the history
…on metadata from providers (#10224)

* Fixed incorrect function count in the log message.(#10220)

* Minor variable rename

* Check DefaultOrEmpty before accessing Length of immutable array.

* Fix integration test to reflect the change.
  • Loading branch information
kshyju committed Jun 14, 2024
1 parent f138d7c commit 9823a46
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
- Ensuring proxies are disabled, with a warning, when running in Flex Consumption.
- Fixed an issue leading to a race when invocation responses returned prior to HTTP requests being sent in proxied scenarios.
- Language worker channels will not be started during placeholder mode if we are in-process (#10161)
- Ordered invocations are now the default (#10201)
- Ordered invocations are now the default (#10201)
- Fixed incorrect function count in the log message.(#10220)
7 changes: 4 additions & 3 deletions src/WebJobs.Script/Host/FunctionMetadataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,15 @@ private void AddMetadataFromCustomProviders(IEnumerable<IFunctionProvider> funct
functionProviderTasks.Add(functionProvider.GetFunctionMetadataAsync());
}

var functionMetadataListArray = Task.WhenAll(functionProviderTasks).GetAwaiter().GetResult();
var providerFunctionMetadataResults = Task.WhenAll(functionProviderTasks).GetAwaiter().GetResult();
var totalFunctionsCount = providerFunctionMetadataResults.Where(metadataArray => !metadataArray.IsDefaultOrEmpty).Sum(metadataArray => metadataArray.Length);

// This is used to make sure no duplicates are registered
var distinctFunctionNames = new HashSet<string>(functionMetadataList.Select(m => m.Name));

_logger.FunctionsReturnedByProvider(functionMetadataListArray.Length, _metadataProviderName);
_logger.FunctionsReturnedByProvider(totalFunctionsCount, _metadataProviderName);

foreach (var metadataArray in functionMetadataListArray)
foreach (var metadataArray in providerFunctionMetadataResults)
{
if (!metadataArray.IsDefaultOrEmpty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public async Task Validate_HostLogs()
Assert.True(traces.Length == expectedCount, $"Expected {expectedCount} messages, but found {traces.Length}. Actual logs:{Environment.NewLine}{string.Join(Environment.NewLine, traces.Select(t => t.Message))}");

int idx = 0;
ValidateTrace(traces[idx++], "1 functions found", LogCategories.Startup);
ValidateTrace(traces[idx++], "0 functions found", LogCategories.Startup);
ValidateTrace(traces[idx++], "2 functions loaded", LogCategories.Startup);
ValidateTrace(traces[idx++], "A function allow list has been specified", LogCategories.Startup);
ValidateTrace(traces[idx++], "Found the following functions:\r\n", LogCategories.Startup);
Expand Down
49 changes: 49 additions & 0 deletions test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,55 @@ public void FunctionMetadataManager_DoesNotError_MissingScriptFile_InWebHostMode
Assert.False(testFunctionMetadataManager.IsScriptFileDetermined(testMetadata));
}

[Fact]
public void FunctionMetadataManager_GetsMetadata_FromMultipleFunctionProviders_Success()
{
var functionMetadataCollection1 = new Collection<FunctionMetadata>
{
GetTestFunctionMetadata("somefile.dll", name: "HelloHttp")
};

var functionMetadataCollection2 = new Collection<FunctionMetadata>
{
GetTestFunctionMetadata("somefile2.dll", name: "Function1"),
GetTestFunctionMetadata("somefile2.dll", name: "Function2"),
GetTestFunctionMetadata("somefile2.dll", name: "Function3")
};

var expectedTotalFunctionsCount = functionMetadataCollection1.Count + functionMetadataCollection2.Count;

var mockFunctionMetadataProvider = new Mock<IFunctionMetadataProvider>();
mockFunctionMetadataProvider.Setup(m => m.GetFunctionMetadataAsync(It.IsAny<IEnumerable<RpcWorkerConfig>>(), It.IsAny<SystemEnvironment>(), It.IsAny<bool>()))
.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()));

var mockFunctionProvider = new Mock<IFunctionProvider>();
mockFunctionProvider.Setup(m => m.GetFunctionMetadataAsync()).ReturnsAsync(functionMetadataCollection1.ToImmutableArray());

var mockFunctionProvider2 = new Mock<IFunctionProvider>();
mockFunctionProvider2.Setup(m => m.GetFunctionMetadataAsync()).ReturnsAsync(functionMetadataCollection2.ToImmutableArray());

var testLoggerProvider = new TestLoggerProvider();
var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(testLoggerProvider);

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

var actualFunctionMetadata = testFunctionMetadataManager.LoadFunctionMetadata();

var traces = testLoggerProvider.GetAllLogMessages();
Assert.Equal(expectedTotalFunctionsCount, actualFunctionMetadata.Length);
Assert.Single(traces.Where(t => t.FormattedMessage.Contains("Reading functions metadata (Custom)")));
Assert.Single(traces.Where(t => t.FormattedMessage.Contains($"{expectedTotalFunctionsCount} functions found (Custom)")));
}

[Fact]
public void FunctionMetadataManager_GetsMetadata_FromFunctionProviders()
{
Expand Down

0 comments on commit 9823a46

Please sign in to comment.