-
Notifications
You must be signed in to change notification settings - Fork 310
Add test for SIGTERM functionality #878
Changes from 4 commits
4e7de6a
37388c7
60960ae
d8bd88e
fcdbbdf
c0f52a9
6ebea6d
877ef71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using Microsoft.Extensions.PlatformAbstractions; | ||
|
||
namespace Microsoft.AspNetCore.Server.IntegrationTesting.xunit | ||
{ | ||
public class TestProjectHelpers | ||
{ | ||
public static string GetProjectRoot() | ||
{ | ||
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; | ||
|
||
var directoryInfo = new DirectoryInfo(applicationBasePath); | ||
do | ||
{ | ||
var projectFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, "project.json")); | ||
if (projectFileInfo.Exists) | ||
{ | ||
return projectFileInfo.DirectoryName; | ||
} | ||
|
||
directoryInfo = directoryInfo.Parent; | ||
} | ||
while (directoryInfo.Parent != null); | ||
|
||
throw new Exception($"Project root could not be found using {applicationBasePath}"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>fc578f4e-171c-4f82-b301-3abf6318d082</ProjectGuid> | ||
<RootNamespace>Microsoft.AspNetCore.Hosting.FunctionalTests</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Server.IntegrationTesting; | ||
using Microsoft.AspNetCore.Server.IntegrationTesting.xunit; | ||
using Microsoft.AspNetCore.Testing.xunit; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace Microsoft.AspNetCore.Hosting.FunctionalTests | ||
{ | ||
public class ShutdownTests | ||
{ | ||
[ConditionalFact] | ||
[OSSkipCondition(OperatingSystems.Windows)] | ||
[OSSkipCondition(OperatingSystems.MacOSX)] | ||
public void ShutdownTest() | ||
{ | ||
var logger = new LoggerFactory() | ||
.AddConsole() | ||
.CreateLogger(nameof(ShutdownTest)); | ||
|
||
string applicationPath = Path.Combine(TestProjectHelpers.GetProjectRoot(), "..", | ||
"Microsoft.AspNetCore.Hosting.TestSites"); | ||
|
||
var deploymentParameters = new DeploymentParameters( | ||
applicationPath, | ||
ServerType.Kestrel, | ||
RuntimeFlavor.CoreClr, | ||
RuntimeArchitecture.x64) | ||
{ | ||
EnvironmentName = "Shutdown", | ||
TargetFramework = "netcoreapp1.1", | ||
ApplicationType = ApplicationType.Portable, | ||
PublishApplicationBeforeDeployment = true | ||
}; | ||
|
||
using (var deployer = new SelfHostDeployer(deploymentParameters, logger)) | ||
{ | ||
deployer.Deploy(); | ||
|
||
// Wait for application to start | ||
System.Threading.Thread.Sleep(1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you query the HostProcess to see if it's started? Why was the delay needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delay is to make sure hosting started IServer. |
||
|
||
string output = string.Empty; | ||
deployer.HostProcess.OutputDataReceived += (sender, args) => output += args.Data + '\n'; | ||
|
||
SendSIGINT(deployer.HostProcess.Id); | ||
|
||
deployer.HostProcess.WaitForExit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this going to make the test hang if something goes wrong? Any way to timeout here? |
||
output = output.Trim('\n'); | ||
|
||
Assert.Equal(output, "Application is shutting down...\n" + | ||
"Stopping firing\n" + | ||
"Stopping end\n" + | ||
"Stopped firing\n" + | ||
"Stopped end"); | ||
} | ||
} | ||
|
||
|
||
private static void SendSIGINT(int processId) | ||
{ | ||
var startInfo = new ProcessStartInfo | ||
{ | ||
FileName = "kill", | ||
Arguments = processId.ToString(), | ||
RedirectStandardOutput = true, | ||
UseShellExecute = false | ||
}; | ||
|
||
var process = Process.Start(startInfo); | ||
process.WaitForExit(1000); | ||
if (!process.HasExited) | ||
{ | ||
process.Kill(); | ||
} | ||
|
||
Assert.Equal(0, process.ExitCode); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"buildOptions": { | ||
"warningsAsErrors": true, | ||
"keyFile": "../../tools/Key.snk", | ||
"copyToOutput": { | ||
"include": [ | ||
"testroot/**/*" | ||
] | ||
} | ||
}, | ||
"publishOptions": { | ||
"include": [ | ||
"testroot/**/*" | ||
] | ||
}, | ||
"dependencies": { | ||
"dotnet-test-xunit": "2.2.0-*", | ||
"Microsoft.AspNetCore.Server.IntegrationTesting": "0.2.0-*", | ||
"Microsoft.AspNetCore.Hosting": "1.1.0-*", | ||
"Microsoft.Extensions.Logging.Console": "1.1.0-*", | ||
"xunit": "2.2.0-*" | ||
}, | ||
"frameworks": { | ||
"netcoreapp1.1": { | ||
"dependencies": { | ||
"Microsoft.NETCore.App": { | ||
"version": "1.1.0-*", | ||
"type": "platform" | ||
} | ||
} | ||
} | ||
}, | ||
"testRunner": "xunit" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>542d4600-b232-4b17-a08c-e31ebfa0d74e</ProjectGuid> | ||
<RootNamespace>Microsoft.AspNetCore.Hosting.TestSites</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Http.Features; | ||
|
||
namespace ServerComparison.TestSites | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var config = new ConfigurationBuilder() | ||
.AddCommandLine(args) | ||
.Build(); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseServer(new NoopServer()) | ||
.UseConfiguration(config) | ||
.UseStartup("Microsoft.AspNetCore.Hosting.TestSites"); | ||
|
||
var host = builder.Build(); | ||
|
||
host.Run(); | ||
} | ||
} | ||
|
||
public class NoopServer : IServer | ||
{ | ||
private readonly ManualResetEventSlim _serverEvent = new ManualResetEventSlim(false); | ||
|
||
public void Dispose() | ||
{ | ||
_serverEvent.Wait(); | ||
} | ||
|
||
public IFeatureCollection Features { get; } = new FeatureCollection(); | ||
|
||
public void Start<TContext>(IHttpApplication<TContext> application) | ||
{ | ||
_serverEvent.Set(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no guarantee Start will be called if there are other startup errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then process would be killed with timeout. |
||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run Dougs script to clean this up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shared version was out-of-date. I fixed that…