Skip to content

Commit

Permalink
Fixed Get-RSJob errors with non-unique identification parameters.
Browse files Browse the repository at this point in the history
Some work on deprecating parameters for the Wait/Stop/Receive/Remove cmdlets
They are not used because no one reports a “non-unique” error :)
  • Loading branch information
MVKozlov committed Dec 4, 2018
1 parent 2a97f9e commit 6b6f271
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
12 changes: 8 additions & 4 deletions PoshRSJob/Public/Get-RSJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,17 @@ Function Get-RSJob {
else {
Write-Verbose "Jobs by $Property"
# And Hash much faster than foreach{ foreach {} } inner loop
$Hash = @{}
$Hash = New-Object -TypeName 'System.Collections.Generic.Dictionary`2[[System.Object],[System.Array]]'
foreach ($jobobj in $PoshRS_Jobs) {
$Hash[$jobobj.$Property] = $jobobj
$k = $jobobj.$Property
if (-not $Hash.ContainsKey($k)) {
$Hash[$k] = @()
}
$Hash[$k] += $jobobj
}
foreach ($prop in $SearchProps) {
if ($Hash.Contains($prop)) {
[void]$ResultJobs.Add($Hash[$prop])
if ($Hash.ContainsKey($prop)) {
[void]$ResultJobs.AddRange($Hash[$prop])
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions PoshRSJob/Public/Receive-RSJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,9 @@ Function Receive-RSJob {
}
}
End {
if (-not $List.Count) { return } # No jobs selected to search
$PSBoundParameters[$Property] = $List
[array]$ToReceive = Get-RSJob @PSBoundParameters

if ($ToReceive.Count) {
$ToReceive | ForEach-Object{
$_ | WriteStream
if ($isReseivedStates -contains $_.State) {
$_ | SetIsReceived -SetTrue
}
}
if ($List.Count) { # obsolete parameter sets used
$PSBoundParameters[$Property] = $List
Get-RSJob @PSBoundParameters | Receive-RSJob
}
}
}
26 changes: 4 additions & 22 deletions PoshRSJob/Public/Stop-RSJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,10 @@ Function Stop-RSJob {
}
}
End {
if (-not $List.Count) { return } # No jobs selected to search
$PSBoundParameters[$Property] = $List
[void]$PSBoundParameters.Remove('PassThru')
[array]$ToStop = Get-RSJob @PSBoundParameters

If ($ToStop.Count) {
[System.Threading.Monitor]::Enter($PoshRS_jobs.syncroot)
try {
$ToStop | ForEach-Object {
Write-Verbose "Stopping $($_.InstanceId)"
if ($_.State -ne 'Completed') {
Write-Verbose "Killing job $($_.InstanceId)"
[void] $_.InnerJob.Stop()
}
if ($PassThru) {
$_
}
}
}
finally {
[System.Threading.Monitor]::Exit($PoshRS_jobs.syncroot)
}
if ($List.Count) { # obsolete parameter sets used
$PSBoundParameters[$Property] = $List
[void]$PSBoundParameters.Remove('PassThru')
Get-RSJob @PSBoundParameters | Stop-RSJob -PassThru:$PassThru
}
}
}
24 changes: 21 additions & 3 deletions Tests/PoshRSJob.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ Describe "Get-RSJob PS$PSVersion" {
$Jobs.Count | Should Be 5
}

It 'should return jobs by batch' {
1..5 | Start-RSJob { Start-Sleep -Seconds 5; $_ } -Batch testbatch1
1..5 | Start-RSJob { Start-Sleep -Seconds 5; $_ } -Batch testbatch2
$Jobs = @(Get-RSJob -Batch testbatch2)
$Jobs.Count | Should Be 5
}

It 'should return job details <Case>' -TestCases $ParameterTestCases {
param(
$Case,
Expand Down Expand Up @@ -309,7 +316,7 @@ Describe "Stop-RSJob PS$PSVersion" {
Start-Sleep -Milliseconds 100
$Job.State | Should be 'Stopped'
}
It 'should stop a job <Case>' -TestCases $ParameterTestCases {
It 'should stop a job <Case>' -TestCases $ParameterTestCases[0,1] { # other cases obsolete
param(
$Case,
$Mode,
Expand Down Expand Up @@ -393,7 +400,7 @@ Describe "Receive-RSJob PS$PSVersion" {
Receive-RSJob | Should BeNullOrEmpty
}

It 'should retrieve job data <Case>' -TestCases $ParameterTestCases {
It 'should retrieve job data <Case>' -TestCases $ParameterTestCases[0,1] { # other cases obsolete
param(
$Case,
$Mode,
Expand Down Expand Up @@ -433,7 +440,7 @@ Describe "Remove-RSJob PS$PSVersion" {
Remove-RSJob | Should BeNullOrEmpty
}

It 'should only remove specified jobs <Case>' -TestCases $ParameterTestCases {
It 'should only remove specified jobs <Case>' -TestCases $ParameterTestCases[0,1] { # other cases obsolete
param(
$Case,
$Mode,
Expand Down Expand Up @@ -496,6 +503,17 @@ Describe "Remove-RSJob PS$PSVersion" {
}
}

Describe "Test proper job pipelining for Wait/Receive" {
It "Should receive data as soon as wait done" {
$jobs = (Start-RSJob { Start-Sleep -Seconds 5; [datetime]::Now }),
(Start-RSJob { Start-Sleep -Seconds 10; [datetime]::Now })
$objects = $jobs | Wait-RSJob | Receive-RSJob | ForEach-Object { [PSCustomObject]@{JTime = $_; RTime= [datetime]::Now} }
$objects.Count | Should be 2
($objects[0].RTime - $objects[0].JTime).TotalSeconds -le 0.5 | Should Be $true
($objects[1].RTime - $objects[1].JTime).TotalSeconds -le 0.5 | Should Be $true
}
}

Describe "Test RSJob Throttling" {
It "Full Pipe input" {
$StartDate = Get-Date
Expand Down

0 comments on commit 6b6f271

Please sign in to comment.