-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
WindowsServiceLifetimeTests.cs
350 lines (290 loc) · 15 KB
/
WindowsServiceLifetimeTests.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Xunit;
namespace Microsoft.Extensions.Hosting
{
public class WindowsServiceLifetimeTests
{
private static bool IsRemoteExecutorSupportedAndPrivilegedProcess => RemoteExecutor.IsSupported && PlatformDetection.IsPrivilegedProcess;
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
public void ServiceStops()
{
using var serviceTester = WindowsServiceTester.Create(async () =>
{
var applicationLifetime = new ApplicationLifetime(NullLogger<ApplicationLifetime>.Instance);
using var lifetime = new WindowsServiceLifetime(
new HostingEnvironment(),
applicationLifetime,
NullLoggerFactory.Instance,
new OptionsWrapper<HostOptions>(new HostOptions()));
await lifetime.WaitForStartAsync(CancellationToken.None);
// would normally occur here, but WindowsServiceLifetime does not depend on it.
// applicationLifetime.NotifyStarted();
// will be signaled by WindowsServiceLifetime when SCM stops the service.
applicationLifetime.ApplicationStopping.WaitHandle.WaitOne();
// required by WindowsServiceLifetime to identify that app has stopped.
applicationLifetime.NotifyStopped();
await lifetime.StopAsync(CancellationToken.None);
});
serviceTester.Start();
serviceTester.WaitForStatus(ServiceControllerStatus.Running);
var statusEx = serviceTester.QueryServiceStatusEx();
var serviceProcess = Process.GetProcessById(statusEx.dwProcessId);
serviceTester.Stop();
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
serviceProcess.WaitForExit();
var status = serviceTester.QueryServiceStatus();
Assert.Equal(0, status.win32ExitCode);
}
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework is missing the fix from https://github.com/dotnet/corefx/commit/3e68d791066ad0fdc6e0b81828afbd9df00dd7f8")]
public void ExceptionOnStartIsPropagated()
{
using var serviceTester = WindowsServiceTester.Create(async () =>
{
using (var lifetime = ThrowingWindowsServiceLifetime.Create(throwOnStart: new Exception("Should be thrown")))
{
Assert.Equal(lifetime.ThrowOnStart,
await Assert.ThrowsAsync<Exception>(async () =>
await lifetime.WaitForStartAsync(CancellationToken.None)));
}
});
serviceTester.Start();
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
var status = serviceTester.QueryServiceStatus();
Assert.Equal(Interop.Errors.ERROR_EXCEPTION_IN_SERVICE, status.win32ExitCode);
}
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
public void ExceptionOnStopIsPropagated()
{
using var serviceTester = WindowsServiceTester.Create(async () =>
{
using (var lifetime = ThrowingWindowsServiceLifetime.Create(throwOnStop: new Exception("Should be thrown")))
{
await lifetime.WaitForStartAsync(CancellationToken.None);
lifetime.ApplicationLifetime.NotifyStopped();
Assert.Equal(lifetime.ThrowOnStop,
await Assert.ThrowsAsync<Exception>(async () =>
await lifetime.StopAsync(CancellationToken.None)));
}
});
serviceTester.Start();
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
var status = serviceTester.QueryServiceStatus();
Assert.Equal(Interop.Errors.ERROR_PROCESS_ABORTED, status.win32ExitCode);
}
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
public void CancelStopAsync()
{
using var serviceTester = WindowsServiceTester.Create(async () =>
{
var applicationLifetime = new ApplicationLifetime(NullLogger<ApplicationLifetime>.Instance);
using var lifetime = new WindowsServiceLifetime(
new HostingEnvironment(),
applicationLifetime,
NullLoggerFactory.Instance,
new OptionsWrapper<HostOptions>(new HostOptions()));
await lifetime.WaitForStartAsync(CancellationToken.None);
await Assert.ThrowsAsync<OperationCanceledException>(async () => await lifetime.StopAsync(new CancellationToken(true)));
});
serviceTester.Start();
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
var status = serviceTester.QueryServiceStatus();
Assert.Equal(Interop.Errors.ERROR_PROCESS_ABORTED, status.win32ExitCode);
}
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
public void ServiceCanStopItself()
{
using (var serviceTester = WindowsServiceTester.Create(async () =>
{
FileLogger.InitializeForTestCase(nameof(ServiceCanStopItself));
using IHost host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddHostedService<LoggingBackgroundService>();
services.AddSingleton<IHostLifetime, LoggingWindowsServiceLifetime>();
})
.Build();
var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
applicationLifetime.ApplicationStarted.Register(() => FileLogger.Log($"lifetime started"));
applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping"));
applicationLifetime.ApplicationStopped.Register(() => FileLogger.Log($"lifetime stopped"));
FileLogger.Log("host.Start()");
host.Start();
using (ServiceController selfController = new(nameof(ServiceCanStopItself)))
{
selfController.WaitForStatus(ServiceControllerStatus.Running, WindowsServiceTester.WaitForStatusTimeout);
Assert.Equal(ServiceControllerStatus.Running, selfController.Status);
FileLogger.Log("host.Stop()");
await host.StopAsync();
FileLogger.Log("host.Stop() complete");
selfController.WaitForStatus(ServiceControllerStatus.Stopped, WindowsServiceTester.WaitForStatusTimeout);
Assert.Equal(ServiceControllerStatus.Stopped, selfController.Status);
}
}))
{
FileLogger.DeleteLog(nameof(ServiceCanStopItself));
// service should start cleanly
serviceTester.Start();
// service will proceed to stopped without any error
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
var status = serviceTester.QueryServiceStatus();
Assert.Equal(0, status.win32ExitCode);
}
var logText = FileLogger.ReadLog(nameof(ServiceCanStopItself));
Assert.Equal("""
host.Start()
WindowsServiceLifetime.OnStart
BackgroundService.StartAsync
lifetime started
host.Stop()
lifetime stopping
BackgroundService.StopAsync
lifetime stopped
WindowsServiceLifetime.OnStop
host.Stop() complete
""", logText);
}
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
public void ServiceSequenceIsCorrect()
{
using (var serviceTester = WindowsServiceTester.Create(() =>
{
FileLogger.InitializeForTestCase(nameof(ServiceSequenceIsCorrect));
using IHost host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddHostedService<LoggingBackgroundService>();
services.AddSingleton<IHostLifetime, LoggingWindowsServiceLifetime>();
})
.Build();
var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
applicationLifetime.ApplicationStarted.Register(() => FileLogger.Log($"lifetime started"));
applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping"));
applicationLifetime.ApplicationStopped.Register(() => FileLogger.Log($"lifetime stopped"));
FileLogger.Log("host.Run()");
host.Run();
FileLogger.Log("host.Run() complete");
}))
{
FileLogger.DeleteLog(nameof(ServiceSequenceIsCorrect));
serviceTester.Start();
serviceTester.WaitForStatus(ServiceControllerStatus.Running);
var statusEx = serviceTester.QueryServiceStatusEx();
var serviceProcess = Process.GetProcessById(statusEx.dwProcessId);
// Give a chance for all asynchronous "started" events to be raised, these happen after the service status changes to started
Thread.Sleep(1000);
serviceTester.Stop();
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
var status = serviceTester.QueryServiceStatus();
Assert.Equal(0, status.win32ExitCode);
}
var logText = FileLogger.ReadLog(nameof(ServiceSequenceIsCorrect));
Assert.Equal("""
host.Run()
WindowsServiceLifetime.OnStart
BackgroundService.StartAsync
lifetime started
WindowsServiceLifetime.OnStop
lifetime stopping
BackgroundService.StopAsync
lifetime stopped
host.Run() complete
""", logText);
}
public class LoggingWindowsServiceLifetime : WindowsServiceLifetime
{
public LoggingWindowsServiceLifetime(IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions<HostOptions> optionsAccessor) :
base(environment, applicationLifetime, loggerFactory, optionsAccessor)
{ }
protected override void OnStart(string[] args)
{
FileLogger.Log("WindowsServiceLifetime.OnStart");
base.OnStart(args);
}
protected override void OnStop()
{
FileLogger.Log("WindowsServiceLifetime.OnStop");
base.OnStop();
}
}
public class ThrowingWindowsServiceLifetime : WindowsServiceLifetime
{
public static ThrowingWindowsServiceLifetime Create(Exception throwOnStart = null, Exception throwOnStop = null) =>
new ThrowingWindowsServiceLifetime(
new HostingEnvironment(),
new ApplicationLifetime(NullLogger<ApplicationLifetime>.Instance),
NullLoggerFactory.Instance,
new OptionsWrapper<HostOptions>(new HostOptions()))
{
ThrowOnStart = throwOnStart,
ThrowOnStop = throwOnStop
};
public ThrowingWindowsServiceLifetime(IHostEnvironment environment, ApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions<HostOptions> optionsAccessor) :
base(environment, applicationLifetime, loggerFactory, optionsAccessor)
{
ApplicationLifetime = applicationLifetime;
}
public ApplicationLifetime ApplicationLifetime { get; }
public Exception ThrowOnStart { get; set; }
protected override void OnStart(string[] args)
{
if (ThrowOnStart != null)
{
throw ThrowOnStart;
}
base.OnStart(args);
}
public Exception ThrowOnStop { get; set; }
protected override void OnStop()
{
if (ThrowOnStop != null)
{
throw ThrowOnStop;
}
base.OnStop();
}
}
public class LoggingBackgroundService : BackgroundService
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
protected override async Task ExecuteAsync(CancellationToken stoppingToken) => FileLogger.Log("BackgroundService.ExecuteAsync");
public override async Task StartAsync(CancellationToken stoppingToken) => FileLogger.Log("BackgroundService.StartAsync");
public override async Task StopAsync(CancellationToken stoppingToken) => FileLogger.Log("BackgroundService.StopAsync");
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
static class FileLogger
{
static string _fileName;
public static void InitializeForTestCase(string testCaseName)
{
Assert.Null(_fileName);
_fileName = GetLogForTestCase(testCaseName);
}
private static string GetLogForTestCase(string testCaseName) => Path.Combine(AppContext.BaseDirectory, $"{testCaseName}.log");
public static void DeleteLog(string testCaseName) => File.Delete(GetLogForTestCase(testCaseName));
public static string ReadLog(string testCaseName) => File.ReadAllText(GetLogForTestCase(testCaseName));
public static void Log(string message)
{
Assert.NotNull(_fileName);
lock (_fileName)
{
File.AppendAllText(_fileName, message + Environment.NewLine);
}
}
}
}
}