Skip to content

Commit d3a2610

Browse files
committed
Fix more unit tests
1 parent a53f92d commit d3a2610

File tree

5 files changed

+110
-105
lines changed

5 files changed

+110
-105
lines changed

Directory.Build.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
<Import Project="$(EngBuildRoot)Engineering.targets" />
44

5+
<PropertyGroup>
6+
<VSTestResultsDirectory>$(ArtifactsPath)/log/$(ArtifactsProjectName)/tests_$(ArtifactsPivots)/</VSTestResultsDirectory>
7+
</PropertyGroup>
8+
59
</Project>

src/WebJobs.Script/Workers/Rpc/RpcWorkerProcess.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,23 @@ internal class RpcWorkerProcess : WorkerProcess
2626
private readonly IOptions<FunctionsHostingConfigOptions> _hostingConfigOptions;
2727
private readonly IEnvironment _environment;
2828

29-
internal RpcWorkerProcess(string runtime,
30-
string workerId,
31-
string rootScriptPath,
32-
Uri serverUri,
33-
RpcWorkerConfig rpcWorkerConfig,
34-
IScriptEventManager eventManager,
35-
IWorkerProcessFactory processFactory,
36-
IProcessRegistry processRegistry,
37-
ILogger workerProcessLogger,
38-
IWorkerConsoleLogSource consoleLogSource,
39-
IMetricsLogger metricsLogger,
40-
IServiceProvider serviceProvider,
41-
IOptions<FunctionsHostingConfigOptions> hostingConfigOptions,
42-
IEnvironment environment,
43-
IOptionsMonitor<ScriptApplicationHostOptions> scriptApplicationHostOptions,
44-
ILoggerFactory loggerFactory)
29+
internal RpcWorkerProcess(
30+
string runtime,
31+
string workerId,
32+
string rootScriptPath,
33+
Uri serverUri,
34+
RpcWorkerConfig rpcWorkerConfig,
35+
IScriptEventManager eventManager,
36+
IWorkerProcessFactory processFactory,
37+
IProcessRegistry processRegistry,
38+
ILogger workerProcessLogger,
39+
IWorkerConsoleLogSource consoleLogSource,
40+
IMetricsLogger metricsLogger,
41+
IServiceProvider serviceProvider,
42+
IOptions<FunctionsHostingConfigOptions> hostingConfigOptions,
43+
IEnvironment environment,
44+
IOptionsMonitor<ScriptApplicationHostOptions> scriptApplicationHostOptions,
45+
ILoggerFactory loggerFactory)
4546
: base(eventManager, processRegistry, workerProcessLogger, consoleLogSource, metricsLogger, serviceProvider, loggerFactory, environment,
4647
scriptApplicationHostOptions, rpcWorkerConfig.Description.UseStdErrorStreamForErrorsOnly)
4748
{
@@ -81,7 +82,7 @@ internal override void HandleWorkerProcessExitError(WorkerProcessExitException r
8182
}
8283

8384
// The subscriber of WorkerErrorEvent is expected to Dispose() the errored channel
84-
_workerProcessLogger.LogError(rpcWorkerProcessExitException, $"Language Worker Process exited. Pid={rpcWorkerProcessExitException.Pid}.", _workerProcessArguments.ExecutablePath);
85+
_workerProcessLogger.LogError(rpcWorkerProcessExitException, $"Language Worker Process exited. Pid={rpcWorkerProcessExitException.Pid}.", _workerProcessArguments?.ExecutablePath);
8586
_eventManager.Publish(new WorkerErrorEvent(_runtime, _workerId, rpcWorkerProcessExitException));
8687
}
8788

@@ -91,4 +92,4 @@ internal override void HandleWorkerProcessRestart()
9192
_eventManager.Publish(new WorkerRestartEvent(_runtime, _workerId));
9293
}
9394
}
94-
}
95+
}

test/WebJobs.Script.Tests.Integration/TestFunctionHost.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,35 @@ public TestFunctionHost(string scriptPath, string logPath, string testDataPath =
104104
.AddFilter<TestLoggerProvider>("Azure.Core", LogLevel.Warning);
105105
})
106106
.ConfigureServices(services =>
107-
{
108-
services.Replace(new ServiceDescriptor(typeof(ISecretManagerProvider), new TestSecretManagerProvider(new TestSecretManager())));
109-
services.Replace(new ServiceDescriptor(typeof(IOptions<ScriptApplicationHostOptions>), sp =>
110-
{
111-
_hostOptions.RootServiceProvider = sp;
112-
return new OptionsWrapper<ScriptApplicationHostOptions>(_hostOptions);
113-
}, ServiceLifetime.Singleton));
114-
services.Replace(new ServiceDescriptor(typeof(IOptionsMonitor<ScriptApplicationHostOptions>), sp =>
115-
{
116-
_hostOptions.RootServiceProvider = sp;
117-
return TestHelpers.CreateOptionsMonitor(_hostOptions);
118-
}, ServiceLifetime.Singleton));
119-
services.Replace(new ServiceDescriptor(typeof(IExtensionBundleManager), new TestExtensionBundleManager()));
120-
services.Replace(new ServiceDescriptor(typeof(IFunctionMetadataManager), sp =>
121-
{
122-
var montior = sp.GetService<IOptionsMonitor<ScriptApplicationHostOptions>>();
123-
var scriptManager = sp.GetService<IScriptHostManager>();
124-
var loggerFactory = sp.GetService<ILoggerFactory>();
125-
var environment = sp.GetService<IEnvironment>();
126-
127-
return GetMetadataManager(montior, scriptManager, loggerFactory, environment);
128-
}, ServiceLifetime.Singleton));
129-
130-
services.AddSingleton<ISystemLoggerFactory, SystemLoggerFactory>();
131-
services.SkipDependencyValidation();
132-
133-
// Allows us to configure services as the last step, thereby overriding anything
134-
services.AddSingleton(new PostConfigureServices(configureWebHostServices));
135-
})
107+
{
108+
services.Replace(new ServiceDescriptor(typeof(ISecretManagerProvider), new TestSecretManagerProvider(new TestSecretManager())));
109+
services.Replace(new ServiceDescriptor(typeof(IOptions<ScriptApplicationHostOptions>), sp =>
110+
{
111+
_hostOptions.RootServiceProvider = sp;
112+
return new OptionsWrapper<ScriptApplicationHostOptions>(_hostOptions);
113+
}, ServiceLifetime.Singleton));
114+
services.Replace(new ServiceDescriptor(typeof(IOptionsMonitor<ScriptApplicationHostOptions>), sp =>
115+
{
116+
_hostOptions.RootServiceProvider = sp;
117+
return TestHelpers.CreateOptionsMonitor(_hostOptions);
118+
}, ServiceLifetime.Singleton));
119+
services.Replace(new ServiceDescriptor(typeof(IExtensionBundleManager), new TestExtensionBundleManager()));
120+
services.Replace(new ServiceDescriptor(typeof(IFunctionMetadataManager), sp =>
121+
{
122+
var montior = sp.GetService<IOptionsMonitor<ScriptApplicationHostOptions>>();
123+
var scriptManager = sp.GetService<IScriptHostManager>();
124+
var loggerFactory = sp.GetService<ILoggerFactory>();
125+
var environment = sp.GetService<IEnvironment>();
126+
127+
return GetMetadataManager(montior, scriptManager, loggerFactory, environment);
128+
}, ServiceLifetime.Singleton));
129+
130+
services.AddSingleton<ISystemLoggerFactory, SystemLoggerFactory>();
131+
services.SkipDependencyValidation();
132+
133+
// Allows us to configure services as the last step, thereby overriding anything
134+
services.AddSingleton(new PostConfigureServices(configureWebHostServices));
135+
})
136136
.ConfigureScriptHostWebJobsBuilder(scriptHostWebJobsBuilder =>
137137
{
138138
/// REVIEW THIS

test/WebJobs.Script.Tests/Description/FunctionInvokerBaseTests.cs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,49 @@
99
using Microsoft.Azure.WebJobs.Host.Loggers;
1010
using Microsoft.Azure.WebJobs.Script.Description;
1111
using Microsoft.Azure.WebJobs.Script.Diagnostics;
12-
using Microsoft.Azure.WebJobs.Script.Eventing;
1312
using Microsoft.Azure.WebJobs.Script.Metrics;
1413
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
15-
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
1614
using Microsoft.Extensions.DependencyInjection;
1715
using Microsoft.Extensions.Hosting;
1816
using Microsoft.Extensions.Logging;
1917
using Microsoft.WebJobs.Script.Tests;
20-
using Moq;
2118
using Newtonsoft.Json.Linq;
2219
using Xunit;
2320

2421
namespace Microsoft.Azure.WebJobs.Script.Tests
2522
{
26-
public class FunctionInvokerBaseTests : IDisposable
23+
public sealed class FunctionInvokerBaseTests : IDisposable
2724
{
28-
private MockInvoker _invoker;
29-
private IHost _host;
30-
private ScriptHost _scriptHost;
31-
private TestMetricsLogger _metricsLogger;
32-
private TestLoggerProvider _testLoggerProvider;
25+
private readonly TestMetricsLogger _metricsLogger = new();
26+
private readonly TestLoggerProvider _testLoggerProvider = new();
27+
private readonly MockInvoker _invoker;
28+
private readonly IHost _host;
29+
private readonly ScriptHost _scriptHost;
3330

3431
public FunctionInvokerBaseTests()
3532
{
36-
_metricsLogger = new TestMetricsLogger();
37-
_testLoggerProvider = new TestLoggerProvider();
38-
39-
ILoggerFactory loggerFactory = new LoggerFactory();
33+
LoggerFactory loggerFactory = new();
4034
loggerFactory.AddProvider(_testLoggerProvider);
4135

42-
var eventManager = new ScriptEventManager();
43-
44-
var metadata = new FunctionMetadata
36+
FunctionMetadata metadata = new()
4537
{
4638
Name = "TestFunction",
4739
ScriptFile = "index.js",
4840
Language = "node"
4941
};
42+
5043
JObject binding = JObject.FromObject(new
5144
{
5245
type = "manualTrigger",
5346
name = "manual",
5447
direction = "in"
5548
});
49+
5650
metadata.Bindings.Add(BindingMetadata.Create(binding));
5751

58-
var metadataManager = new MockMetadataManager(new[] { metadata });
52+
MockMetadataManager metadataManager = new([metadata]);
53+
54+
// TODO: Can we instantiate a ScriptHost directly?
5955
_host = new HostBuilder()
6056
.ConfigureDefaultTestWebScriptHost()
6157
.ConfigureServices(s =>
@@ -66,10 +62,7 @@ public FunctionInvokerBaseTests()
6662
.Build();
6763

6864
_scriptHost = _host.GetScriptHost();
69-
_scriptHost.InitializeAsync().Wait();
70-
7165
var hostMetrics = _host.Services.GetService<HostMetrics>();
72-
7366
_invoker = new MockInvoker(_scriptHost, _metricsLogger, hostMetrics, metadataManager, metadata, loggerFactory);
7467
}
7568

@@ -182,17 +175,16 @@ await Assert.ThrowsAsync<InvalidOperationException>(async () =>
182175
Assert.Equal(startLatencyEvent, completedLatencyEvent);
183176
}
184177

185-
protected virtual void Dispose(bool disposing)
178+
public void Dispose()
186179
{
187-
if (disposing)
180+
try
188181
{
189182
_host?.Dispose();
190183
}
191-
}
192-
193-
public void Dispose()
194-
{
195-
Dispose(true);
184+
catch (Exception)
185+
{
186+
// this might throw due to invalid setup.
187+
}
196188
}
197189

198190
private class MockInvoker : FunctionInvokerBase

0 commit comments

Comments
 (0)