Fix flaky test failure in ServerCommandTest

This commit is contained in:
Ajay Bhargav Baaskaran 2018-04-30 13:02:15 -07:00
parent c59dcb9b4c
commit 7cba5ed593
3 changed files with 103 additions and 27 deletions

View File

@ -25,7 +25,10 @@ namespace Microsoft.AspNetCore.Razor.Tools
internal ServerCommand(Application parent, string pipeName, int? keepAlive = null) internal ServerCommand(Application parent, string pipeName, int? keepAlive = null)
: this(parent) : this(parent)
{ {
Pipe.Values.Add(pipeName); if (!string.IsNullOrEmpty(pipeName))
{
Pipe.Values.Add(pipeName);
}
if (keepAlive.HasValue) if (keepAlive.HasValue)
{ {
@ -119,8 +122,21 @@ namespace Microsoft.AspNetCore.Razor.Tools
dispatcher.Run(); dispatcher.Run();
} }
internal FileStream WritePidFile() protected virtual FileStream WritePidFile()
{ {
var path = GetPidFilePath(env => Environment.GetEnvironmentVariable(env));
return WritePidFile(path);
}
// Internal for testing.
internal virtual FileStream WritePidFile(string directoryPath)
{
if (string.IsNullOrEmpty(directoryPath))
{
// Invalid path. Bail.
return null;
}
// To make all the running rzc servers more discoverable, We want to write the process Id and pipe name to a file. // To make all the running rzc servers more discoverable, We want to write the process Id and pipe name to a file.
// The file contents will be in the following format, // The file contents will be in the following format,
// //
@ -133,24 +149,10 @@ namespace Microsoft.AspNetCore.Razor.Tools
var processId = Process.GetCurrentProcess().Id; var processId = Process.GetCurrentProcess().Id;
var fileName = $"rzc-{processId}"; var fileName = $"rzc-{processId}";
var path = Environment.GetEnvironmentVariable("DOTNET_BUILD_PIDFILE_DIRECTORY");
if (string.IsNullOrEmpty(path))
{
var homeEnvVariable = PlatformInformation.IsWindows ? "USERPROFILE" : "HOME";
var homePath = Environment.GetEnvironmentVariable(homeEnvVariable);
if (string.IsNullOrEmpty(homePath))
{
// Couldn't locate the user profile directory. Bail.
return null;
}
path = Path.Combine(homePath, ".dotnet", "pids", "build");
}
// Make sure the directory exists. // Make sure the directory exists.
Directory.CreateDirectory(path); Directory.CreateDirectory(directoryPath);
path = Path.Combine(path, fileName); var path = Path.Combine(directoryPath, fileName);
var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, DefaultBufferSize, FileOptions.DeleteOnClose); var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, DefaultBufferSize, FileOptions.DeleteOnClose);
using (var writer = new StreamWriter(fileStream, Encoding.UTF8, DefaultBufferSize, leaveOpen: true)) using (var writer = new StreamWriter(fileStream, Encoding.UTF8, DefaultBufferSize, leaveOpen: true))
@ -162,5 +164,25 @@ namespace Microsoft.AspNetCore.Razor.Tools
return fileStream; return fileStream;
} }
// Internal for testing.
internal virtual string GetPidFilePath(Func<string, string> getEnvironmentVariable)
{
var path = getEnvironmentVariable("DOTNET_BUILD_PIDFILE_DIRECTORY");
if (string.IsNullOrEmpty(path))
{
var homeEnvVariable = PlatformInformation.IsWindows ? "USERPROFILE" : "HOME";
var homePath = getEnvironmentVariable(homeEnvVariable);
if (string.IsNullOrEmpty(homePath))
{
// Couldn't locate the user profile directory. Bail.
return null;
}
path = Path.Combine(homePath, ".dotnet", "pids", "build");
}
return path;
}
} }
} }

View File

@ -116,7 +116,6 @@ namespace Microsoft.AspNetCore.Razor.Tools
private readonly CancellationToken _cancellationToken; private readonly CancellationToken _cancellationToken;
private readonly TimeSpan? _keepAlive; private readonly TimeSpan? _keepAlive;
public TestableServerCommand( public TestableServerCommand(
ConnectionHost host, ConnectionHost host,
CompilerHost compilerHost, CompilerHost compilerHost,
@ -146,6 +145,12 @@ namespace Microsoft.AspNetCore.Razor.Tools
_eventBus ?? eventBus, _eventBus ?? eventBus,
_keepAlive ?? keepAlive); _keepAlive ?? keepAlive);
} }
protected override FileStream WritePidFile()
{
// Disable writing PID file as it is tested separately.
return null;
}
} }
} }
} }

View File

@ -13,15 +13,15 @@ namespace Microsoft.AspNetCore.Razor.Tools
{ {
public class ServerCommandTest public class ServerCommandTest
{ {
[Fact(Skip = "https://github.com/aspnet/Razor/issues/2310")] [Fact]
public void WritePidFile_WorksAsExpected() public void WritePidFile_WorksAsExpected()
{ {
// Arrange // Arrange
var expectedProcessId = Process.GetCurrentProcess().Id; var expectedProcessId = Process.GetCurrentProcess().Id;
var expectedRzcPath = typeof(ServerCommand).Assembly.Location; var expectedRzcPath = typeof(ServerCommand).Assembly.Location;
var expectedFileName = $"rzc-{expectedProcessId}"; var expectedFileName = $"rzc-{expectedProcessId}";
var homeEnvVariable = PlatformInformation.IsWindows ? "USERPROFILE" : "HOME"; var directoryPath = Path.Combine(Path.GetTempPath(), "RazorTest", Guid.NewGuid().ToString());
var path = Path.Combine(Environment.GetEnvironmentVariable(homeEnvVariable), ".dotnet", "pids", "build", expectedFileName); var path = Path.Combine(directoryPath, expectedFileName);
var pipeName = Guid.NewGuid().ToString(); var pipeName = Guid.NewGuid().ToString();
var server = GetServerCommand(pipeName); var server = GetServerCommand(pipeName);
@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Razor.Tools
// Act & Assert // Act & Assert
try try
{ {
using (var _ = server.WritePidFile()) using (var _ = server.WritePidFile(directoryPath))
{ {
Assert.True(File.Exists(path)); Assert.True(File.Exists(path));
@ -47,15 +47,64 @@ namespace Microsoft.AspNetCore.Razor.Tools
} }
finally finally
{ {
// Delete the file in case the test fails. // Cleanup after the test.
if (File.Exists(path)) if (Directory.Exists(directoryPath))
{ {
File.Delete(path); Directory.Delete(directoryPath, recursive: true);
} }
} }
} }
private ServerCommand GetServerCommand(string pipeName) [Fact]
public void GetPidFilePath_ReturnsCorrectDefaultPath()
{
// Arrange
var expectedPath = Path.Combine("homeDir", ".dotnet", "pids", "build");
var server = GetServerCommand();
// Act
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env =>
{
if (env == "DOTNET_BUILD_PIDFILE_DIRECTORY")
{
return null;
}
return "homeDir";
});
// Assert
Assert.Equal(expectedPath, directoryPath);
}
[Fact]
public void GetPidFilePath_UsesEnvironmentVariablePathIfSpecified()
{
// Arrange
var expectedPath = "/Some/directory/path/";
var server = GetServerCommand();
// Act
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env => expectedPath);
// Assert
Assert.Equal(expectedPath, directoryPath);
}
[Fact]
public void GetPidFilePath_NullEnvironmentVariableValue_ReturnsNull()
{
// Arrange
var server = GetServerCommand();
// Act
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env => null);
// Assert
Assert.Null(directoryPath);
}
private ServerCommand GetServerCommand(string pipeName = null)
{ {
var application = new Application( var application = new Application(
CancellationToken.None, CancellationToken.None,