2020-03-24 19:08:37 +01:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 18:23:53 +01:00
|
|
|
# Licensed under the MIT License.
|
2019-10-30 22:22:45 +01:00
|
|
|
|
2020-01-05 09:01:42 +01:00
|
|
|
Describe 'Basic Job Tests' -Tags 'Feature' {
|
2016-11-10 07:44:39 +01:00
|
|
|
BeforeAll {
|
2017-10-02 22:43:43 +02:00
|
|
|
# Make sure we do not have any jobs running
|
|
|
|
Get-Job | Remove-Job -Force
|
|
|
|
$timeBeforeStartedJob = Get-Date
|
2020-05-07 14:00:30 +02:00
|
|
|
$startedJob = Start-Job -Name 'StartedJob' -Scriptblock { 1 + 1 } | Wait-Job
|
2017-10-02 22:43:43 +02:00
|
|
|
$timeAfterStartedJob = Get-Date
|
|
|
|
|
|
|
|
function script:ValidateJobInfo($job, $state, $hasMoreData, $command)
|
|
|
|
{
|
2018-03-14 20:13:32 +01:00
|
|
|
$job.State | Should -BeExactly $state
|
|
|
|
$job.HasMoreData | Should -Be $hasMoreData
|
2017-10-02 22:43:43 +02:00
|
|
|
|
|
|
|
if($command -ne $null)
|
|
|
|
{
|
2018-03-14 20:13:32 +01:00
|
|
|
$job.Command | Should -BeExactly $command
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 07:44:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
AfterAll {
|
|
|
|
$startedJob | Remove-Job -Force -ErrorAction SilentlyContinue
|
2016-11-10 07:44:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
Context 'Basic tests' {
|
|
|
|
|
2019-09-11 19:51:32 +02:00
|
|
|
BeforeAll {
|
|
|
|
$invalidPathTestCases = @(
|
|
|
|
@{ path = "This is an invalid path"; case = "invalid path"; errorId = "DirectoryNotFoundException,Microsoft.PowerShell.Commands.StartJobCommand"}
|
|
|
|
@{ path = ""; case = "empty string"; errorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartJobCommand"}
|
|
|
|
@{ path = " "; case = "whitespace string (single space)"; errorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartJobCommand"}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
AfterEach {
|
|
|
|
Get-Job | Where-Object { $_.Id -ne $startedJob.Id } | Remove-Job -ErrorAction SilentlyContinue -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can start, wait and receive a Job' {
|
|
|
|
$job = Start-Job -ScriptBlock { 1 + 1 }
|
|
|
|
$result = $job | Wait-Job | Receive-Job
|
|
|
|
ValidateJobInfo -job $job -state 'Completed' -hasMoreData $false -command ' 1 + 1 '
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can run nested jobs' {
|
|
|
|
$job = Start-Job -ScriptBlock { Start-Job -ScriptBlock { 1 + 1 } | Wait-Job | Receive-Job }
|
|
|
|
ValidateJobInfo -job $job -state 'Running' -hasMoreData $true -command ' Start-Job -ScriptBlock { 1 + 1 } | Wait-Job | Receive-Job '
|
|
|
|
$result = $job | Wait-Job | Receive-Job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can get errors messages from job' {
|
|
|
|
$job = Start-Job -ScriptBlock { throw 'MyError' } | Wait-Job
|
|
|
|
Receive-Job -Job $job -ErrorVariable ev -ErrorAction SilentlyContinue
|
2018-03-14 20:13:32 +01:00
|
|
|
$ev[0].Exception.Message | Should -BeExactly 'MyError'
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can get warning messages from job' {
|
|
|
|
$job = Start-Job -ScriptBlock { Write-Warning 'MyWarning' } | Wait-Job
|
|
|
|
Receive-Job -Job $job -WarningVariable wv -WarningAction SilentlyContinue
|
2018-03-14 20:13:32 +01:00
|
|
|
$wv | Should -BeExactly 'MyWarning'
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can get verbose message from job' {
|
|
|
|
$job = Start-Job -ScriptBlock { Write-Verbose -Verbose 'MyVerbose' } | Wait-Job
|
|
|
|
$VerboseMsg = $job.ChildJobs[0].verbose.readall()
|
2018-03-14 20:13:32 +01:00
|
|
|
$VerboseMsg | Should -BeExactly 'MyVerbose'
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can get progress message from job' {
|
|
|
|
$job = Start-Job -ScriptBlock { Write-Progress -Activity 1 -Status 2 } | Wait-Job
|
|
|
|
$ProgressMsg = $job.ChildJobs[0].progress.readall()
|
2018-03-14 20:13:32 +01:00
|
|
|
$ProgressMsg[0].Activity | Should -BeExactly 1
|
|
|
|
$ProgressMsg[0].StatusDescription | Should -BeExactly 2
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 19:09:00 +01:00
|
|
|
It 'Can use the user specified working directory parameter with whitespace' {
|
|
|
|
$path = Join-Path -Path $TestDrive -ChildPath "My Dir"
|
|
|
|
$null = New-Item -ItemType Directory -Path "$path"
|
2020-01-13 20:24:10 +01:00
|
|
|
$job = Start-Job -ScriptBlock { $PWD } -WorkingDirectory $path | Wait-Job
|
2019-09-11 19:51:32 +02:00
|
|
|
$jobOutput = Receive-Job $job
|
2019-11-01 19:09:00 +01:00
|
|
|
$jobOutput | Should -BeExactly $path.ToString()
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can use the user specified working directory parameter with quote' -Skip:($IsWindows) {
|
|
|
|
$path = Join-Path -Path $TestDrive -ChildPath "My ""Dir"
|
|
|
|
$null = New-Item -ItemType Directory -Path "$path"
|
2020-01-13 20:24:10 +01:00
|
|
|
$job = Start-Job -ScriptBlock { $PWD } -WorkingDirectory $path | Wait-Job
|
2019-11-01 19:09:00 +01:00
|
|
|
$jobOutput = Receive-Job $job
|
|
|
|
$jobOutput | Should -BeExactly $path.ToString()
|
2019-09-11 19:51:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-16 02:05:27 +01:00
|
|
|
It 'Verifies the working directory parameter path with trailing backslash' -Skip:(! $IsWindows) {
|
2020-01-13 20:24:10 +01:00
|
|
|
$job = Start-Job { $PWD } -WorkingDirectory '\' | Wait-Job
|
2019-11-16 02:05:27 +01:00
|
|
|
$job.JobStateInfo.State | Should -BeExactly 'Completed'
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:51:32 +02:00
|
|
|
It 'Throws an error when the working directory parameter is <case>' -TestCases $invalidPathTestCases {
|
|
|
|
param($path, $case, $expectedErrorId)
|
|
|
|
|
|
|
|
{Start-Job -ScriptBlock { 1 + 1 } -WorkingDirectory $path} | Should -Throw -ErrorId $expectedErrorId
|
|
|
|
}
|
|
|
|
|
2019-10-30 22:22:45 +01:00
|
|
|
It 'Verifies that the current working directory is preserved' {
|
2020-01-13 20:24:10 +01:00
|
|
|
$job = Start-Job -ScriptBlock { $PWD }
|
2019-10-30 22:22:45 +01:00
|
|
|
$location = $job | Wait-Job | Receive-Job
|
|
|
|
$job | Remove-Job
|
2020-01-13 20:24:10 +01:00
|
|
|
$location.Path | Should -BeExactly $PWD.Path
|
2019-10-30 22:22:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
It "Create job with native command" {
|
|
|
|
try {
|
2020-05-07 14:00:30 +02:00
|
|
|
$nativeJob = Start-Job { & "$PSHOME/pwsh" -c 1+1 }
|
2017-10-02 22:43:43 +02:00
|
|
|
$nativeJob | Wait-Job
|
2018-03-14 20:13:32 +01:00
|
|
|
$nativeJob.State | Should -BeExactly "Completed"
|
|
|
|
$nativeJob.HasMoreData | Should -BeTrue
|
|
|
|
Receive-Job $nativeJob | Should -BeExactly 2
|
2017-10-02 22:43:43 +02:00
|
|
|
Remove-Job $nativeJob
|
2018-05-17 23:42:04 +02:00
|
|
|
{ Get-Job $nativeJob -ErrorAction Stop } | Should -Throw -ErrorId "JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.GetJobCommand"
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Remove-Job $nativeJob -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 07:44:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
Context 'Wait-Job tests' {
|
|
|
|
|
|
|
|
BeforeAll {
|
|
|
|
$waitJobTestCases = @(
|
|
|
|
@{ parameters = @{ Name = $startedJob.Name } ; property = '-Name'},
|
|
|
|
@{ parameters = @{ Id = $startedJob.Id } ; property = '-Id'},
|
|
|
|
@{ parameters = @{ Job = $startedJob } ; property = '-Job'},
|
|
|
|
@{ parameters = @{ InstanceId = $startedJob.InstanceId } ; property = '-InstanceId'},
|
|
|
|
@{ parameters = @{ State = $startedJob.State } ; property = '-State'}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
AfterEach {
|
|
|
|
Get-Job | Where-Object { $_.Id -ne $startedJob.Id } | Remove-Job -ErrorAction SilentlyContinue -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can wait for jobs to complete using <property>' -TestCases $waitJobTestCases {
|
|
|
|
param($parameters)
|
|
|
|
$job = Wait-Job @parameters
|
|
|
|
ValidateJobInfo -job $job -state 'Completed' -hasMoreData $true -command ' 1 + 1 '
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can wait for any job to complete' {
|
|
|
|
$jobs = 1..3 | ForEach-Object { $seconds = $_ ; Start-Job -ScriptBlock { Start-Sleep -Seconds $using:seconds ; $using:seconds} }
|
|
|
|
$waitedJob = Wait-Job -Job $jobs -Any
|
|
|
|
ValidateJobInfo -job $waitedJob -state 'Completed' -hasMoreData $true -command ' Start-Sleep -Seconds $using:seconds ; $using:seconds'
|
|
|
|
$result = $waitedJob | Receive-Job
|
|
|
|
## We check for $result to be less than 4 so that any of the jobs completing first will considered a success.
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -BeLessThan 4
|
2017-10-02 22:43:43 +02:00
|
|
|
## Check none of the jobs threw errors.
|
2018-03-14 20:13:32 +01:00
|
|
|
$jobs.Error | Should -BeNullOrEmpty
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can timeout waiting for a job' {
|
|
|
|
$job = Start-Job -ScriptBlock { Start-Sleep -Seconds 10 }
|
|
|
|
$job | Wait-Job -TimeoutSec 1
|
|
|
|
ValidateJobInfo -job $job -state 'Running' -hasMoreData $true -command ' Start-Sleep -Seconds 10 '
|
|
|
|
}
|
2016-11-10 07:44:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
Context 'Receive-job tests' {
|
|
|
|
It 'Can Receive-Job with state change events' {
|
|
|
|
$result = Start-Job -Name 'ReceiveWriteEventsJob' -ScriptBlock { 1 + 1 } | Receive-Job -Wait -WriteEvents
|
2018-03-14 20:13:32 +01:00
|
|
|
$result.Count | Should -Be 3
|
|
|
|
$result[0] | Should -Be 2
|
|
|
|
$result[1].GetType().FullName | Should -BeExactly 'System.Management.Automation.JobStateEventArgs'
|
2017-09-29 02:15:37 +02:00
|
|
|
}
|
2017-10-02 22:43:43 +02:00
|
|
|
|
|
|
|
It 'Can Receive-Job with job object and result' {
|
|
|
|
$result = Start-Job -ScriptBlock { 1 + 1 } | Receive-Job -Wait -WriteJobInResults
|
2018-03-14 20:13:32 +01:00
|
|
|
$result.Count | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
ValidateJobInfo -job $result[0] -command ' 1 + 1 ' -state 'Completed' -hasMoreData $false
|
2018-03-14 20:13:32 +01:00
|
|
|
$result[1] | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
$result[0] | Remove-Job -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Receive-Job and autoremove' {
|
|
|
|
$result = Start-Job -Name 'ReceiveJobAutoRemove' -ScriptBlock { 1 + 1 } | Receive-Job -Wait -AutoRemoveJob
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2018-05-17 23:42:04 +02:00
|
|
|
{ Get-Job -Name 'ReceiveJobAutoRemove' -ErrorAction Stop } | Should -Throw -ErrorId 'JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.GetJobCommand'
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Receive-Job and keep results' {
|
|
|
|
$job = Start-Job -ScriptBlock { 1 + 1 } | Wait-Job
|
|
|
|
$result = Receive-Job -Keep -Job $job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
$result2 = Receive-Job -Job $job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result2 | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
$result3 = Receive-Job -Job $job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result3 | Should -BeNullOrEmpty
|
2017-10-02 22:43:43 +02:00
|
|
|
$job | Remove-Job -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Receive-Job with NoRecurse' {
|
|
|
|
$job = Start-Job -ScriptBlock { 1 + 1 }
|
|
|
|
$result = Receive-Job -Wait -NoRecurse -Job $job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -BeNullOrEmpty
|
2017-10-02 22:43:43 +02:00
|
|
|
$job | Remove-Job -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Receive-Job using ComputerName' {
|
|
|
|
$jobName = 'ReceiveUsingComputerName'
|
|
|
|
$job = Start-Job -ScriptBlock { 1 + 1 } -Name $jobName | Wait-Job
|
|
|
|
$result = Receive-Job -ComputerName localhost -Job $job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
$job | Remove-Job -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Receive-Job using Location' {
|
|
|
|
$jobName = 'ReceiveUsingLocation'
|
|
|
|
$job = Start-Job -ScriptBlock { 1 + 1 } -Name $jobName | Wait-Job
|
|
|
|
$result = Receive-Job -Location localhost -Job $job
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
$job | Remove-Job -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can receive a job with -wait switch' {
|
|
|
|
$job = Start-Job -ScriptBlock { 1 + 1 }
|
|
|
|
$result = $job | Receive-Job -Wait
|
|
|
|
ValidateJobInfo -job $job -state 'Completed' -hasMoreData $false -command ' 1 + 1 '
|
2018-03-14 20:13:32 +01:00
|
|
|
$result | Should -Be 2
|
2017-09-29 02:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
Context 'Get-Job tests' {
|
|
|
|
BeforeAll {
|
|
|
|
$getJobTestCases = @(
|
|
|
|
@{ parameters = @{ Name = $startedJob.Name } ; property = 'Name'},
|
|
|
|
@{ parameters = @{ Id = $startedJob.Id } ; property = 'Id'},
|
|
|
|
@{ parameters = @{ InstanceId = $startedJob.InstanceId } ; property = 'InstanceId'},
|
|
|
|
@{ parameters = @{ State = $startedJob.State } ; property = 'State'}
|
|
|
|
)
|
|
|
|
|
|
|
|
$getJobSwitches = @(
|
|
|
|
@{ parameters = @{ Before = $timeAfterStartedJob }; property = '-Before'},
|
|
|
|
@{ parameters = @{ After = $timeBeforeStartedJob }; property = '-After'},
|
|
|
|
@{ parameters = @{ HasMoreData = $true }; property = '-HasMoreData'}
|
|
|
|
)
|
|
|
|
|
|
|
|
$getJobChildJobs = @(
|
|
|
|
@{ parameters = @{ IncludeChildJob = $true }; property = '-IncludeChildJob'},
|
|
|
|
@{ parameters = @{ ChildJobState = 'Completed' }; property = '-ChildJobState'}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
AfterEach {
|
|
|
|
Get-Job | Where-Object { $_.Id -ne $startedJob.Id } | Remove-Job -ErrorAction SilentlyContinue -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Get-Job with <property>' -TestCases $getJobTestCases {
|
|
|
|
param($parameters)
|
|
|
|
$job = Get-Job @parameters
|
|
|
|
ValidateJobInfo -job $job -state 'Completed' -hasMoreData $true -command ' 1 + 1 '
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Get-Job with <property>' -TestCases $getJobSwitches {
|
|
|
|
param($parameters)
|
|
|
|
$job = Get-Job @parameters
|
|
|
|
ValidateJobInfo -job $job -state 'Completed' -hasMoreData $true -Name 'StartedJob'
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Get-Job with <property>' -TestCases $getJobChildJobs {
|
|
|
|
param($parameters)
|
|
|
|
$jobs = Get-Job @parameters
|
2018-03-14 20:13:32 +01:00
|
|
|
$jobs.Count | Should -Be 2
|
2017-10-02 22:43:43 +02:00
|
|
|
ValidateJobInfo -job $jobs[0] -state 'Completed' -hasMoreData $true -Name 'StartedJob'
|
|
|
|
ValidateJobInfo -job $jobs[1] -state 'Completed' -hasMoreData $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'Remove-Job tests' {
|
|
|
|
# The test pattern used here is different from other tests since there is a scoping issue in Pester.
|
|
|
|
# If BeforeEach is used then $removeJobTestCases does not bind when the It is called.
|
|
|
|
# This implementation works around the problem by using a BeforeAll and creating a job inside the It.
|
|
|
|
BeforeAll {
|
|
|
|
$removeJobTestCases = @(
|
|
|
|
@{ property = 'Name'}
|
|
|
|
@{ property = 'Id'}
|
|
|
|
@{ property = 'InstanceId'}
|
|
|
|
@{ property = 'State'}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Remove-Job with <property>' -TestCases $removeJobTestCases {
|
|
|
|
param($property)
|
|
|
|
$jobToRemove = Start-Job -ScriptBlock { 1 + 1 } -Name 'JobToRemove' | Wait-Job
|
|
|
|
$splat = @{ $property = $jobToRemove.$property }
|
|
|
|
Remove-Job @splat
|
2018-03-14 20:13:32 +01:00
|
|
|
Get-Job $jobToRemove -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'Stop-Job tests' {
|
|
|
|
# The test pattern used here is different from other tests since there is a scoping issue in Pester.
|
|
|
|
# If BeforeEach is used then $stopJobTestCases does not bind when the It is called.
|
|
|
|
# This implementation works around the problem by using a BeforeAll and creating a job inside the It.
|
|
|
|
BeforeAll {
|
|
|
|
$stopJobTestCases = @(
|
|
|
|
@{ property = 'Name'}
|
|
|
|
@{ property = 'Id'}
|
|
|
|
@{ property = 'InstanceId'}
|
|
|
|
@{ property = 'State'}
|
|
|
|
)
|
2020-02-06 00:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BeforeEach {
|
|
|
|
# 20 seconds is chosen to be large, so that the job is in running state when Stop-Job is called.
|
2020-05-07 14:00:30 +02:00
|
|
|
$jobToStop = Start-Job -Scriptblock {
|
2020-02-06 00:27:57 +01:00
|
|
|
1..80 | ForEach-Object {
|
|
|
|
Write-Output $_
|
|
|
|
Start-Sleep -Milliseconds 250
|
|
|
|
}
|
|
|
|
} -Name 'JobToStop'
|
|
|
|
# Wait until the job is actually running and executing the script
|
|
|
|
do {
|
|
|
|
$data = Receive-Job -Job $jobToStop
|
|
|
|
} while (($data.Count -eq 0) -and ($jobToStop.State -eq 'Running'))
|
|
|
|
}
|
|
|
|
|
|
|
|
AfterEach {
|
|
|
|
Remove-Job $jobToStop -Force -ErrorAction SilentlyContinue
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'Can Stop-Job with <property>' -TestCases $stopJobTestCases {
|
|
|
|
param($property)
|
|
|
|
$splat = @{ $property = $jobToStop.$property }
|
|
|
|
Stop-Job @splat
|
|
|
|
ValidateJobInfo -job $jobToStop -state 'Stopped' -hasMoreData $false
|
|
|
|
}
|
2016-11-10 07:44:39 +01:00
|
|
|
}
|
2020-03-28 11:44:23 +01:00
|
|
|
|
|
|
|
Context 'Background pwsh process should terminate after job is done' {
|
|
|
|
It "Can clean up background pwsh process after job is done" {
|
|
|
|
$job = Start-Job { $pid }
|
|
|
|
$processId = Receive-Job $job -Wait
|
|
|
|
|
|
|
|
try {
|
|
|
|
$process = Get-Process -Id $processId -ErrorAction Stop
|
|
|
|
Wait-UntilTrue { $process.HasExited } -IntervalInMilliseconds 300 | Should -BeTrue
|
|
|
|
} catch {
|
|
|
|
$_.FullyQualifiedErrorId | Should -BeExactly 'NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand'
|
|
|
|
}
|
|
|
|
|
|
|
|
Remove-Job $job -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Can clean up background pwsh process when job is stopped" {
|
|
|
|
$job = Start-Job { $pid; Start-Sleep -Second 10 }
|
|
|
|
|
|
|
|
# Wait for the pid to be received.
|
|
|
|
Wait-UntilTrue { [bool](Receive-Job $job -Keep) } | Should -BeTrue
|
|
|
|
$processId = Receive-Job $job
|
|
|
|
|
|
|
|
# Stop the job and wait for the cleanup to finish.
|
|
|
|
Stop-Job $job
|
|
|
|
|
|
|
|
try {
|
|
|
|
$process = Get-Process -Id $processId -ErrorAction Stop
|
|
|
|
Wait-UntilTrue { $process.HasExited } -IntervalInMilliseconds 300 | Should -BeTrue
|
|
|
|
} catch {
|
|
|
|
$_.FullyQualifiedErrorId | Should -BeExactly 'NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand'
|
|
|
|
}
|
|
|
|
|
|
|
|
Remove-Job $job -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Can clean up background pwsh process when job is removed" {
|
|
|
|
$job = Start-Job { $pid; Start-Sleep -Second 10 }
|
|
|
|
|
|
|
|
# Wait for the pid to be received.
|
|
|
|
Wait-UntilTrue { [bool](Receive-Job $job -Keep) } | Should -BeTrue
|
|
|
|
$processId = Receive-Job $job
|
|
|
|
|
|
|
|
# Remove the job and wait for the cleanup to finish.
|
|
|
|
Remove-Job $job -Force
|
|
|
|
|
|
|
|
try {
|
|
|
|
$process = Get-Process -Id $processId -ErrorAction Stop
|
|
|
|
Wait-UntilTrue { $process.HasExited } -IntervalInMilliseconds 300 | Should -BeTrue
|
|
|
|
} catch {
|
|
|
|
$_.FullyQualifiedErrorId | Should -BeExactly 'NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 22:43:43 +02:00
|
|
|
}
|