MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Wait-Process" {5 6 BeforeAll {7 $pingCommandPath = (Get-Command -CommandType Application ping)[0].Definition8 9 $startProcessArgs = @{10 FilePath = $pingCommandPath11 PassThru = $true12 } 13 $nc = $IsWindows ? "-n" : "-c"14 15 function longPing { Start-Process @startProcessArgs -ArgumentList "$nc 10 localhost" }16 function shortPing { Start-Process @startProcessArgs -ArgumentList "$nc 2 localhost" }17 }18 19 BeforeEach {20 $Processes = @( 1..3 | ForEach-Object { longPing } ) + ($shortPing = shortPing)21 }22 23 AfterEach {24 Stop-Process -InputObject $Processes25 }26 27 It "Should wait until all processes have exited" {28 Wait-Process -InputObject $Processes29 30 $Processes.Where({$_.HasExited -eq $true}).Count | Should -Be $Processes.Count31 }32 33 It "Should return after all processes have exited, even if some exit before the wait starts." {34 Wait-UntilTrue -sb { $shortPing.HasExited -eq $true } -IntervalInMilliseconds 10035 Wait-Process -InputObject $Processes36 37 $Processes.Where({$_.HasExited -eq $true}).Count | Should -Be $Processes.Count38 }39 40 It "Should return immediately if all processes have exited before the wait starts" {41 Wait-UntilTrue -sb { $Processes.HasExited -NotContains $false } -IntervalInMilliseconds 10042 Wait-Process -InputObject $Processes43 44 $Processes.Where({$_.HasExited -eq $true}).Count | Should -Be $Processes.Count45 }46 47 It "Should return immediately if at least one process has exited before the wait starts" {48 Wait-UntilTrue -sb { $shortPing.HasExited -eq $true } -IntervalInMilliseconds 10049 Wait-Process -InputObject $Processes -Any50 51 $Processes.Where({$_.HasExited -eq $true}).Count | Should -Be 152 $Processes.Where({$_.HasExited -eq $false}).Count | Should -Be ($Processes.Count - 1)53 }54 55 It "Should wait until any one process has exited" {56 Wait-Process -InputObject $Processes -Any57 58 $Processes.Where({$_.HasExited -eq $true}).Count | Should -Be 159 $Processes.Where({$_.HasExited -eq $false}).Count | Should -Be ($Processes.Count - 1)60 }61 62 It "Should passthru all processes when all processes have exited" {63 $PassThruProcesses = Wait-Process -InputObject $Processes -PassThru64 65 $PassThruProcesses.Where({$_.HasExited -eq $true}).Count | Should -Be $Processes.Count66 }67 68 It "Should passthru all processes when any one process has exited" {69 $PassThruProcesses = Wait-Process -InputObject $Processes -Any -PassThru70 71 $PassThruProcesses.Where({$_.HasExited -eq $true}).Count | Should -Be 172 $PassThruProcesses.Where({$_.HasExited -eq $false}).Count | Should -Be ($Processes.Count - 1)73 }74}75 