From 982b4b61205b6656590b497dd92327d90cb81e6f Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Mon, 29 Jul 2024 11:49:30 -0700 Subject: [PATCH 1/2] Fix flaky tests in WindowsServiceLifetimeTests --- .../tests/WindowsServiceLifetimeTests.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs index f275b47d70921..be6129ee111f0 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs @@ -144,7 +144,11 @@ public void ServiceCanStopItself() .Build(); var applicationLifetime = host.Services.GetRequiredService(); - applicationLifetime.ApplicationStarted.Register(() => FileLogger.Log($"lifetime started")); + applicationLifetime.ApplicationStarted.Register(() => + { + FileLogger.Log($"lifetime started"); + are.Set(); + }); applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping")); applicationLifetime.ApplicationStopped.Register(() => FileLogger.Log($"lifetime stopped")); @@ -171,7 +175,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); @@ -194,6 +198,8 @@ lifetime stopped """, logText); } + private static AutoResetEvent are = new AutoResetEvent(false); + [ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))] public void ServiceSequenceIsCorrect() { @@ -209,7 +215,12 @@ public void ServiceSequenceIsCorrect() .Build(); var applicationLifetime = host.Services.GetRequiredService(); - applicationLifetime.ApplicationStarted.Register(() => FileLogger.Log($"lifetime started")); + applicationLifetime.ApplicationStarted.Register(() => + { + FileLogger.Log($"lifetime started"); + are.Set(); + }); + applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping")); applicationLifetime.ApplicationStopped.Register(() => FileLogger.Log($"lifetime stopped")); @@ -268,6 +279,7 @@ protected override void OnStart(string[] args) protected override void OnStop() { + are.WaitOne(); FileLogger.Log("WindowsServiceLifetime.OnStop"); base.OnStop(); } From 572974bf019eef0878eada3022750c1d50d13726 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 1 Aug 2024 10:14:36 -0700 Subject: [PATCH 2/2] Apply feedback --- .../tests/WindowsServiceLifetimeTests.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs index be6129ee111f0..27a56fca3d510 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceLifetimeTests.cs @@ -144,11 +144,7 @@ public void ServiceCanStopItself() .Build(); var applicationLifetime = host.Services.GetRequiredService(); - applicationLifetime.ApplicationStarted.Register(() => - { - FileLogger.Log($"lifetime started"); - are.Set(); - }); + applicationLifetime.ApplicationStarted.Register(() => FileLogger.Log($"lifetime started")); applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping")); applicationLifetime.ApplicationStopped.Register(() => FileLogger.Log($"lifetime stopped")); @@ -198,7 +194,7 @@ lifetime stopped """, logText); } - private static AutoResetEvent are = new AutoResetEvent(false); + private static AutoResetEvent s_are = new AutoResetEvent(false); [ConditionalFact(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))] public void ServiceSequenceIsCorrect() @@ -214,11 +210,14 @@ public void ServiceSequenceIsCorrect() }) .Build(); + var windowsService = (LoggingWindowsServiceLifetime)host.Services.GetRequiredService(); + windowsService.WaitOnStop = true; + var applicationLifetime = host.Services.GetRequiredService(); applicationLifetime.ApplicationStarted.Register(() => { FileLogger.Log($"lifetime started"); - are.Set(); + s_are.Set(); }); applicationLifetime.ApplicationStopping.Register(() => FileLogger.Log($"lifetime stopping")); @@ -271,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"); @@ -279,7 +280,10 @@ protected override void OnStart(string[] args) protected override void OnStop() { - are.WaitOne(); + if (WaitOnStop) + { + s_are.WaitOne(); + } FileLogger.Log("WindowsServiceLifetime.OnStop"); base.OnStop(); }