Skip to content

Commit

Permalink
done pester tests, few code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Jun 28, 2023
1 parent 0f82f0c commit eb44912
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 61 deletions.
16 changes: 10 additions & 6 deletions Module/private/InvocationManager.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using namespace System.Collections.Generic
using namespace System.Diagnostics
using namespace System.Management.Automation
using namespace System.Management.Automation.Host
using namespace System.Threading

Expand Down Expand Up @@ -75,12 +76,15 @@ class InvocationManager : IDisposable {
}

[void] GetTaskResult([PSParallelTask] $Task) {
$this.Tasks.Remove($Task)
$this.Release($Task.GetRunspace())
$Task.EndInvoke()

if ($Task -is [IDisposable]) {
$Task.Dispose()
try {
$this.Tasks.Remove($Task)
$this.Release($Task.GetRunspace())
$Task.EndInvoke()
}
finally {
if ($Task -is [IDisposable]) {
$Task.Dispose()
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions Module/private/PSParallelTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class PSParallelTask : IDisposable {
$this.Cmdlet.WriteObject($this.Instance.EndInvoke($this.AsyncResult), $true)
$this.GetErrors()
}
catch [PipelineStoppedException] {
$this.Cmdlet.WriteError($_)
}
catch {
$this.Cmdlet.WriteError($_)
}
Expand Down
12 changes: 9 additions & 3 deletions Module/public/Invoke-Parallel.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using namespace System.Management.Automation.Language
using namespace System.Management.Automation.Runspaces
using namespace System.Text

# .ExternalHelp PSParallelPipeline-help.xml
function Invoke-Parallel {
Expand Down Expand Up @@ -38,12 +39,17 @@ function Invoke-Parallel {
$iss = [initialsessionstate]::CreateDefault2()

foreach ($key in $Variables.PSBase.Keys) {
$iss.Variables.Add([SessionStateVariableEntry]::new($key, $Variables[$key], ''))
$iss.Variables.Add(
[SessionStateVariableEntry]::new($key, $Variables[$key], ''))
}

foreach ($function in $Functions) {
$def = (Get-Command $function).Definition
$iss.Commands.Add([SessionStateFunctionEntry]::new($function, $def))
$def = $PSCmdlet.InvokeCommand.GetCommand(
$function,
[System.Management.Automation.CommandTypes]::Function)

$iss.Commands.Add(
[SessionStateFunctionEntry]::new($function, $def.Definition))
}

$usingParams = @{}
Expand Down
2 changes: 1 addition & 1 deletion docs/en-US/Invoke-Parallel.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Get-Process | Invoke-Parallel {

If the timeout in seconds is reached all parallel invocations are stopped.

### Example 5: Using a new runspace for each invocation
### Example 7: Using a new runspace for each invocation

```powershell
0..5 | Invoke-Parallel { [runspace]::DefaultRunspace.InstanceId }
Expand Down
51 changes: 0 additions & 51 deletions examples/examples.ps1

This file was deleted.

42 changes: 42 additions & 0 deletions tests/PSParallelPipeline.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
$items | Should -HaveCount 11
}

It 'Should process in parallel' {
$timer = [System.Diagnostics.Stopwatch]::StartNew()
0..5 | Invoke-Parallel { Start-Sleep 1 } -ThrottleLimit 5
$timer.Elapsed | Should -BeLessOrEqual ([timespan]::FromSeconds(5))
$timer.Stop()
}

It 'Should stop processing after a set timeout' {
$timer = [System.Diagnostics.Stopwatch]::StartNew()

{ 0..5 | Invoke-Parallel { Start-Sleep 10 } -TimeoutSeconds 5 -ErrorAction Stop } |
Should -Throw

$timer.Elapsed | Should -BeLessOrEqual ([timespan]::FromSeconds(5.5))
$timer.Stop()
}

It 'Allows $using: statements' {
$message = 'Hello world from {0:D2}'
$items = 0..10 | Invoke-Parallel { $using:message -f $_ } |
Expand Down Expand Up @@ -61,5 +78,30 @@

$dict[$PID].ProcessName | Should -Be (Get-Process -Id $PID).ProcessName
}

It 'Should add functions to the parallel scope with -Functions parameter' {
# This test is broken in Pester, need to figure out why
return

function Test-Function {
param($s)
'Hello {0:D2}' -f $s
}

$invokeParallelSplat = @{
Functions = 'Test-Function'
ScriptBlock = { Test-Function $_ }
}

0..10 | Invoke-Parallel @invokeParallelSplat |
Sort-Object |
Should -BeExactly @(0..10 | ForEach-Object { Test-Function $_ })
}

It 'Should autocomplete existing commands in the caller scope' {
$result = TabExpansion2 -inputScript ($s = 'Invoke-Parallel -Function Get-') -cursorColumn $s.Length
$result.CompletionMatches.Count | Should -BeGreaterThan 0
$result.CompletionMatches.ListItemText | Should -Match '^Get-'
}
}
}

0 comments on commit eb44912

Please sign in to comment.