Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tests in WindowsServiceLifetimeTests #105666

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void ServiceCanStopItself()
serviceTester.Start();

// service will proceed to stopped without any error
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped);
serviceTester.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.MaxValue);

var status = serviceTester.QueryServiceStatus();
Assert.Equal(0, status.win32ExitCode);
Expand All @@ -194,6 +194,8 @@ lifetime stopped
""", logText);
}

private static AutoResetEvent s_are = new AutoResetEvent(false);

[ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
public void ServiceSequenceIsCorrect()
{
Expand All @@ -208,8 +210,16 @@ public void ServiceSequenceIsCorrect()
})
.Build();

var windowsService = (LoggingWindowsServiceLifetime)host.Services.GetRequiredService<IHostLifetime>();
windowsService.WaitOnStop = true;

var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
applicationLifetime.ApplicationStarted.Register(() => FileLogger.Log($"lifetime started"));
applicationLifetime.ApplicationStarted.Register(() =>
{
FileLogger.Log($"lifetime started");
s_are.Set();
});

applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping"));
applicationLifetime.ApplicationStopped.Register(() => FileLogger.Log($"lifetime stopped"));

Expand Down Expand Up @@ -260,6 +270,8 @@ public LoggingWindowsServiceLifetime(IHostEnvironment environment, IHostApplicat
base(environment, applicationLifetime, loggerFactory, optionsAccessor)
{ }

public bool WaitOnStop { get; set; }

protected override void OnStart(string[] args)
{
FileLogger.Log("WindowsServiceLifetime.OnStart");
Expand All @@ -268,6 +280,10 @@ protected override void OnStart(string[] args)

protected override void OnStop()
{
if (WaitOnStop)
{
s_are.WaitOne();
}
FileLogger.Log("WindowsServiceLifetime.OnStop");
base.OnStop();
}
Expand Down
Loading