Skip to content

Commit

Permalink
Make service wait on its state before stopping (#84447)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericstj authored Apr 7, 2023
1 parent 17629fc commit dcc0d25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,18 @@ public void ServiceCanStopItself()
FileLogger.Log("host.Start()");
host.Start();

FileLogger.Log("host.Stop()");
await host.StopAsync();
FileLogger.Log("host.Stop() complete");
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private WindowsServiceTester(SafeServiceHandle serviceHandle, RemoteInvokeHandle
{ }
}

public TimeSpan WaitForStatusTimeout { get; set; } = TimeSpan.FromSeconds(30);
public static TimeSpan WaitForStatusTimeout { get; set; } = TimeSpan.FromSeconds(30);

public new void WaitForStatus(ServiceControllerStatus desiredStatus) =>
WaitForStatus(desiredStatus, WaitForStatusTimeout);
Expand Down

0 comments on commit dcc0d25

Please sign in to comment.