11// Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the MIT License. See License.txt in the project root for license information.
33
4- using System ;
54using System . Collections . Generic ;
65using System . Collections . Immutable ;
7- using System . IO ;
8- using System . Linq ;
96using System . Threading . Tasks ;
107using Microsoft . Azure . WebJobs . Script . Config ;
118using Microsoft . Azure . WebJobs . Script . Description ;
@@ -31,95 +28,97 @@ public FunctionMetadataProviderTests()
3128 }
3229
3330 [ Fact ]
34- public void GetFunctionMetadataAsync_WorkerIndexing_HostFallback ( )
31+ public async Task GetFunctionMetadataAsync_WorkerIndexing_HostFallback ( )
3532 {
3633 // Arrange
3734 _logger . ClearLogMessages ( ) ;
35+ ImmutableArray < FunctionMetadata > functionMetadataCollection = GetTestFunctionMetadata ( ) ;
36+ IList < RpcWorkerConfig > workerConfigs = TestHelpers . GetTestWorkerConfigs ( ) ;
37+ foreach ( RpcWorkerConfig config in workerConfigs )
38+ {
39+ config . Description . WorkerIndexing = "true" ;
40+ }
3841
39- var function = GetTestRawFunctionMetadata ( useDefaultMetadataIndexing : true ) ;
40- IEnumerable < RawFunctionMetadata > rawFunctionMetadataCollection = new List < RawFunctionMetadata > ( ) { function } ;
41- var functionMetadataCollection = new List < FunctionMetadata > ( ) ;
42- functionMetadataCollection . Add ( GetTestFunctionMetadata ( ) ) ;
43-
44- var workerConfigs = TestHelpers . GetTestWorkerConfigs ( ) . ToImmutableArray ( ) ;
45- workerConfigs . ToList ( ) . ForEach ( config => config . Description . WorkerIndexing = "true" ) ;
46- var scriptjobhostoptions = new ScriptJobHostOptions ( ) ;
47- scriptjobhostoptions . RootScriptPath = Path . Combine ( Environment . CurrentDirectory , @".." , ".." , ".." , ".." , "sample" , "node" ) ;
48-
49- var environment = SystemEnvironment . Instance ;
42+ TestEnvironment environment = new ( ) ;
5043 environment . SetEnvironmentVariable ( EnvironmentSettingNames . FunctionWorkerRuntime , "node" ) ;
5144 environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebJobsFeatureFlags , "EnableWorkerIndexing" ) ;
5245
53- var defaultProvider = new FunctionMetadataProvider ( _logger , _workerFunctionMetadataProvider . Object , _hostFunctionMetadataProvider . Object , new OptionsWrapper < FunctionsHostingConfigOptions > ( new FunctionsHostingConfigOptions ( ) ) , SystemEnvironment . Instance ) ;
46+ FunctionMetadataProvider defaultProvider = new (
47+ _logger ,
48+ _workerFunctionMetadataProvider . Object ,
49+ _hostFunctionMetadataProvider . Object ,
50+ new OptionsWrapper < FunctionsHostingConfigOptions > ( new FunctionsHostingConfigOptions ( ) ) ,
51+ environment ) ;
5452
55- FunctionMetadataResult result = new FunctionMetadataResult ( true , functionMetadataCollection . ToImmutableArray ( ) ) ;
56- _workerFunctionMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( workerConfigs , false ) ) . Returns ( Task . FromResult ( result ) ) ;
57- _hostFunctionMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( workerConfigs , false ) ) . Returns ( Task . FromResult ( functionMetadataCollection . ToImmutableArray ( ) ) ) ;
53+ FunctionMetadataResult result = new ( true , functionMetadataCollection ) ;
54+ _workerFunctionMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( workerConfigs , false ) )
55+ . ReturnsAsync ( result ) ;
56+ _hostFunctionMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( workerConfigs , false ) )
57+ . ReturnsAsync ( functionMetadataCollection ) ;
5858
5959 // Act
60- var functions = defaultProvider . GetFunctionMetadataAsync ( workerConfigs , false ) . GetAwaiter ( ) . GetResult ( ) ;
60+ ImmutableArray < FunctionMetadata > functions = await defaultProvider
61+ . GetFunctionMetadataAsync ( workerConfigs , false ) ;
6162
6263 // Assert
6364 Assert . Equal ( 1 , functions . Length ) ;
64- var traces = _logger . GetLogMessages ( ) ;
65- var functionLoadLogs = traces . Where ( m => string . Equals ( m . FormattedMessage , "Fallback to host indexing as worker denied indexing" ) ) ;
66- Assert . True ( functionLoadLogs . Any ( ) ) ;
65+ Assert . Contains (
66+ _logger . GetLogMessages ( ) ,
67+ m => string . Equals ( m . FormattedMessage , "Fallback to host indexing as worker denied indexing" ) ) ;
6768 }
6869
6970 [ Fact ]
70- public void GetFunctionMetadataAsync_HostIndexing ( )
71+ public async Task GetFunctionMetadataAsync_HostIndexing ( )
7172 {
7273 // Arrange
7374 _logger . ClearLogMessages ( ) ;
75+ ImmutableArray < FunctionMetadata > functionMetadataCollection = GetTestFunctionMetadata ( ) ;
76+ IList < RpcWorkerConfig > workerConfigs = TestHelpers . GetTestWorkerConfigs ( ) ;
77+ foreach ( RpcWorkerConfig config in workerConfigs )
78+ {
79+ config . Description . WorkerIndexing = "true" ;
80+ }
7481
75- var function = GetTestRawFunctionMetadata ( useDefaultMetadataIndexing : true ) ;
76- IEnumerable < RawFunctionMetadata > rawFunctionMetadataCollection = new List < RawFunctionMetadata > ( ) { function } ;
77- var functionMetadataCollection = new List < FunctionMetadata > ( ) ;
78- functionMetadataCollection . Add ( GetTestFunctionMetadata ( ) ) ;
79-
80- var workerConfigs = TestHelpers . GetTestWorkerConfigs ( ) . ToImmutableArray ( ) ;
81- workerConfigs . ToList ( ) . ForEach ( config => config . Description . WorkerIndexing = "true" ) ;
82- var scriptjobhostoptions = new ScriptJobHostOptions ( ) ;
83- scriptjobhostoptions . RootScriptPath = Path . Combine ( Environment . CurrentDirectory , @".." , ".." , ".." , ".." , "sample" , "node" ) ;
84-
85- var environment = SystemEnvironment . Instance ;
82+ TestEnvironment environment = new ( ) ;
8683 environment . SetEnvironmentVariable ( EnvironmentSettingNames . FunctionWorkerRuntime , "node" ) ;
8784 environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebJobsFeatureFlags , string . Empty ) ;
88- var optionsMonitor = TestHelpers . CreateOptionsMonitor ( new FunctionsHostingConfigOptions ( ) ) ;
8985
90- var workerMetadataProvider = new Mock < IWorkerFunctionMetadataProvider > ( ) ;
91- workerMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( It . IsAny < IEnumerable < RpcWorkerConfig > > ( ) , false ) ) . Returns ( Task . FromResult ( new FunctionMetadataResult ( true , ImmutableArray < FunctionMetadata > . Empty ) ) ) ;
86+ Mock < IWorkerFunctionMetadataProvider > workerMetadataProvider = new ( ) ;
87+ workerMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( It . IsAny < IEnumerable < RpcWorkerConfig > > ( ) , false ) )
88+ . ReturnsAsync ( new FunctionMetadataResult ( true , [ ] ) ) ;
9289
93- var defaultProvider = new FunctionMetadataProvider ( _logger , workerMetadataProvider . Object , _hostFunctionMetadataProvider . Object , new OptionsWrapper < FunctionsHostingConfigOptions > ( new FunctionsHostingConfigOptions ( ) ) , SystemEnvironment . Instance ) ;
90+ FunctionMetadataProvider defaultProvider = new (
91+ _logger ,
92+ workerMetadataProvider . Object ,
93+ _hostFunctionMetadataProvider . Object ,
94+ new OptionsWrapper < FunctionsHostingConfigOptions > ( new FunctionsHostingConfigOptions ( ) ) ,
95+ environment ) ;
9496
95- FunctionMetadataResult result = new FunctionMetadataResult ( true , functionMetadataCollection . ToImmutableArray ( ) ) ;
96- _hostFunctionMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( workerConfigs , false ) ) . Returns ( Task . FromResult ( functionMetadataCollection . ToImmutableArray ( ) ) ) ;
97+ FunctionMetadataResult result = new ( true , functionMetadataCollection ) ;
98+ _hostFunctionMetadataProvider . Setup ( m => m . GetFunctionMetadataAsync ( workerConfigs , false ) )
99+ . ReturnsAsync ( functionMetadataCollection ) ;
97100
98101 // Act
99- var functions = defaultProvider . GetFunctionMetadataAsync ( workerConfigs , false ) . GetAwaiter ( ) . GetResult ( ) ;
102+ ImmutableArray < FunctionMetadata > functions = await defaultProvider
103+ . GetFunctionMetadataAsync ( workerConfigs , false ) ;
100104
101105 // Assert
102106 Assert . Equal ( 1 , functions . Length ) ;
103- var traces = _logger . GetLogMessages ( ) ;
104- var functionLoadLogs = traces . Where ( m => string . Equals ( m . FormattedMessage , "Fallback to host indexing as worker denied indexing" ) ) ;
105- Assert . True ( functionLoadLogs . Any ( ) ) ;
107+ Assert . Contains (
108+ _logger . GetLogMessages ( ) ,
109+ m => string . Equals ( m . FormattedMessage , "Fallback to host indexing as worker denied indexing" ) ) ;
106110 }
107111
108- private static RawFunctionMetadata GetTestRawFunctionMetadata ( bool useDefaultMetadataIndexing )
112+ private static ImmutableArray < FunctionMetadata > GetTestFunctionMetadata ( string name = "testFunction" )
109113 {
110- return new RawFunctionMetadata ( )
111- {
112- UseDefaultMetadataIndexing = useDefaultMetadataIndexing
113- } ;
114- }
115-
116- private static FunctionMetadata GetTestFunctionMetadata ( string name = "testFunction" )
117- {
118- return new FunctionMetadata ( )
119- {
120- Name = name ,
121- Language = "node"
122- } ;
114+ return
115+ [
116+ new FunctionMetadata ( )
117+ {
118+ Name = name ,
119+ Language = "node"
120+ }
121+ ] ;
123122 }
124123 }
125124}
0 commit comments