Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,6 @@ protected override void BeginProcessing()
throw new InvalidOperationException(RemotingErrorIdStrings.AsJobAndDisconnectedError);
}

if (this.InvokeAndDisconnect &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this check removed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ComputerName cannot be null due to ParameterValidation. So this.ComputerName == null is always false. Which makes the if always false due to the &&. Hence unreachable code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Can you also remove the error message from RemotingErrorIdStrings.resx and RemotingErrorIdStrings.cs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

(this.ComputerName == null || this.ComputerName.Length == 0) &&
(this.ConnectionUri == null || this.ConnectionUri.Length == 0))
{
throw new InvalidOperationException(RemotingErrorIdStrings.InvokeDisconnectedWithoutComputerName);
}

if (MyInvocation.BoundParameters.ContainsKey("SessionName") && !this.InvokeAndDisconnect)
{
throw new InvalidOperationException(RemotingErrorIdStrings.SessionNameWithoutInvokeDisconnected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,6 @@ Do you want to continue?</value>
<data name="JobIdentifierNull" xml:space="preserve">
<value>The JobIdentifier provided must not be null. Please provide a valid JobIdentifier.</value>
</data>
<data name="InvokeDisconnectedWithoutComputerName" xml:space="preserve">
<value>No computer names or connection Uris were specified. You must provide a computer name or connection Uri when invoking a command with the -Disconnected switch.</value>
</data>
<data name="JobBlockedSoWaitJobCannotContinue" xml:space="preserve">
<value>The Wait-Job cmdlet cannot finish working, because one or more jobs are blocked waiting for user interaction. Process interactive job output by using the Receive-Job cmdlet, and then try again.</value>
</data>
Expand Down
293 changes: 270 additions & 23 deletions test/powershell/engine/Job/Jobs.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,38 +1,285 @@
Describe 'Basic Job Tests' -Tags 'CI' {

BeforeAll {
$job = Start-Job {1}
# Make sure we do not have any jobs running
Get-Job | Remove-Job -Force
$timeBeforeStartedJob = Get-Date
$startedJob = Start-Job -Name 'StartedJob' -ScriptBlock { 1 + 1 } | Wait-Job
$timeAfterStartedJob = Get-Date

function script:ValidateJobInfo($job, $state, $hasMoreData, $command)
{
$job.State | Should BeExactly $state
$job.HasMoreData | Should Be $hasMoreData

if($command -ne $null)
{
$job.Command | Should BeExactly $command
}
}
}

It 'Test job creation' {
$job | should not be $null
AfterAll {
$startedJob | Remove-Job -Force -ErrorAction SilentlyContinue
}

It 'Test job State' {
Wait-Job $job -Timeout 60
$job.JobStateInfo.State -eq 'Completed' | should be $true
Context 'Basic tests' {

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 '
$result | Should Be 2
}

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 '

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not as concerned about this intermediate state race condition because the second Start-Job takes time. So we are probably Ok.

$result = $job | Wait-Job | Receive-Job
$result | Should Be 2
}

It 'Can get errors messages from job' {
$job = Start-Job -ScriptBlock { throw 'MyError' } | Wait-Job
Receive-Job -Job $job -ErrorVariable ev -ErrorAction SilentlyContinue
$ev[0].Exception.Message | Should BeExactly 'MyError'
}

It 'Can get warning messages from job' {
$job = Start-Job -ScriptBlock { Write-Warning 'MyWarning' } | Wait-Job
Receive-Job -Job $job -WarningVariable wv -WarningAction SilentlyContinue
$wv | Should BeExactly 'MyWarning'
}

It 'Can get verbose message from job' {
$job = Start-Job -ScriptBlock { Write-Verbose -Verbose 'MyVerbose' } | Wait-Job
$VerboseMsg = $job.ChildJobs[0].verbose.readall()
$VerboseMsg | Should BeExactly 'MyVerbose'
}

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()
$ProgressMsg[0].Activity | Should BeExactly 1
$ProgressMsg[0].StatusDescription | Should BeExactly 2
}

It "Create job with native command" {
try {
$nativeJob = Start-job { powershell -c 1+1 }
$nativeJob | Wait-Job
$nativeJob.State | Should BeExactly "Completed"
$nativeJob.HasMoreData | Should Be $true
Receive-Job $nativeJob | Should BeExactly 2
Remove-Job $nativeJob
{ Get-Job $nativeJob -ErrorAction Stop } | ShouldBeErrorId "JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.GetJobCommand"
}
finally {
Remove-Job $nativeJob -Force -ErrorAction SilentlyContinue
}
}
}

It 'Job output test' {
Receive-Job $job -wait | should be 1
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'}
)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AfterEach cleanup is needed within this Context as well.

Comment applies to all Context blocks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


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.
$result | Should BeLessThan 4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a check to make sure that none of the jobs threw errors make sense here too?

## Check none of the jobs threw errors.
$jobs.Error | Should BeNullOrEmpty
}

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 '
}
}

It "Create job with native command" {
try {
$nativeJob = Start-job { powershell -c 1+1 }
$nativeJob | Wait-Job
$nativeJob.State | Should BeExactly "Completed"
$nativeJob.HasMoreData | Should Be $true
Receive-Job $nativeJob | Should BeExactly 2
Remove-Job $nativeJob
{ Get-Job $nativeJob -ErrorAction Stop } | ShouldBeErrorId "JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.GetJobCommand"
Context 'Receive-job tests' {
It 'Can Receive-Job with state change events' {
$result = Start-Job -Name 'ReceiveWriteEventsJob' -ScriptBlock { 1 + 1 } | Receive-Job -Wait -WriteEvents
$result.Count | Should Be 3
$result[0] | Should Be 2
$result[1].GetType().FullName | Should BeExactly 'System.Management.Automation.JobStateEventArgs'
}
finally {
Remove-Job $nativeJob -Force -ErrorAction SilentlyContinue

It 'Can Receive-Job with job object and result' {
$result = Start-Job -ScriptBlock { 1 + 1 } | Receive-Job -Wait -WriteJobInResults
$result.Count | Should Be 2
ValidateJobInfo -job $result[0] -command ' 1 + 1 ' -state 'Completed' -hasMoreData $false
$result[1] | Should Be 2
$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
$result | Should Be 2
{ Get-Job -Name 'ReceiveJobAutoRemove' -ErrorAction Stop } | ShouldBeErrorId 'JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.GetJobCommand'
}

It 'Can Receive-Job and keep results' {
$job = Start-Job -ScriptBlock { 1 + 1 } | Wait-Job
$result = Receive-Job -Keep -Job $job
$result | Should Be 2
$result2 = Receive-Job -Job $job
$result2 | Should Be 2
$result3 = Receive-Job -Job $job
$result3 | Should BeNullOrEmpty
$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
$result | Should BeNullOrEmpty
$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
$result | Should Be 2
$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
$result | Should Be 2
$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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Organizational nitpick: This is a Receive-Job test that is included within the Wait-Job set of tests

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved. Good catch.

ValidateJobInfo -job $job -state 'Completed' -hasMoreData $false -command ' 1 + 1 '
$result | Should Be 2
}
}

AfterAll {
Remove-Job $job -Force
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'}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these tests require there only be a single job ($startedJob) in the job collection. An AfterEach or BeforeEach block (as mentioned in an above comment) would be useful:
AfterEach { Get-Job | ? Id -ne $startedJob.Id | Remove-Job }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do a Get-Job | Remove-Job -Force in the Describe's BeforeAll, I think that should be sufficient. Thoughts?

)

$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
$jobs.Count | Should Be 2
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue for this in the Pester repo? If so, please include a link

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to do a deeper analysis to file an issue. It could be Pester or PowerShell scoping issue.

# 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
Get-Job $jobToRemove -ErrorAction SilentlyContinue | Should BeNullOrEmpty
}
}

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'}
)
# '-Seconds 100' is chosen to be substantially large, so that the job is in running state when Stop-Job is called.
$jobToStop = Start-Job -ScriptBlock { Start-Sleep -Seconds 100 } -Name 'JobToStop'
}

It 'Can Stop-Job with <property>' -TestCases $stopJobTestCases {
param($property)
$splat = @{ $property = $jobToStop.$property }
Stop-Job @splat
ValidateJobInfo -job $jobToStop -state 'Stopped' -hasMoreData $false
}

AfterAll {
Remove-Job $jobToStop -Force -ErrorAction SilentlyContinue
}
}
}
}
Loading