diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index 2cbb8b7a487..f9dc2880074 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -196,19 +196,13 @@ Categories=Application; elseif ($IsLinux) { # Validate on Linux by reassociating default app for text file & $TestFile - $startTime = Get-Date # It may take time for handler to start - while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/nativeCommandProcessor.Success"))) { - Start-Sleep -Milliseconds 100 - } + Wait-FileToBePresent -File "$HOME/nativeCommandProcessor.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 Get-Content $HOME/nativeCommandProcessor.Success | Should Be $TestFile } else { & $TestFile - $startTime = Get-Date - while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $TestDrive\foo.txt))) { - Start-Sleep -Milliseconds 100 - } + Wait-FileToBePresent -File $TestDrive\foo.txt -TimeoutInSeconds 10 -IntervalInMilliseconds 100 "$TestDrive\foo.txt" | Should Exist Get-Content $TestDrive\foo.txt | Should BeExactly $TestFile } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 6ef50f25bab..9d460f16f1b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -233,12 +233,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { "Test timeout waiting for $cmdLine" | Set-Content -Path $errFile runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline -ErrorAction Stop } catch { `$_.FullyQualifiedErrorId | Out-File $errFile }; New-Item -Type File -Path $doneFile" - - $startTime = Get-Date - while (((Get-Date) - $startTime).TotalSeconds -lt 15 -and -not (Test-Path $doneFile)) - { - Start-Sleep -Milliseconds 100 - } + Wait-FileToBePresent -File $doneFile -TimeoutInSeconds 15 -IntervalInMilliseconds 100 $err = Get-Content $errFile $err | Should Be $expectedError diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 index 09ad4e1f4fa..ba5f3583923 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 @@ -133,11 +133,9 @@ Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWi $fooFile = Join-Path $TestDrive "FooTest.foo" New-Item $fooFile -ItemType File -Force Start-Process $fooFile - $startTime = Get-Date - while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $testdrive\foo.txt))) - { - Start-Sleep -Milliseconds 100 - } + + Wait-FileToBePresent -File "$testdrive\foo.txt" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 + "$testdrive\foo.txt" | Should Exist Get-Content $testdrive\foo.txt | Should BeExactly $fooFile } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 index 120b5d0d4ea..416b6387e6e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 @@ -125,12 +125,8 @@ Categories=Application; $before = $windows.Count Invoke-Item -Path $PSHOME - $startTime = Get-Date # may take time for explorer to open window - while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and ($windows.Count -eq $before)) - { - Start-Sleep -Milliseconds 100 - } + Wait-UntilTrue -sb { $windows.Count -gt $before } -TimeoutInMilliseconds (10*1000) -IntervalInMilliseconds 100 > $null $after = $windows.Count $before + 1 | Should Be $after @@ -143,12 +139,8 @@ Categories=Application; { # validate on Unix by reassociating default app for directories Invoke-Item -Path $PSHOME - $startTime = Get-Date # may take time for handler to start - while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/InvokeItemTest.Success"))) - { - Start-Sleep -Milliseconds 100 - } + Wait-FileToBePresent -File "$HOME/InvokeItemTest.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 Get-Content $HOME/InvokeItemTest.Success | Should Be $PSHOME } else diff --git a/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 b/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 index 09afa49b281..91f39511fc0 100644 --- a/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 @@ -100,11 +100,7 @@ Describe "CredSSP cmdlet tests" -Tags 'Feature','RequireAdminOnWindows' { param ($cmdline, $cmd) runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline } catch { `$_.FullyQualifiedErrorId | Out-File $errtxt }; New-Item -Type File -Path $donefile" - $startTime = Get-Date - while (((Get-Date) - $startTime).TotalSeconds -lt 5 -and -not (Test-Path "$donefile")) - { - Start-Sleep -Milliseconds 100 - } + Wait-FileToBePresent -File $donefile -TimeoutInSeconds 5 -IntervalInMilliseconds 100 $errtxt | Should Exist $err = Get-Content $errtxt $err | Should Be "System.InvalidOperationException,Microsoft.WSMan.Management.$cmd" diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 index 5367155d098..fbc067114fc 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 @@ -16,6 +16,6 @@ Copyright = 'Copyright (C) Microsoft Corporation, All rights reserved.' Description = 'Temporary module contains functions for using in tests' -FunctionsToExport = 'Wait-UntilTrue', 'Test-IsElevated', 'ShouldBeErrorId' +FunctionsToExport = 'Wait-UntilTrue', 'Test-IsElevated', 'ShouldBeErrorId', 'Wait-FileToBePresent' } diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index e3376c1231f..74e441b002c 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -11,18 +11,28 @@ function Wait-UntilTrue # Loop until the script block evaluates to true while (-not ($sb.Invoke())) { - # Sleep for the specified interval - start-sleep -mil $intervalInMilliseconds - - # If the timeout period has passed, throw an exception - if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) - { + # If the timeout period has passed, return false + if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) { return $false } + # Sleep for the specified interval + Start-Sleep -Milliseconds $intervalInMilliseconds } return $true } +function Wait-FileToBePresent +{ + [CmdletBinding()] + param ( + [string]$File, + [int]$TimeoutInSeconds = 10, + [int]$IntervalInMilliseconds = 100 + ) + + Wait-UntilTrue -sb { Test-Path $File } -TimeoutInMilliseconds ($TimeoutInSeconds*1000) -IntervalInMilliseconds $IntervalInMilliseconds > $null +} + function Test-IsElevated { $IsElevated = $False