File size: 25,553 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' {
BeforeAll {
$sb = { "Hello!" }
$psosb = [psobject]{ "Hello!" }
}
It "Verifies dollar underbar variable" {
$expected = 1..10
$result = $expected | ForEach-Object -Parallel { $_ }
$result.Count | Should -BeExactly $expected.Count
$result | Should -Contain 1
$result | Should -Contain 10
}
It 'Verifies using variables' {
$var = "Hello"
$varArray = "Hello","There"
$result = 1..1 | ForEach-Object -Parallel { $using:var; $using:varArray[1] }
$result.Count | Should -BeExactly 2
$result[0] | Should -BeExactly $var
$result[1] | Should -BeExactly $varArray[1]
}
It 'Verifies in scope using variables in nested calls' {
$Test = "Test1"
$results = 1..2 | ForEach-Object -Parallel {
$using:Test
$Test = "Test2"
1..2 | ForEach-Object -Parallel {
$using:Test
$Test = "Test3"
1..2 | ForEach-Object -Parallel {
$using:Test
}
}
}
$results.Count | Should -BeExactly 14
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
$groups['Test3'].Count | Should -BeExactly 8
}
It 'Verifies in scope using variables with different names in nested calls' {
$Test1 = "TestA"
$results = 1..2 | ForEach-Object -parallel {
$using:Test1
$Test2 = "TestB"
1..2 | ForEach-Object -parallel {
$using:Test2
}
}
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['TestA'].Count | Should -BeExactly 2
$groups['TestB'].Count | Should -BeExactly 4
}
It 'Verifies using variable in nested scriptblock' {
$test = 'testC'
$results = 1..2 | ForEach-Object -parallel {
& { $using:test }
}
$results.Count | Should -BeExactly 2
$groups = $results | Group-Object -AsHashTable
$groups['TestC'].Count | Should -BeExactly 2
}
It 'Verifies expected error for out of scope using variable in nested calls' {
$Test = "TestZ"
1..1 | ForEach-Object -Parallel {
$using:Test
# Variable '$Test' is not defined in this scope.
1..1 | ForEach-Object -Parallel {
$using:Test
}
} -ErrorVariable usingErrors 2>$null
$usingErrors[0].FullyQualifiedErrorId | Should -BeExactly 'UsingVariableIsUndefined,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies using variables with passed-in script block object' {
$var = "Hello"
$varArray = "Hello","There"
$sBlock = [scriptblock]::Create('$using:var; $using:varArray[1]')
$result = 1..1 | ForEach-Object -Parallel $sBlock
$result[0] | Should -BeExactly $var
$result[1] | Should -BeExactly $varArray[1]
}
It 'Verifies using variables with passed-in {} script block object' {
$var = "Hello"
$varArray = "Hello","There"
$sBlock = { $using:var; $using:varArray[1] }
$result = 1..1 | ForEach-Object -Parallel $sBlock
$result[0] | Should -BeExactly $var
$result[1] | Should -BeExactly $varArray[1]
}
It 'Verifies in scope using same-name variables in nested calls for passed-in script block objects' {
$Test1 = "Test1"
$sBlock = [scriptblock]::Create(@'
$using:Test1
$Test2 = "Test2"
$sBlock2 = [scriptblock]::Create('$using:Test2')
1..2 | ForEach-Object -Parallel $sBlock2
'@)
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using same-name variables in nested calls for passed-in {} script block objects' {
$Test1 = "Test1"
$sBlock = {
$using:Test1
$Test2 = "Test2"
$sBlock2 = [scriptblock]::Create('$using:Test2')
1..2 | ForEach-Object -Parallel $sBlock2
}
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using same-name variables in nested calls for mixed script block objects' {
$Test1 = "Test1"
$sBlock = [scriptblock]::Create(@'
$using:Test1
$Test2 = "Test2"
1..2 | ForEach-Object -Parallel { $using:Test2 }
'@)
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using same-name variables in nested calls for mixed script block {} objects' {
$Test1 = "Test1"
$sBlock = {
$using:Test1
$Test2 = "Test2"
1..2 | ForEach-Object -Parallel { $using:Test2 }
}
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using different-name variables in nested calls for passed-in script block objects' {
$Test1 = "Test1"
$sBlock = [scriptblock]::Create(@'
$using:Test1
$Test2 = "Test2"
$sBlock2 = [scriptblock]::Create('$using:Test2')
1..2 | ForEach-Object -Parallel $sBlock2
'@)
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using different-name variables in nested calls for passed-in script {} block objects' {
$Test1 = "Test1"
$sBlock = {
$using:Test1
$Test2 = "Test2"
$sBlock2 = [scriptblock]::Create('$using:Test2')
1..2 | ForEach-Object -Parallel $sBlock2
}
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using different-name variables in nested calls for mixed script block objects' {
$Test1 = "Test1"
$sBlock = [scriptblock]::Create(@'
$using:Test1
$Test2 = "Test2"
1..2 | ForEach-Object -Parallel { $using:Test2 }
'@)
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies in scope using different-name variables in nested calls for mixed script block {} objects' {
$Test1 = "Test1"
$sBlock = {
$using:Test1
$Test2 = "Test2"
1..2 | ForEach-Object -Parallel { $using:Test2 }
}
$results = 1..2 | ForEach-Object -Parallel $sBlock
$results.Count | Should -BeExactly 6
$groups = $results | Group-Object -AsHashTable
$groups['Test1'].Count | Should -BeExactly 2
$groups['Test2'].Count | Should -BeExactly 4
}
It 'Verifies expected error for out of scope using variable in nested calls for passed-in script block objects' {
$Test = "TestZ"
$sBlock = [scriptblock]::Create(@'
$using:Test
$sBlock2 = [scriptblock]::Create('$using:Test')
1..1 | ForEach-Object -Parallel $sBlock2
'@)
1..1 | ForEach-Object -Parallel $SBlock -ErrorVariable usingErrors 2>$null
$usingErrors[0].FullyQualifiedErrorId | Should -BeExactly 'UsingVariableIsUndefined,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies expected error for out of scope using variable in nested calls for passed-in {} script block objects' {
$Test = "TestZ"
$sBlock = [scriptblock]::Create(@'
$using:Test
$sBlock2 = { $using:Test }
1..1 | ForEach-Object -Parallel $sBlock2
'@)
1..1 | ForEach-Object -Parallel $SBlock -ErrorVariable usingErrors 2>$null
$usingErrors[0].FullyQualifiedErrorId | Should -BeExactly 'UsingVariableIsUndefined,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies expected error for out of scope using variable in nested calls for mixed script block objects' {
$Test = "TestZ"
$sBlock = [scriptblock]::Create(@'
$using:Test
1..1 | ForEach-Object -Parallel { $using:Test }
'@)
1..1 | ForEach-Object -Parallel $SBlock -ErrorVariable usingErrors 2>$null
$usingErrors[0].FullyQualifiedErrorId | Should -BeExactly 'UsingVariableIsUndefined,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies terminating error streaming' {
$result = 1..1 | ForEach-Object -Parallel { throw 'Terminating Error!'; "Hello" } 2>&1
$result.Count | Should -BeExactly 1
$result.ToString() | Should -BeExactly 'Terminating Error!'
$result.FullyQualifiedErrorId | Should -BeExactly 'PSTaskException'
}
It 'Verifies terminating error in multiple iterations' {
$results = 1..2 | ForEach-Object -Parallel {
if ($_ -eq 1) {
throw 'Terminating Error!'
"Hello!"
}
else {
"Goodbye!"
}
} 2>&1
$resultStrings = $results | ForEach-Object { $_.ToString() }
$resultStrings | Should -Not -Contain "Hello!"
$resultStrings | Should -Contain "Goodbye!"
$resultStrings | Should -Contain "Terminating Error!"
}
It 'Verifies non-terminating error streaming' {
$actualError = 1..1 | ForEach-Object -Parallel { Write-Error "Error!" } 2>&1
$actualError.ToString() | Should -BeExactly 'Error!'
$actualError.FullyQualifiedErrorId | Should -BeExactly 'Microsoft.PowerShell.Commands.WriteErrorException'
}
It 'Verifies warning data streaming' {
$actualWarning = 1..1 | ForEach-Object -Parallel { Write-Warning "Warning!" } 3>&1
$actualWarning.Message | Should -BeExactly 'Warning!'
}
It 'Verifies verbose data streaming' {
$actualVerbose = 1..1 | ForEach-Object -Parallel { Write-Verbose "Verbose!" -Verbose } -Verbose 4>&1
$actualVerbose.Message | Should -BeExactly 'Verbose!'
}
It 'Verifies debug data streaming' {
$actualDebug = 1..1 | ForEach-Object -Parallel { Write-Debug "Debug!" -Debug } -Debug 5>&1
$actualDebug.Message | Should -BeExactly 'Debug!'
}
It 'Verifies progress data streaming' {
$ps = [Powershell]::Create([System.Management.Automation.RunspaceMode]::NewRunspace)
$ps.Commands.AddScript("`$ProgressPreference='Continue'; 1..2 | ForEach-Object -Parallel { Write-Progress -Activity Progress -Status Running }") | Out-Null
$ps.Invoke()
$pr = $ps.Streams.Progress.ReadAll()
$pr.Count | Should -Be 2
$pr[0].Activity | Should -Be Progress
$pr[0].StatusDescription | Should -Be Running
$ps.Dispose()
}
It 'Verifies information data streaming' {
$actualInformation = 1..1 | ForEach-Object -Parallel { Write-Information "Information!" } 6>&1
$actualInformation.MessageData | Should -BeExactly 'Information!'
}
It 'Verifies error for using script block variable' {
{ 1..1 | ForEach-Object -Parallel { $using:sb } } | Should -Throw -ErrorId 'ParallelUsingVariableCannotBeScriptBlock,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies error for using script block variable in PSObject' {
{ 1..1 | ForEach-Object -Parallel { $using:psosb } } | Should -Throw -ErrorId 'ParallelUsingVariableCannotBeScriptBlock,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies error for script block piped variable' {
$actualError = $sb | ForEach-Object -Parallel { "Hello" } 2>&1
$actualError.FullyQualifiedErrorId | Should -BeExactly 'ParallelPipedInputObjectCannotBeScriptBlock,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies that parallel script blocks run in FullLanguage mode by default' {
$results = 1..1 | ForEach-Object -Parallel { $ExecutionContext.SessionState.LanguageMode }
$results | Should -BeExactly 'FullLanguage'
}
It 'Verifies that the current working directory is preserved' {
$parallelScriptLocation = 1..1 | ForEach-Object -Parallel { $PWD }
$parallelScriptLocation.Path | Should -BeExactly $PWD.Path
}
It 'Verifies that the current working directory can have wildcards in its name' {
$oldLocation = Get-Location
$wildcardName = New-Item -Path 'TestDrive:\' -Name '[' -ItemType Directory
Set-Location -LiteralPath $wildcardName.FullName
try
{
{ 1..1 | ForEach-Object -Parallel { $PWD } } | Should -Not -Throw
$wildcardPathResult = 1..1 | ForEach-Object -Parallel { $PWD }
$wildcardPathResult.Path | Should -BeExactly $PWD.Path
}
finally
{
Set-Location -Path $oldLocation
if ($drive -is [System.IO.DirectoryInfo]) {
$drive | Remove-Item -Force
}
}
}
It 'Verifies no terminating error if current working drive is not found' {
$oldLocation = Get-Location
try
{
New-PSDrive -Name ZZ -PSProvider FileSystem -Root $TestDrive
Set-Location -Path 'ZZ:'
{ 1..1 | ForEach-Object -Parallel { $_ } } | Should -Not -Throw
}
finally
{
Set-Location -Path $oldLocation
}
}
}
Describe 'ForEach-Object -Parallel common parameters' -Tags 'CI' {
BeforeAll {
# Test cases
$TestCasesNotSupportedCommonParameters = @(
@{
testName = 'Verifies that ErrorAction common parameter is not supported'
scriptBlock = { 1..1 | ForEach-Object -Parallel { "Hello" } -ErrorAction Stop }
},
@{
testName = 'Verifies that WarningAction common parameter is not supported'
scriptBlock = { 1..1 | ForEach-Object -Parallel { "Hello" } -WarningAction SilentlyContinue }
},
@{
testName = 'Verifies that InformationAction common parameter is not supported'
scriptBlock = { 1..1 | ForEach-Object -Parallel { "Hello" } -InformationAction SilentlyContinue }
},
@{
testName = 'Verifies that PipelineVariable common parameter is not supported'
scriptBlock = { 1..1 | ForEach-Object -Parallel { "Hello" } -PipelineVariable pipeVar }
}
)
$TestCasesForSupportedCommonParameters = @(
@{
testName = 'Verifies ErrorVariable common parameter'
scriptBlock = { 1..1 | ForEach-Object -Parallel { Write-Error "Error:$_" } -ErrorVariable global:actualVariable }
expectedResult = 'Error:1'
},
@{
testName = 'Verifies WarningVarible common parameter'
scriptBlock = { 1..1 | ForEach-Object -Parallel { Write-Warning "Warning:$_" } -WarningVariable global:actualVariable }
expectedResult = 'Warning:1'
},
@{
testName = 'Verifies InformationVariable common parameter'
scriptBlock = { 1..1 | ForEach-Object -Parallel { Write-Information "Information:$_" } -InformationVariable global:actualVariable }
expectedResult = 'Information:1'
},
@{
testName = 'Verifies OutVariable common parameter'
scriptBlock = { 1..1 | ForEach-Object -Parallel { Write-Output "Output:$_" } -OutVariable global:actualVariable }
expectedResult = 'Output:1'
}
)
}
BeforeEach {
$global:actualVariable = $null
}
AfterAll {
$global:actualVariable = $null
}
It "<testName>" -TestCases $TestCasesNotSupportedCommonParameters {
param ($scriptBlock)
{ & $scriptBlock } | Should -Throw -ErrorId 'ParallelCommonParametersNotSupported,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It "<testName>" -TestCases $TestCasesForSupportedCommonParameters {
param ($scriptBlock, $expectedResult)
& $scriptBlock *>$null
$global:actualVariable[0].ToString() | Should -BeExactly $expectedResult
}
}
Describe 'ForEach-Object -Parallel -AsJob Basic Tests' -Tags 'CI' {
It 'Verifies TimeoutSeconds parameter is excluded from AsJob' {
{ 1..1 | ForEach-Object -AsJob -Parallel { "Hello" } -TimeoutSeconds 60 } | Should -Throw -ErrorId 'ParallelCannotUseTimeoutWithJob,Microsoft.PowerShell.Commands.ForEachObjectCommand'
}
It 'Verifies ForEach-Object -Parallel jobs appear in job repository' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { "Hello" }
Get-Job | Should -Contain $job
$job | Wait-Job | Remove-Job
}
It 'Verifies dollar underbar variable' {
$expected = 1..10
$job = $expected | ForEach-Object -AsJob -Parallel { $_ }
$result = $job | Wait-Job | Receive-Job
$job | Remove-Job
$result.Count | Should -BeExactly $expected.Count
$result | Should -Contain 1
$result | Should -Contain 10
}
It 'Verifies using variables' {
$Var1 = "Hello"
$Var2 = "Goodbye"
$Var3 = 105
$Var4 = "One","Two","Three"
$job = 1..1 | ForEach-Object -AsJob -Parallel {
Write-Output $using:Var1
Write-Output $using:Var2
Write-Output $using:Var3
Write-Output @(,$using:Var4)
Write-Output $using:Var4[1]
}
$results = $job | Wait-Job | Receive-Job
$job | Remove-Job
$results[0] | Should -BeExactly $Var1
$results[1] | Should -BeExactly $Var2
$results[2] | Should -BeExactly $Var3
$results[3] | Should -BeExactly $Var4
$results[4] | Should -BeExactly $Var4[1]
}
It 'Verifies terminating error in single iteration' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { throw "Terminating Error!"; "Hello" }
$results = $job | Wait-Job | Receive-Job 2>$null
$results.Count | Should -BeExactly 0
$job.State | Should -BeExactly 'Failed'
$job.ChildJobs[0].JobStateInfo.State | Should -BeExactly 'Failed'
$job.ChildJobs[0].JobStateInfo.Reason.Message | Should -BeExactly 'Terminating Error!'
$job | Remove-Job
}
It 'Verifies terminating error in double iteration' {
$job = 1..2 | ForEach-Object -AsJob -Parallel {
if ($_ -eq 1) {
throw "Terminating Error!"
"Goodbye!"
}
else {
"Hello!"
}
}
$results = $job | Wait-Job | Receive-Job 2>$null
$results | Should -Contain 'Hello!'
$results | Should -Not -Contain 'Goodbye!'
$job.JobStateInfo.State | Should -BeExactly 'Failed'
$job.ChildJobs[0].JobStateInfo.State | Should -BeExactly 'Failed'
$job.ChildJobs[0].JobStateInfo.Reason.Message | Should -BeExactly 'Terminating Error!'
$job.ChildJobs[1].JobStateInfo.State | Should -BeExactly 'Completed'
$job | Remove-Job
}
It 'Verifies non-terminating error' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { Write-Error "Error:$_" }
$results = $job | Wait-Job | Receive-Job 2>&1
$job | Remove-Job
$results.ToString() | Should -BeExactly "Error:1"
}
It 'Verifies warning data' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { Write-Warning "Warning:$_" }
$results = $job | Wait-Job | Receive-Job 3>&1
$job | Remove-Job
$results.Message | Should -BeExactly "Warning:1"
}
It 'Verifies verbose data' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { Write-Verbose "Verbose:$_" -Verbose }
$results = $job | Wait-Job | Receive-Job -Verbose 4>&1
$job | Remove-Job
$results.Message | Should -BeExactly "Verbose:1"
}
It 'Verifies debug data' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { Write-Debug "Debug:$_" -Debug }
$results = $job | Wait-Job | Receive-Job -Debug 5>&1
$job | Remove-Job
$results.Message | Should -BeExactly "Debug:1"
}
It 'Verifies information data' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { Write-Information "Information:$_" }
$results = $job | Wait-Job | Receive-Job 6>&1
$job | Remove-Job
$results.MessageData | Should -BeExactly "Information:1"
}
It 'Verifies job Command property' {
$job = 1..1 | ForEach-Object -AsJob -Parallel {"Hello"}
$job.Command | Should -BeExactly '"Hello"'
$job.ChildJobs[0].Command | Should -BeExactly '"Hello"'
$job | Wait-Job | Remove-Job
}
It 'Verifies that the current working directory is preserved' {
$job = 1..1 | ForEach-Object -AsJob -Parallel { $PWD }
$parallelScriptLocation = $job | Wait-Job | Receive-Job
$job | Remove-Job
$parallelScriptLocation.Path | Should -BeExactly $PWD.Path
}
}
Describe 'ForEach-Object -Parallel runspace pool tests' -Tags 'CI' {
It "Verifies job allocated runspace count is limited to pool size" {
$job = 1..4 | ForEach-Object -Parallel { Start-Sleep 1 } -AsJob -ThrottleLimit 2 | Wait-Job
$job.AllocatedRunspaceCount | Should -BeExactly 2
$job | Remove-Job
}
It "Verifies job with -UseNewRunspace switch allocates one runspace per iteration" {
$job = 1..10 | ForEach-Object -Parallel { $_ } -AsJob -ThrottleLimit 2 -UseNewRunspace | Wait-Job
$job.AllocatedRunspaceCount | Should -BeExactly 10
$job | Remove-Job
}
}
Describe 'ForEach-Object -Parallel Functional Tests' -Tags 'Feature' {
It 'Verifies job queuing and throttle limit' {
# Run four job tasks, two in parallel at a time.
$job = 1..4 | ForEach-Object -Parallel { Start-Sleep 60 } -AsJob -ThrottleLimit 2
# Wait for child job 2 to begin running for up to ten seconds
if ( !(Wait-UntilTrue -TimeoutInMilliseconds 10000 -IntervalInMilliseconds 250 `
{ $job.ChildJobs[1].JobStateInfo.State -eq 'Running' }))
{
throw "ForEach-Object -Parallel child job 2 did not start"
}
# Two job tasks should be running and two waiting to run
$job.ChildJobs[0].JobStateInfo.State | Should -BeExactly 'Running'
$job.ChildJobs[1].JobStateInfo.State | Should -BeExactly 'Running'
$job.ChildJobs[2].JobStateInfo.State | Should -BeExactly 'NotStarted'
$job.ChildJobs[3].JobStateInfo.State | Should -BeExactly 'NotStarted'
$job | Remove-Job -Force
}
It 'Verifies jobs work with Receive-Job -AutoRemove parameter' {
$job = 1..4 | ForEach-Object -AsJob -Parallel { "Hello:$_" }
$null = $job | Receive-Job -Wait -AutoRemoveJob
Get-Job | Should -Not -Contain $job
}
It 'Verifies parallel task queuing' {
$results = 10..1 | ForEach-Object -Parallel { Start-Sleep 1; $_ } -ThrottleLimit 5
$results[0] | Should -BeGreaterThan 5
$results[1] | Should -BeGreaterThan 5
$results[2] | Should -BeGreaterThan 5
$results[3] | Should -BeGreaterThan 5
$results[4] | Should -BeGreaterThan 5
}
It 'Verifies timeout and throttle parameters' {
# With ThrottleLimit set to 1, the two 60 second long script blocks will run sequentially,
# until the timeout in 5 seconds.
$results = 1..2 | ForEach-Object -Parallel { "Output $_"; Start-Sleep -Seconds 60 } -TimeoutSeconds 5 -ThrottleLimit 1 2>&1
$results.Count | Should -BeExactly 2
$results[0] | Should -BeExactly 'Output 1'
$results[1].FullyQualifiedErrorId | Should -BeExactly 'PSTaskException'
$results[1].Exception | Should -BeOfType System.Management.Automation.PipelineStoppedException
}
}
|