Repository navigation
Remoting and Job tests #4928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remoting and Job tests #4928
Changes from all commits
b77439f
04e1e89
fc61c0f
0f83e79
dd9e137
e978771
ad62f14
fb69dff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 ' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'} | ||
| ) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do a |
||
| ) | ||
|
|
||
| $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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 == nullis always false. Which makes the if always false due to the &&. Hence unreachable code.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.