1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
- using System ;
5
4
using System . Collections . Generic ;
6
5
using System . Collections . Immutable ;
7
- using System . IO ;
8
- using System . Linq ;
9
6
using System . Threading . Tasks ;
10
7
using Microsoft . Azure . WebJobs . Script . Config ;
11
8
using Microsoft . Azure . WebJobs . Script . Description ;
@@ -31,95 +28,97 @@ public FunctionMetadataProviderTests()
31
28
}
32
29
33
30
[ Fact ]
34
- public void GetFunctionMetadataAsync_WorkerIndexing_HostFallback ( )
31
+ public async Task GetFunctionMetadataAsync_WorkerIndexing_HostFallback ( )
35
32
{
36
33
// Arrange
37
34
_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
+ }
38
41
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 ( ) ;
50
43
environment . SetEnvironmentVariable ( EnvironmentSettingNames . FunctionWorkerRuntime , "node" ) ;
51
44
environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebJobsFeatureFlags , "EnableWorkerIndexing" ) ;
52
45
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 ) ;
54
52
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 ) ;
58
58
59
59
// Act
60
- var functions = defaultProvider . GetFunctionMetadataAsync ( workerConfigs , false ) . GetAwaiter ( ) . GetResult ( ) ;
60
+ ImmutableArray < FunctionMetadata > functions = await defaultProvider
61
+ . GetFunctionMetadataAsync ( workerConfigs , false ) ;
61
62
62
63
// Assert
63
64
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" ) ) ;
67
68
}
68
69
69
70
[ Fact ]
70
- public void GetFunctionMetadataAsync_HostIndexing ( )
71
+ public async Task GetFunctionMetadataAsync_HostIndexing ( )
71
72
{
72
73
// Arrange
73
74
_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
+ }
74
81
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 ( ) ;
86
83
environment . SetEnvironmentVariable ( EnvironmentSettingNames . FunctionWorkerRuntime , "node" ) ;
87
84
environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebJobsFeatureFlags , string . Empty ) ;
88
- var optionsMonitor = TestHelpers . CreateOptionsMonitor ( new FunctionsHostingConfigOptions ( ) ) ;
89
85
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 , [ ] ) ) ;
92
89
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 ) ;
94
96
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 ) ;
97
100
98
101
// Act
99
- var functions = defaultProvider . GetFunctionMetadataAsync ( workerConfigs , false ) . GetAwaiter ( ) . GetResult ( ) ;
102
+ ImmutableArray < FunctionMetadata > functions = await defaultProvider
103
+ . GetFunctionMetadataAsync ( workerConfigs , false ) ;
100
104
101
105
// Assert
102
106
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" ) ) ;
106
110
}
107
111
108
- private static RawFunctionMetadata GetTestRawFunctionMetadata ( bool useDefaultMetadataIndexing )
112
+ private static ImmutableArray < FunctionMetadata > GetTestFunctionMetadata ( string name = "testFunction" )
109
113
{
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
+ ] ;
123
122
}
124
123
}
125
124
}
0 commit comments