From d7d6604117a40d16b3b637e4900dcba13124535f Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 1 Mar 2022 12:19:55 -0800 Subject: [PATCH] Try to stabilize a few tests that fail intermittently --- .../Enter-PSHostProcess.Tests.ps1 | 32 ++++++++----------- .../Get-WinEvent.Tests.ps1 | 2 +- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 index a4898b1e1ad3..7e9469613bb0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 @@ -34,32 +34,26 @@ function Invoke-PSHostProcessScript { [int] $Retry = 5 # Default retry of 5 times ) - $sb = { - # use $i as an incrementally growing pause based on the attempt number - # so that it's more likely to succeed. - $commandStr = @' -Start-Sleep -Seconds {0} -Enter-PSHostProcess {1} -ErrorAction Stop + $commandStr = @' +Enter-PSHostProcess {0} -ErrorAction Stop $PID Exit-PSHostProcess -'@ -f $i, $ArgumentString - - ($commandStr | & $powershell -c -) -eq $Id - } +'@ -f $ArgumentString $result = $false - $failures = 0 foreach ($i in 1..$Retry) { - if ($sb.Invoke()) { - $result = $true + # use $i as an incrementally growing pause based on the attempt number + # so that it's more likely to succeed. + Start-Sleep -Seconds $i + + $result = ($commandStr | & $powershell -noprofile -c -) -eq $Id + if ($result) { break } - - $failures++ } - if($failures) { - Write-Warning "Enter-PSHostProcess script failed $i out of $Retry times." + if ($i -gt 1) { + Write-Verbose -Verbose "Enter-PSHostProcess script failed $i out of $Retry times." } $result @@ -99,7 +93,7 @@ Describe "Enter-PSHostProcess tests" -Tag Feature { # This will enter and exit another process Invoke-PSHostProcessScript -ArgumentString "-Id $pwshId" -Id $pwshId | - Should -BeTrue -Because "The script was able to enter another process and grab the pid of '$pwshId'." + Should -BeTrue -Because "The script was able to enter another process and grab the pid of '$pwshId'" # Re-enter and exit the other process Invoke-PSHostProcessScript -ArgumentString "-Id $pwshId" -Id $pwshId | @@ -109,7 +103,7 @@ Describe "Enter-PSHostProcess tests" -Tag Feature { It "Can enter, exit, and re-enter another Windows PowerShell PSHost" -Skip:(!$IsWindows) { # Start a PowerShell job where the first thing it does is return $PID. After that, spin forever. # We will use this job as the target process for Enter-PSHostProcess - $powershellJob = Start-Job { + $powershellJob = Start-Job -PSVersion 5.1 { $PID while ($true) { Start-Sleep -Seconds 30 | Out-Null diff --git a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 index 3d881d3b2da9..5a321539f146 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 @@ -44,7 +44,7 @@ Describe 'Get-WinEvent' -Tags "CI" { # we sample the first 20 results, as this could be very large $results = Get-WinEvent -provider $providerForTests.Name -max 20 foreach($event in $results ) { - $event.providername | Should -BeExactly $providerForTests.name + $event.providername | Should -Be $providerForTests.name } } It 'Get-WinEvent can get events via logname' {