Skip to content

Commit 7cba5ed

Browse files
committed
Fix flaky test failure in ServerCommandTest
1 parent c59dcb9 commit 7cba5ed

File tree

3 files changed

+103
-27
lines changed

3 files changed

+103
-27
lines changed

src/Microsoft.AspNetCore.Razor.Tools/ServerCommand.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public ServerCommand(Application parent)
2525
internal ServerCommand(Application parent, string pipeName, int? keepAlive = null)
2626
: this(parent)
2727
{
28-
Pipe.Values.Add(pipeName);
28+
if (!string.IsNullOrEmpty(pipeName))
29+
{
30+
Pipe.Values.Add(pipeName);
31+
}
2932

3033
if (keepAlive.HasValue)
3134
{
@@ -119,8 +122,21 @@ protected virtual void ExecuteServerCore(ConnectionHost host, CompilerHost compi
119122
dispatcher.Run();
120123
}
121124

122-
internal FileStream WritePidFile()
125+
protected virtual FileStream WritePidFile()
126+
{
127+
var path = GetPidFilePath(env => Environment.GetEnvironmentVariable(env));
128+
return WritePidFile(path);
129+
}
130+
131+
// Internal for testing.
132+
internal virtual FileStream WritePidFile(string directoryPath)
123133
{
134+
if (string.IsNullOrEmpty(directoryPath))
135+
{
136+
// Invalid path. Bail.
137+
return null;
138+
}
139+
124140
// To make all the running rzc servers more discoverable, We want to write the process Id and pipe name to a file.
125141
// The file contents will be in the following format,
126142
//
@@ -133,24 +149,10 @@ internal FileStream WritePidFile()
133149
var processId = Process.GetCurrentProcess().Id;
134150
var fileName = $"rzc-{processId}";
135151

136-
var path = Environment.GetEnvironmentVariable("DOTNET_BUILD_PIDFILE_DIRECTORY");
137-
if (string.IsNullOrEmpty(path))
138-
{
139-
var homeEnvVariable = PlatformInformation.IsWindows ? "USERPROFILE" : "HOME";
140-
var homePath = Environment.GetEnvironmentVariable(homeEnvVariable);
141-
if (string.IsNullOrEmpty(homePath))
142-
{
143-
// Couldn't locate the user profile directory. Bail.
144-
return null;
145-
}
146-
147-
path = Path.Combine(homePath, ".dotnet", "pids", "build");
148-
}
149-
150152
// Make sure the directory exists.
151-
Directory.CreateDirectory(path);
153+
Directory.CreateDirectory(directoryPath);
152154

153-
path = Path.Combine(path, fileName);
155+
var path = Path.Combine(directoryPath, fileName);
154156
var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, DefaultBufferSize, FileOptions.DeleteOnClose);
155157

156158
using (var writer = new StreamWriter(fileStream, Encoding.UTF8, DefaultBufferSize, leaveOpen: true))
@@ -162,5 +164,25 @@ internal FileStream WritePidFile()
162164

163165
return fileStream;
164166
}
167+
168+
// Internal for testing.
169+
internal virtual string GetPidFilePath(Func<string, string> getEnvironmentVariable)
170+
{
171+
var path = getEnvironmentVariable("DOTNET_BUILD_PIDFILE_DIRECTORY");
172+
if (string.IsNullOrEmpty(path))
173+
{
174+
var homeEnvVariable = PlatformInformation.IsWindows ? "USERPROFILE" : "HOME";
175+
var homePath = getEnvironmentVariable(homeEnvVariable);
176+
if (string.IsNullOrEmpty(homePath))
177+
{
178+
// Couldn't locate the user profile directory. Bail.
179+
return null;
180+
}
181+
182+
path = Path.Combine(homePath, ".dotnet", "pids", "build");
183+
}
184+
185+
return path;
186+
}
165187
}
166188
}

test/Microsoft.AspNetCore.Razor.Tools.Test/Infrastructure/ServerUtilities.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ private class TestableServerCommand : ServerCommand
116116
private readonly CancellationToken _cancellationToken;
117117
private readonly TimeSpan? _keepAlive;
118118

119-
120119
public TestableServerCommand(
121120
ConnectionHost host,
122121
CompilerHost compilerHost,
@@ -146,6 +145,12 @@ protected override void ExecuteServerCore(
146145
_eventBus ?? eventBus,
147146
_keepAlive ?? keepAlive);
148147
}
148+
149+
protected override FileStream WritePidFile()
150+
{
151+
// Disable writing PID file as it is tested separately.
152+
return null;
153+
}
149154
}
150155
}
151156
}

test/Microsoft.AspNetCore.Razor.Tools.Test/ServerCommandTest.cs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ namespace Microsoft.AspNetCore.Razor.Tools
1313
{
1414
public class ServerCommandTest
1515
{
16-
[Fact(Skip = "https://github.com/aspnet/Razor/issues/2310")]
16+
[Fact]
1717
public void WritePidFile_WorksAsExpected()
1818
{
1919
// Arrange
2020
var expectedProcessId = Process.GetCurrentProcess().Id;
2121
var expectedRzcPath = typeof(ServerCommand).Assembly.Location;
2222
var expectedFileName = $"rzc-{expectedProcessId}";
23-
var homeEnvVariable = PlatformInformation.IsWindows ? "USERPROFILE" : "HOME";
24-
var path = Path.Combine(Environment.GetEnvironmentVariable(homeEnvVariable), ".dotnet", "pids", "build", expectedFileName);
23+
var directoryPath = Path.Combine(Path.GetTempPath(), "RazorTest", Guid.NewGuid().ToString());
24+
var path = Path.Combine(directoryPath, expectedFileName);
2525

2626
var pipeName = Guid.NewGuid().ToString();
2727
var server = GetServerCommand(pipeName);
2828

2929
// Act & Assert
3030
try
3131
{
32-
using (var _ = server.WritePidFile())
32+
using (var _ = server.WritePidFile(directoryPath))
3333
{
3434
Assert.True(File.Exists(path));
3535

@@ -47,15 +47,64 @@ public void WritePidFile_WorksAsExpected()
4747
}
4848
finally
4949
{
50-
// Delete the file in case the test fails.
51-
if (File.Exists(path))
50+
// Cleanup after the test.
51+
if (Directory.Exists(directoryPath))
5252
{
53-
File.Delete(path);
53+
Directory.Delete(directoryPath, recursive: true);
5454
}
5555
}
5656
}
5757

58-
private ServerCommand GetServerCommand(string pipeName)
58+
[Fact]
59+
public void GetPidFilePath_ReturnsCorrectDefaultPath()
60+
{
61+
// Arrange
62+
var expectedPath = Path.Combine("homeDir", ".dotnet", "pids", "build");
63+
var server = GetServerCommand();
64+
65+
// Act
66+
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env =>
67+
{
68+
if (env == "DOTNET_BUILD_PIDFILE_DIRECTORY")
69+
{
70+
return null;
71+
}
72+
73+
return "homeDir";
74+
});
75+
76+
// Assert
77+
Assert.Equal(expectedPath, directoryPath);
78+
}
79+
80+
[Fact]
81+
public void GetPidFilePath_UsesEnvironmentVariablePathIfSpecified()
82+
{
83+
// Arrange
84+
var expectedPath = "/Some/directory/path/";
85+
var server = GetServerCommand();
86+
87+
// Act
88+
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env => expectedPath);
89+
90+
// Assert
91+
Assert.Equal(expectedPath, directoryPath);
92+
}
93+
94+
[Fact]
95+
public void GetPidFilePath_NullEnvironmentVariableValue_ReturnsNull()
96+
{
97+
// Arrange
98+
var server = GetServerCommand();
99+
100+
// Act
101+
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env => null);
102+
103+
// Assert
104+
Assert.Null(directoryPath);
105+
}
106+
107+
private ServerCommand GetServerCommand(string pipeName = null)
59108
{
60109
var application = new Application(
61110
CancellationToken.None,

0 commit comments

Comments
 (0)