Skip to content

Commit dcc0d25

Browse files
authored
Make service wait on its state before stopping (dotnet#84447)
1 parent 17629fc commit dcc0d25

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ public void ServiceCanStopItself()
148148
FileLogger.Log("host.Start()");
149149
host.Start();
150150

151-
FileLogger.Log("host.Stop()");
152-
await host.StopAsync();
153-
FileLogger.Log("host.Stop() complete");
151+
using (ServiceController selfController = new(nameof(ServiceCanStopItself)))
152+
{
153+
selfController.WaitForStatus(ServiceControllerStatus.Running, WindowsServiceTester.WaitForStatusTimeout);
154+
Assert.Equal(ServiceControllerStatus.Running, selfController.Status);
155+
156+
FileLogger.Log("host.Stop()");
157+
await host.StopAsync();
158+
FileLogger.Log("host.Stop() complete");
159+
160+
selfController.WaitForStatus(ServiceControllerStatus.Stopped, WindowsServiceTester.WaitForStatusTimeout);
161+
Assert.Equal(ServiceControllerStatus.Stopped, selfController.Status);
162+
}
154163
}))
155164
{
156165
FileLogger.DeleteLog(nameof(ServiceCanStopItself));

src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private WindowsServiceTester(SafeServiceHandle serviceHandle, RemoteInvokeHandle
4848
{ }
4949
}
5050

51-
public TimeSpan WaitForStatusTimeout { get; set; } = TimeSpan.FromSeconds(30);
51+
public static TimeSpan WaitForStatusTimeout { get; set; } = TimeSpan.FromSeconds(30);
5252

5353
public new void WaitForStatus(ServiceControllerStatus desiredStatus) =>
5454
WaitForStatus(desiredStatus, WaitForStatusTimeout);

0 commit comments

Comments
 (0)