This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathServerLifecycleTest.cs
291 lines (250 loc) · 11.3 KB
/
ServerLifecycleTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// 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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing.xunit;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Tools
{
public class ServerLifecycleTest
{
private static ServerRequest EmptyServerRequest => new ServerRequest(1, Array.Empty<RequestArgument>());
private static ServerResponse EmptyServerResponse => new CompletedServerResponse(
returnCode: 0,
utf8output: false,
output: string.Empty,
error: string.Empty);
[Fact]
public void ServerStartup_MutexAlreadyAcquired_Fails()
{
// Arrange
var pipeName = Guid.NewGuid().ToString("N");
var mutexName = MutexName.GetServerMutexName(pipeName);
var compilerHost = new Mock<CompilerHost>(MockBehavior.Strict);
var host = new Mock<ConnectionHost>(MockBehavior.Strict);
// Act & Assert
using (var mutex = new Mutex(initiallyOwned: true, name: mutexName, createdNew: out var holdsMutex))
{
Assert.True(holdsMutex);
try
{
var result = ServerUtilities.RunServer(pipeName, host.Object, compilerHost.Object);
// Assert failure
Assert.Equal(1, result);
}
finally
{
mutex.ReleaseMutex();
}
}
}
[Fact]
public void ServerStartup_SuccessfullyAcquiredMutex()
{
// Arrange, Act & Assert
var pipeName = Guid.NewGuid().ToString("N");
var mutexName = MutexName.GetServerMutexName(pipeName);
var compilerHost = new Mock<CompilerHost>(MockBehavior.Strict);
var host = new Mock<ConnectionHost>(MockBehavior.Strict);
host
.Setup(x => x.WaitForConnectionAsync(It.IsAny<CancellationToken>()))
.Returns(() =>
{
// Use a thread instead of Task to guarantee this code runs on a different
// thread and we can validate the mutex state.
var source = new TaskCompletionSource<bool>();
var thread = new Thread(_ =>
{
Mutex mutex = null;
try
{
Assert.True(Mutex.TryOpenExisting(mutexName, out mutex));
Assert.False(mutex.WaitOne(millisecondsTimeout: 0));
source.SetResult(true);
}
catch (Exception ex)
{
source.SetException(ex);
throw;
}
finally
{
mutex?.Dispose();
}
});
// Synchronously wait here. Don't returned a Task value because we need to
// ensure the above check completes before the server hits a timeout and
// releases the mutex.
thread.Start();
source.Task.Wait();
return new TaskCompletionSource<Connection>().Task;
});
var result = ServerUtilities.RunServer(pipeName, host.Object, compilerHost.Object, keepAlive: TimeSpan.FromSeconds(1));
Assert.Equal(0, result);
}
[Fact]
public async Task ServerRunning_ShutdownRequest_processesSuccessfully()
{
// Arrange
using (var serverData = ServerUtilities.CreateServer())
{
// Act
var serverProcessId = await ServerUtilities.SendShutdown(serverData.PipeName);
// Assert
Assert.Equal(Process.GetCurrentProcess().Id, serverProcessId);
await serverData.Verify(connections: 1, completed: 1);
}
}
/// <summary>
/// A shutdown request should not abort an existing compilation. It should be allowed to run to
/// completion.
/// </summary>
[Fact]
public async Task ServerRunning_ShutdownRequest_DoesNotAbortCompilation()
{
// Arrange
var startCompilationSource = new TaskCompletionSource<bool>();
var finishCompilationSource = new TaskCompletionSource<bool>();
var host = CreateCompilerHost(c => c.ExecuteFunc = (req, ct) =>
{
// At this point, the connection has been accepted and the compilation has started.
startCompilationSource.SetResult(true);
// We want this to keep running even after the shutdown is seen.
finishCompilationSource.Task.Wait();
return EmptyServerResponse;
});
using (var serverData = ServerUtilities.CreateServer(compilerHost: host))
{
var compileTask = ServerUtilities.Send(serverData.PipeName, EmptyServerRequest);
// Wait for the request to go through and trigger compilation.
await startCompilationSource.Task;
// Act
// The compilation is now in progress, send the shutdown.
await ServerUtilities.SendShutdown(serverData.PipeName);
Assert.False(compileTask.IsCompleted);
// Now let the task complete.
finishCompilationSource.SetResult(true);
// Assert
var response = await compileTask;
Assert.Equal(ServerResponse.ResponseType.Completed, response.Type);
Assert.Equal(0, ((CompletedServerResponse)response).ReturnCode);
await serverData.Verify(connections: 2, completed: 2);
}
}
/// <summary>
/// Multiple clients should be able to send shutdown requests to the server.
/// </summary>
[Fact]
public async Task ServerRunning_MultipleShutdownRequests_HandlesSuccessfully()
{
// Arrange
var startCompilationSource = new TaskCompletionSource<bool>();
var finishCompilationSource = new TaskCompletionSource<bool>();
var host = CreateCompilerHost(c => c.ExecuteFunc = (req, ct) =>
{
// At this point, the connection has been accepted and the compilation has started.
startCompilationSource.SetResult(true);
// We want this to keep running even after the shutdown is seen.
finishCompilationSource.Task.Wait();
return EmptyServerResponse;
});
using (var serverData = ServerUtilities.CreateServer(compilerHost: host))
{
var compileTask = ServerUtilities.Send(serverData.PipeName, EmptyServerRequest);
// Wait for the request to go through and trigger compilation.
await startCompilationSource.Task;
// Act
for (var i = 0; i < 10; i++)
{
// The compilation is now in progress, send the shutdown.
var processId = await ServerUtilities.SendShutdown(serverData.PipeName);
Assert.Equal(Process.GetCurrentProcess().Id, processId);
Assert.False(compileTask.IsCompleted);
}
// Now let the task complete.
finishCompilationSource.SetResult(true);
// Assert
var response = await compileTask;
Assert.Equal(ServerResponse.ResponseType.Completed, response.Type);
Assert.Equal(0, ((CompletedServerResponse)response).ReturnCode);
await serverData.Verify(connections: 11, completed: 11);
}
}
// https://github.com/aspnet/Razor/issues/1991
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public async Task ServerRunning_CancelCompilation_CancelsSuccessfully()
{
// Arrange
const int requestCount = 5;
var count = 0;
var completionSource = new TaskCompletionSource<bool>();
var host = CreateCompilerHost(c => c.ExecuteFunc = (req, ct) =>
{
if (Interlocked.Increment(ref count) == requestCount)
{
completionSource.SetResult(true);
}
ct.WaitHandle.WaitOne();
return new RejectedServerResponse();
});
var semaphore = new SemaphoreSlim(1);
Action<object, EventArgs> onListening = (s, e) =>
{
semaphore.Release();
};
using (var serverData = ServerUtilities.CreateServer(compilerHost: host, onListening: onListening))
{
// Send all the requests.
var clients = new List<Client>();
for (var i = 0; i < requestCount; i++)
{
// Wait for the server to start listening.
await semaphore.WaitAsync(TimeSpan.FromMinutes(1));
var client = await Client.ConnectAsync(serverData.PipeName, timeout: null, cancellationToken: default);
await EmptyServerRequest.WriteAsync(client.Stream);
clients.Add(client);
}
// Act
// Wait until all of the connections are being processed by the server.
await completionSource.Task;
// Now cancel
var stats = await serverData.CancelAndCompleteAsync();
// Assert
Assert.Equal(requestCount, stats.Connections);
Assert.Equal(requestCount, count);
// Read the server response to each client.
foreach (var client in clients)
{
var task = ServerResponse.ReadAsync(client.Stream);
// We expect this to throw because the stream is already closed.
await Assert.ThrowsAnyAsync<IOException>(() => task);
client.Dispose();
}
}
}
private static TestableCompilerHost CreateCompilerHost(Action<TestableCompilerHost> configureCompilerHost = null)
{
var compilerHost = new TestableCompilerHost();
configureCompilerHost?.Invoke(compilerHost);
return compilerHost;
}
private class TestableCompilerHost : CompilerHost
{
internal Func<ServerRequest, CancellationToken, ServerResponse> ExecuteFunc;
public override ServerResponse Execute(ServerRequest request, CancellationToken cancellationToken)
{
if (ExecuteFunc != null)
{
return ExecuteFunc(request, cancellationToken);
}
return EmptyServerResponse;
}
}
}
}