Skip to content

Commit ca6ce42

Browse files
committed
Fix tests setting env vars
1 parent 54a51ec commit ca6ce42

File tree

4 files changed

+80
-78
lines changed

4 files changed

+80
-78
lines changed

test/WebJobs.Script.Tests.Shared/TestHelpers.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,26 @@ private static async Task<string[]> ReadAllLinesSafeAsync(string logFile)
313313
public static async Task<string> ReadStreamToEnd(Stream stream)
314314
{
315315
stream.Position = 0;
316-
using (var sr = new StreamReader(stream))
317-
{
318-
return await sr.ReadToEndAsync();
319-
}
316+
using var sr = new StreamReader(stream);
317+
return await sr.ReadToEndAsync();
320318
}
321319

322-
public static IList<RpcWorkerConfig> GetTestWorkerConfigs(bool includeDllWorker = false, int processCountValue = 1,
323-
TimeSpan? processStartupInterval = null, TimeSpan? processRestartInterval = null, TimeSpan? processShutdownTimeout = null, bool workerIndexing = false)
320+
public static IList<RpcWorkerConfig> GetTestWorkerConfigs(
321+
bool includeDllWorker = false,
322+
int processCountValue = 1,
323+
TimeSpan? processStartupInterval = null,
324+
TimeSpan? processRestartInterval = null,
325+
TimeSpan? processShutdownTimeout = null,
326+
bool workerIndexing = false)
324327
{
325328
var defaultCountOptions = new WorkerProcessCountOptions();
326329
TimeSpan startupInterval = processStartupInterval ?? defaultCountOptions.ProcessStartupInterval;
327330
TimeSpan restartInterval = processRestartInterval ?? defaultCountOptions.ProcessRestartInterval;
328331
TimeSpan shutdownTimeout = processShutdownTimeout ?? defaultCountOptions.ProcessShutdownTimeout;
329332

330-
var workerConfigs = new List<RpcWorkerConfig>
331-
{
332-
new RpcWorkerConfig
333+
List<RpcWorkerConfig> workerConfigs =
334+
[
335+
new()
333336
{
334337
Description = GetTestWorkerDescription("node", ".js", workerIndexing),
335338
CountOptions = new WorkerProcessCountOptions
@@ -340,7 +343,7 @@ public static IList<RpcWorkerConfig> GetTestWorkerConfigs(bool includeDllWorker
340343
ProcessShutdownTimeout = shutdownTimeout
341344
}
342345
},
343-
new RpcWorkerConfig
346+
new()
344347
{
345348
Description = GetTestWorkerDescription("java", ".jar", workerIndexing),
346349
CountOptions = new WorkerProcessCountOptions
@@ -351,7 +354,7 @@ public static IList<RpcWorkerConfig> GetTestWorkerConfigs(bool includeDllWorker
351354
ProcessShutdownTimeout = shutdownTimeout
352355
}
353356
}
354-
};
357+
];
355358

356359
// Allow tests to have a worker that claims the .dll extension.
357360
if (includeDllWorker)

test/WebJobs.Script.Tests/Configuration/DefaultDependencyValidatorTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class DefaultDependencyValidatorTests
2828
[Fact]
2929
public async Task Validator_AllValid()
3030
{
31-
LogMessage invalidServicesMessage = await RunTest();
31+
LogMessage invalidServicesMessage = await RunTestAsync();
3232

3333
string msg = $"If you have registered new dependencies, make sure to update the DependencyValidator. Invalid Services:{Environment.NewLine}";
3434
Assert.True(invalidServicesMessage == null, msg + invalidServicesMessage?.Exception?.ToString());
@@ -37,7 +37,7 @@ public async Task Validator_AllValid()
3737
[Fact]
3838
public async Task Validator_InvalidServices_ThrowsException()
3939
{
40-
LogMessage invalidServicesMessage = await RunTest(configureJobHost: s =>
40+
LogMessage invalidServicesMessage = await RunTestAsync(configureJobHost: s =>
4141
{
4242
s.AddSingleton<IHostedService, MyHostedService>();
4343
s.AddSingleton<IScriptEventManager, MyScriptEventManager>();
@@ -61,7 +61,7 @@ public async Task Validator_InvalidServices_ThrowsException()
6161
[Fact]
6262
public async Task Validator_NoJobHost()
6363
{
64-
LogMessage invalidServicesMessage = await RunTest(configureWebHost: s =>
64+
LogMessage invalidServicesMessage = await RunTestAsync(configureWebHost: s =>
6565
{
6666
// This will force us to skip host startup (which removes the JobHost)
6767
s.AddSingleton<IScriptHostBuilder, TestScriptHostBuilder>();
@@ -71,10 +71,10 @@ public async Task Validator_NoJobHost()
7171
Assert.True(invalidServicesMessage == null, msg + invalidServicesMessage?.Exception?.ToString());
7272
}
7373

74-
private async Task<LogMessage> RunTest(Action<IServiceCollection> configureWebHost = null, Action<IServiceCollection> configureJobHost = null, bool expectSuccess = true)
74+
private async Task<LogMessage> RunTestAsync(Action<IServiceCollection> configureWebHost = null, Action<IServiceCollection> configureJobHost = null, bool expectSuccess = true)
7575
{
7676
LogMessage invalidServicesMessage = null;
77-
TestLoggerProvider loggerProvider = new TestLoggerProvider();
77+
TestLoggerProvider loggerProvider = new();
7878

7979
var builder = Program.CreateWebHostBuilder(null)
8080
.ConfigureLogging(b =>
Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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;
54
using System.Collections.Generic;
65
using System.Collections.Immutable;
7-
using System.IO;
8-
using System.Linq;
96
using System.Threading.Tasks;
107
using Microsoft.Azure.WebJobs.Script.Config;
118
using 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
}

test/WebJobs.Script.Tests/WorkerFunctionMetadataProviderTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ public async void ValidateFunctionMetadata_Logging()
193193
{
194194
});
195195

196-
var environment = SystemEnvironment.Instance;
196+
TestEnvironment environment = new();
197197
environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, "node");
198198

199-
var workerFunctionMetadataProvider = new WorkerFunctionMetadataProvider(optionsMonitor, logger, SystemEnvironment.Instance,
200-
mockWebHostRpcWorkerChannelManager.Object, mockScriptHostManager.Object);
199+
var workerFunctionMetadataProvider = new WorkerFunctionMetadataProvider(
200+
optionsMonitor, logger, environment, mockWebHostRpcWorkerChannelManager.Object, mockScriptHostManager.Object);
201201
await workerFunctionMetadataProvider.GetFunctionMetadataAsync(workerConfigs, false);
202202

203203
var traces = logger.GetLogMessages();

0 commit comments

Comments
 (0)